2 * Default window procedure
4 * Copyright 1993, 1996 Alexandre Julliard
14 #include "nonclient.h"
17 #include "debugtools.h"
24 #include "wine/unicode.h"
25 #include "wine/winuser16.h"
27 DEFAULT_DEBUG_CHANNEL(win);
29 /* bits in the dwKeyData */
30 #define KEYDATA_ALT 0x2000
31 #define KEYDATA_PREVSTATE 0x4000
33 static short iF10Key = 0;
34 static short iMenuSysKey = 0;
36 /***********************************************************************
37 * DEFWND_HandleWindowPosChanged
39 * Handle the WM_WINDOWPOSCHANGED message.
41 static void DEFWND_HandleWindowPosChanged( WND *wndPtr, UINT flags )
43 WPARAM16 wp = SIZE_RESTORED;
45 if (!(flags & SWP_NOCLIENTMOVE))
46 SendMessage16( wndPtr->hwndSelf, WM_MOVE, 0,
47 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top));
48 if (!(flags & SWP_NOCLIENTSIZE))
50 if (wndPtr->dwStyle & WS_MAXIMIZE) wp = SIZE_MAXIMIZED;
51 else if (wndPtr->dwStyle & WS_MINIMIZE) wp = SIZE_MINIMIZED;
53 SendMessage16( wndPtr->hwndSelf, WM_SIZE, wp,
54 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
55 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
60 /***********************************************************************
63 * Set the window text.
65 void DEFWND_SetTextA( WND *wndPtr, LPCSTR text )
70 count = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
72 if (wndPtr->text) HeapFree(SystemHeap, 0, wndPtr->text);
73 if ((wndPtr->text = HeapAlloc(SystemHeap, 0, count * sizeof(WCHAR))))
74 MultiByteToWideChar( CP_ACP, 0, text, -1, wndPtr->text, count );
76 ERR("Not enough memory for window text");
78 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
81 /***********************************************************************
84 * Set the window text.
86 void DEFWND_SetTextW( WND *wndPtr, LPCWSTR text )
88 static const WCHAR empty_string[] = {0};
91 if (!text) text = empty_string;
92 count = strlenW(text) + 1;
94 if (wndPtr->text) HeapFree(SystemHeap, 0, wndPtr->text);
95 if ((wndPtr->text = HeapAlloc(SystemHeap, 0, count * sizeof(WCHAR))))
96 lstrcpyW( wndPtr->text, text );
98 ERR("Not enough memory for window text");
100 wndPtr->pDriver->pSetText(wndPtr, wndPtr->text);
103 /***********************************************************************
104 * DEFWND_ControlColor
106 * Default colors for control painting.
108 HBRUSH DEFWND_ControlColor( HDC hDC, UINT16 ctlType )
110 if( ctlType == CTLCOLOR_SCROLLBAR)
112 HBRUSH hb = GetSysColorBrush(COLOR_SCROLLBAR);
113 if (TWEAK_WineLook == WIN31_LOOK) {
114 SetTextColor( hDC, RGB(0, 0, 0) );
115 SetBkColor( hDC, RGB(255, 255, 255) );
117 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
118 SetTextColor( hDC, GetSysColor(COLOR_3DFACE));
119 SetBkColor( hDC, bk);
121 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
122 * we better use 0x55aa bitmap brush to make scrollbar's background
123 * look different from the window background.
125 if (bk == GetSysColor(COLOR_WINDOW)) {
126 return CACHE_GetPattern55AABrush();
129 UnrealizeObject( hb );
133 SetTextColor( hDC, GetSysColor(COLOR_WINDOWTEXT));
135 if (TWEAK_WineLook > WIN31_LOOK) {
136 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
137 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
139 SetBkColor( hDC, GetSysColor(COLOR_3DFACE) );
140 return GetSysColorBrush(COLOR_3DFACE);
144 SetBkColor( hDC, GetSysColor(COLOR_WINDOW) );
145 return GetSysColorBrush(COLOR_WINDOW);
149 /***********************************************************************
152 static void DEFWND_SetRedraw( WND* wndPtr, WPARAM wParam )
154 BOOL bVisible = wndPtr->dwStyle & WS_VISIBLE;
156 TRACE("%04x %i\n", wndPtr->hwndSelf, (wParam!=0) );
162 wndPtr->dwStyle |= WS_VISIBLE;
163 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
168 if( wndPtr->dwStyle & WS_MINIMIZE ) wParam = RDW_VALIDATE;
169 else wParam = RDW_ALLCHILDREN | RDW_VALIDATE;
171 PAINT_RedrawWindow( wndPtr->hwndSelf, NULL, 0, wParam, 0 );
172 DCE_InvalidateDCE( wndPtr, &wndPtr->rectWindow );
173 wndPtr->dwStyle &= ~WS_VISIBLE;
177 /***********************************************************************
180 * This method handles the default behavior for the WM_PRINT message.
182 static void DEFWND_Print(
190 if ( (uFlags & PRF_CHECKVISIBLE) &&
191 !IsWindowVisible(wndPtr->hwndSelf) )
195 * Unimplemented flags.
197 if ( (uFlags & PRF_CHILDREN) ||
198 (uFlags & PRF_OWNED) ||
199 (uFlags & PRF_NONCLIENT) )
201 WARN("WM_PRINT message with unsupported flags\n");
207 if ( uFlags & PRF_ERASEBKGND)
208 SendMessageA(wndPtr->hwndSelf, WM_ERASEBKGND, (WPARAM)hdc, 0);
213 if ( uFlags & PRF_CLIENT)
214 SendMessageA(wndPtr->hwndSelf, WM_PRINTCLIENT, (WPARAM)hdc, PRF_CLIENT);
217 /***********************************************************************
220 * Default window procedure for messages that are the same in Win16 and Win32.
222 static LRESULT DEFWND_DefWinProc( WND *wndPtr, UINT msg, WPARAM wParam,
228 return NC_HandleNCPaint( wndPtr->hwndSelf, (HRGN)wParam );
231 return NC_HandleNCHitTest( wndPtr->hwndSelf, MAKEPOINT16(lParam) );
233 case WM_NCLBUTTONDOWN:
234 return NC_HandleNCLButtonDown( wndPtr, wParam, lParam );
236 case WM_LBUTTONDBLCLK:
237 case WM_NCLBUTTONDBLCLK:
238 return NC_HandleNCLButtonDblClk( wndPtr, wParam, lParam );
242 if ((wndPtr->flags & WIN_ISWIN32) || (TWEAK_WineLook > WIN31_LOOK))
244 ClientToScreen16(wndPtr->hwndSelf, (LPPOINT16)&lParam);
245 SendMessageA( wndPtr->hwndSelf, WM_CONTEXTMENU,
246 wndPtr->hwndSelf, lParam);
251 if( wndPtr->dwStyle & WS_CHILD )
252 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
253 else if (wndPtr->hSysMenu)
256 POINT16 pt = MAKEPOINT16(lParam);
258 ScreenToClient16(wndPtr->hwndSelf, &pt);
259 hitcode = NC_HandleNCHitTest(wndPtr->hwndSelf, pt);
261 /* Track system popup if click was in the caption area. */
262 if (hitcode==HTCAPTION || hitcode==HTSYSMENU)
263 TrackPopupMenu(GetSystemMenu(wndPtr->hwndSelf, FALSE),
264 TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
265 pt.x, pt.y, 0, wndPtr->hwndSelf, NULL);
270 return NC_HandleNCActivate( wndPtr, wParam );
273 if (wndPtr->text) HeapFree( SystemHeap, 0, wndPtr->text );
275 if (wndPtr->pVScroll) HeapFree( SystemHeap, 0, wndPtr->pVScroll );
276 if (wndPtr->pHScroll) HeapFree( SystemHeap, 0, wndPtr->pHScroll );
277 wndPtr->pVScroll = wndPtr->pHScroll = NULL;
281 DEFWND_Print(wndPtr, (HDC)wParam, lParam);
288 HDC16 hdc = BeginPaint16( wndPtr->hwndSelf, &ps );
291 if( (wndPtr->dwStyle & WS_MINIMIZE) && wndPtr->class->hIcon )
293 int x = (wndPtr->rectWindow.right - wndPtr->rectWindow.left -
294 GetSystemMetrics(SM_CXICON))/2;
295 int y = (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top -
296 GetSystemMetrics(SM_CYICON))/2;
297 TRACE("Painting class icon: vis rect=(%i,%i - %i,%i)\n",
298 ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom );
299 DrawIcon( hdc, x, y, wndPtr->class->hIcon );
301 EndPaint16( wndPtr->hwndSelf, &ps );
307 DEFWND_SetRedraw( wndPtr, wParam );
311 DestroyWindow( wndPtr->hwndSelf );
314 case WM_MOUSEACTIVATE:
315 if (wndPtr->dwStyle & WS_CHILD)
317 LONG ret = SendMessage16( wndPtr->parent->hwndSelf,
318 WM_MOUSEACTIVATE, wParam, lParam );
322 /* Caption clicks are handled by the NC_HandleNCLButtonDown() */
323 return (LOWORD(lParam) >= HTCLIENT) ? MA_ACTIVATE : MA_NOACTIVATE;
326 /* The default action in Windows is to set the keyboard focus to
327 * the window, if it's being activated and not minimized */
328 if (LOWORD(wParam) != WA_INACTIVE) {
329 if (!(wndPtr->dwStyle & WS_MINIMIZE))
330 SetFocus(wndPtr->hwndSelf);
335 case WM_ICONERASEBKGND:
339 if (!wndPtr->class->hbrBackground) return 0;
341 /* Since WM_ERASEBKGND may receive either a window dc or a */
342 /* client dc, the area to be erased has to be retrieved from */
343 /* the device context. */
344 GetClipBox( (HDC)wParam, &rect );
346 /* Always call the Win32 variant of FillRect even on Win16,
347 * since despite the fact that Win16, as well as Win32,
348 * supports special background brushes for a window class,
349 * the Win16 variant of FillRect does not.
351 FillRect( (HDC) wParam, &rect, wndPtr->class->hbrBackground);
358 case WM_CTLCOLORMSGBOX:
359 case WM_CTLCOLOREDIT:
360 case WM_CTLCOLORLISTBOX:
363 case WM_CTLCOLORSTATIC:
364 case WM_CTLCOLORSCROLLBAR:
365 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, msg - WM_CTLCOLORMSGBOX );
368 return (LRESULT)DEFWND_ControlColor( (HDC)wParam, HIWORD(lParam) );
370 case WM_GETTEXTLENGTH:
371 if (wndPtr->text) return (LRESULT)strlenW(wndPtr->text);
375 if (wndPtr->dwStyle & WS_CHILD)
376 if (SendMessage16(wndPtr->parent->hwndSelf, WM_SETCURSOR,
379 return NC_HandleSetCursor( wndPtr->hwndSelf, wParam, lParam );
382 return NC_HandleSysCommand( wndPtr->hwndSelf, wParam,
383 MAKEPOINT16(lParam) );
386 if(wParam == VK_F10) iF10Key = VK_F10;
390 if( HIWORD(lParam) & KEYDATA_ALT )
392 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
393 if( wParam == VK_MENU && !iMenuSysKey )
400 if( wParam == VK_F4 ) /* try to close the window */
402 HWND hWnd = WIN_GetTopParent( wndPtr->hwndSelf );
403 wndPtr = WIN_FindWndPtr( hWnd );
404 if( wndPtr && !(wndPtr->class->style & CS_NOCLOSE) )
405 PostMessage16( hWnd, WM_SYSCOMMAND, SC_CLOSE, 0 );
406 WIN_ReleaseWndPtr(wndPtr);
409 else if( wParam == VK_F10 )
412 if( wParam == VK_ESCAPE && (GetKeyState(VK_SHIFT) & 0x8000))
413 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
414 (WPARAM16)SC_KEYMENU, (LPARAM)VK_SPACE);
419 /* Press and release F10 or ALT */
420 if (((wParam == VK_MENU) && iMenuSysKey) ||
421 ((wParam == VK_F10) && iF10Key))
422 SendMessage16( WIN_GetTopParent(wndPtr->hwndSelf),
423 WM_SYSCOMMAND, SC_KEYMENU, 0L );
424 iMenuSysKey = iF10Key = 0;
429 if (wParam == VK_RETURN && (wndPtr->dwStyle & WS_MINIMIZE))
431 PostMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
432 (WPARAM16)SC_RESTORE, 0L );
435 if ((HIWORD(lParam) & KEYDATA_ALT) && wParam)
437 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
438 if (wParam == VK_SPACE && (wndPtr->dwStyle & WS_CHILD))
439 SendMessage16( wndPtr->parent->hwndSelf, msg, wParam, lParam );
441 SendMessage16( wndPtr->hwndSelf, WM_SYSCOMMAND,
442 (WPARAM16)SC_KEYMENU, (LPARAM)(DWORD)wParam );
444 else /* check for Ctrl-Esc */
445 if (wParam != VK_ESCAPE) MessageBeep(0);
449 if (!lParam) return 0; /* sent from ShowWindow */
450 if (!(wndPtr->dwStyle & WS_POPUP) || !wndPtr->owner) return 0;
451 if ((wndPtr->dwStyle & WS_VISIBLE) && wParam) return 0;
452 else if (!(wndPtr->dwStyle & WS_VISIBLE) && !wParam) return 0;
453 ShowWindow( wndPtr->hwndSelf, wParam ? SW_SHOWNOACTIVATE : SW_HIDE );
457 if (wndPtr->parent == WIN_GetDesktop()) EndMenu();
458 if (GetCapture() == wndPtr->hwndSelf) ReleaseCapture();
459 WIN_ReleaseDesktop();
469 case WM_QUERYDROPOBJECT:
470 if (wndPtr->dwExStyle & WS_EX_ACCEPTFILES) return 1;
473 case WM_QUERYDRAGICON:
478 if( (hIcon=wndPtr->class->hCursor) ) return (LRESULT)hIcon;
479 for(len=1; len<64; len++)
480 if((hIcon=LoadIcon16(wndPtr->hInstance,MAKEINTRESOURCE16(len))))
481 return (LRESULT)hIcon;
482 return (LRESULT)LoadIcon16(0,IDI_APPLICATION16);
486 case WM_ISACTIVEICON:
487 return ((wndPtr->flags & WIN_NCACTIVATED) != 0);
489 case WM_NOTIFYFORMAT:
490 if (IsWindowUnicode(wndPtr->hwndSelf)) return NFR_UNICODE;
491 else return NFR_ANSI;
494 case WM_QUERYENDSESSION:
499 int index = (wParam != ICON_SMALL) ? GCL_HICON : GCL_HICONSM;
500 HICON16 hOldIcon = GetClassLongA(wndPtr->hwndSelf, index);
501 SetClassLongA(wndPtr->hwndSelf, index, lParam);
503 SetWindowPos(wndPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
504 | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE
507 if( wndPtr->flags & WIN_NATIVE )
508 wndPtr->pDriver->pSetHostAttr(wndPtr, HAK_ICONS, 0);
515 int index = (wParam != ICON_SMALL) ? GCL_HICON : GCL_HICONSM;
516 return GetClassLongA(wndPtr->hwndSelf, index);
520 SendMessageA( wndPtr->parent->hwndSelf, msg, wParam, lParam );
529 /***********************************************************************
530 * DefWindowProc16 (USER.107)
532 LRESULT WINAPI DefWindowProc16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
535 WND * wndPtr = WIN_FindWndPtr( hwnd );
538 if (!wndPtr) return 0;
539 SPY_EnterMessage( SPY_DEFWNDPROC16, hwnd, msg, wParam, lParam );
545 CREATESTRUCT16 *cs = (CREATESTRUCT16 *)PTR_SEG_TO_LIN(lParam);
547 DEFWND_SetTextA( wndPtr, (LPCSTR)PTR_SEG_TO_LIN(cs->lpszName) );
555 CONV_RECT16TO32( (RECT16 *)PTR_SEG_TO_LIN(lParam), &rect32 );
556 result = NC_HandleNCCalcSize( wndPtr, &rect32 );
557 CONV_RECT32TO16( &rect32, (RECT16 *)PTR_SEG_TO_LIN(lParam) );
561 case WM_WINDOWPOSCHANGING:
562 result = WINPOS_HandleWindowPosChanging16( wndPtr,
563 (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam) );
566 case WM_WINDOWPOSCHANGED:
568 WINDOWPOS16 * winPos = (WINDOWPOS16 *)PTR_SEG_TO_LIN(lParam);
569 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
574 if (wParam && wndPtr->text)
576 lstrcpynWtoA( (LPSTR)PTR_SEG_TO_LIN(lParam), wndPtr->text, wParam );
577 result = (LRESULT)strlen( (LPSTR)PTR_SEG_TO_LIN(lParam) );
582 DEFWND_SetTextA( wndPtr, (LPCSTR)PTR_SEG_TO_LIN(lParam) );
583 if( wndPtr->dwStyle & WS_CAPTION ) NC_HandleNCPaint( hwnd , (HRGN)1 );
587 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
591 WIN_ReleaseWndPtr(wndPtr);
592 SPY_ExitMessage( SPY_RESULT_DEFWND16, hwnd, msg, result );
597 /***********************************************************************
598 * DefWindowProcA [USER32.126]
601 LRESULT WINAPI DefWindowProcA( HWND hwnd, UINT msg, WPARAM wParam,
604 WND * wndPtr = WIN_FindWndPtr( hwnd );
607 if (!wndPtr) return 0;
608 SPY_EnterMessage( SPY_DEFWNDPROC, hwnd, msg, wParam, lParam );
614 CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
615 if (cs->lpszName) DEFWND_SetTextA( wndPtr, cs->lpszName );
621 result = NC_HandleNCCalcSize( wndPtr, (RECT *)lParam );
624 case WM_WINDOWPOSCHANGING:
625 result = WINPOS_HandleWindowPosChanging( wndPtr,
626 (WINDOWPOS *)lParam );
629 case WM_WINDOWPOSCHANGED:
631 WINDOWPOS * winPos = (WINDOWPOS *)lParam;
632 DEFWND_HandleWindowPosChanged( wndPtr, winPos->flags );
637 if (wParam && wndPtr->text)
639 lstrcpynWtoA( (LPSTR)lParam, wndPtr->text, wParam );
640 result = (LRESULT)strlen( (LPSTR)lParam );
645 DEFWND_SetTextA( wndPtr, (LPCSTR)lParam );
646 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
650 result = DEFWND_DefWinProc( wndPtr, msg, wParam, lParam );
654 WIN_ReleaseWndPtr(wndPtr);
655 SPY_ExitMessage( SPY_RESULT_DEFWND, hwnd, msg, result );
660 /***********************************************************************
661 * DefWindowProcW [USER32.127] Calls default window message handler
663 * Calls default window procedure for messages not processed
667 * Return value is dependent upon the message.
669 LRESULT WINAPI DefWindowProcW(
670 HWND hwnd, /* [in] window procedure recieving message */
671 UINT msg, /* [in] message identifier */
672 WPARAM wParam, /* [in] first message parameter */
673 LPARAM lParam ) /* [in] second message parameter */
675 WND * wndPtr = WIN_FindWndPtr( hwnd );
678 if (!wndPtr) return 0;
683 CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
684 if (cs->lpszName) DEFWND_SetTextW( wndPtr, cs->lpszName );
690 if (wParam && wndPtr->text)
692 lstrcpynW( (LPWSTR)lParam, wndPtr->text, wParam );
693 result = strlenW( (LPWSTR)lParam );
698 DEFWND_SetTextW( wndPtr, (LPCWSTR)lParam );
699 NC_HandleNCPaint( hwnd , (HRGN)1 ); /* Repaint caption */
703 result = DefWindowProcA( hwnd, msg, wParam, lParam );
706 WIN_ReleaseWndPtr(wndPtr);