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 if (wndPtr
->dwStyle
& WS_CHILD
)
337 return SendMessageA( wndPtr
->parent
->hwndSelf
,
338 WM_MOUSEWHEEL
, wParam
, lParam
);
343 case WM_ICONERASEBKGND
:
347 if (!wndPtr
->class->hbrBackground
) return 0;
349 /* Since WM_ERASEBKGND may receive either a window dc or a */
350 /* client dc, the area to be erased has to be retrieved from */
351 /* the device context. */
352 GetClipBox( (HDC
)wParam
, &rect
);
354 /* Always call the Win32 variant of FillRect even on Win16,
355 * since despite the fact that Win16, as well as Win32,
356 * supports special background brushes for a window class,
357 * the Win16 variant of FillRect does not.
359 FillRect( (HDC
) wParam
, &rect
, wndPtr
->class->hbrBackground
);
366 case WM_CTLCOLORMSGBOX
:
367 case WM_CTLCOLOREDIT
:
368 case WM_CTLCOLORLISTBOX
:
371 case WM_CTLCOLORSTATIC
:
372 case WM_CTLCOLORSCROLLBAR
:
373 return (LRESULT
)DEFWND_ControlColor( (HDC
)wParam
, msg
- WM_CTLCOLORMSGBOX
);
376 return (LRESULT
)DEFWND_ControlColor( (HDC
)wParam
, HIWORD(lParam
) );
378 case WM_GETTEXTLENGTH
:
379 if (wndPtr
->text
) return (LRESULT
)strlenW(wndPtr
->text
);
383 if (wndPtr
->dwStyle
& WS_CHILD
)
384 if (SendMessage16(wndPtr
->parent
->hwndSelf
, WM_SETCURSOR
,
387 return NC_HandleSetCursor( wndPtr
->hwndSelf
, wParam
, lParam
);
390 return NC_HandleSysCommand( wndPtr
->hwndSelf
, wParam
,
391 MAKEPOINT16(lParam
) );
394 if(wParam
== VK_F10
) iF10Key
= VK_F10
;
398 if( HIWORD(lParam
) & KEYDATA_ALT
)
400 /* if( HIWORD(lParam) & ~KEYDATA_PREVSTATE ) */
401 if( wParam
== VK_MENU
&& !iMenuSysKey
)
408 if( wParam
== VK_F4
) /* try to close the window */
410 HWND hWnd
= WIN_GetTopParent( wndPtr
->hwndSelf
);
411 wndPtr
= WIN_FindWndPtr( hWnd
);
412 if( wndPtr
&& !(wndPtr
->class->style
& CS_NOCLOSE
) )
413 PostMessage16( hWnd
, WM_SYSCOMMAND
, SC_CLOSE
, 0 );
414 WIN_ReleaseWndPtr(wndPtr
);
417 else if( wParam
== VK_F10
)
420 if( wParam
== VK_ESCAPE
&& (GetKeyState(VK_SHIFT
) & 0x8000))
421 SendMessage16( wndPtr
->hwndSelf
, WM_SYSCOMMAND
,
422 (WPARAM16
)SC_KEYMENU
, (LPARAM
)VK_SPACE
);
427 /* Press and release F10 or ALT */
428 if (((wParam
== VK_MENU
) && iMenuSysKey
) ||
429 ((wParam
== VK_F10
) && iF10Key
))
430 SendMessage16( WIN_GetTopParent(wndPtr
->hwndSelf
),
431 WM_SYSCOMMAND
, SC_KEYMENU
, 0L );
432 iMenuSysKey
= iF10Key
= 0;
437 if (wParam
== VK_RETURN
&& (wndPtr
->dwStyle
& WS_MINIMIZE
))
439 PostMessage16( wndPtr
->hwndSelf
, WM_SYSCOMMAND
,
440 (WPARAM16
)SC_RESTORE
, 0L );
443 if ((HIWORD(lParam
) & KEYDATA_ALT
) && wParam
)
445 if (wParam
== VK_TAB
|| wParam
== VK_ESCAPE
) break;
446 if (wParam
== VK_SPACE
&& (wndPtr
->dwStyle
& WS_CHILD
))
447 SendMessage16( wndPtr
->parent
->hwndSelf
, msg
, wParam
, lParam
);
449 SendMessage16( wndPtr
->hwndSelf
, WM_SYSCOMMAND
,
450 (WPARAM16
)SC_KEYMENU
, (LPARAM
)(DWORD
)wParam
);
452 else /* check for Ctrl-Esc */
453 if (wParam
!= VK_ESCAPE
) MessageBeep(0);
457 if (!lParam
) return 0; /* sent from ShowWindow */
458 if (!(wndPtr
->dwStyle
& WS_POPUP
) || !wndPtr
->owner
) return 0;
459 if ((wndPtr
->dwStyle
& WS_VISIBLE
) && wParam
) return 0;
460 else if (!(wndPtr
->dwStyle
& WS_VISIBLE
) && !wParam
) return 0;
461 ShowWindow( wndPtr
->hwndSelf
, wParam
? SW_SHOWNOACTIVATE
: SW_HIDE
);
465 if (wndPtr
->parent
== WIN_GetDesktop()) EndMenu();
466 if (GetCapture() == wndPtr
->hwndSelf
) ReleaseCapture();
467 WIN_ReleaseDesktop();
477 case WM_QUERYDROPOBJECT
:
478 if (wndPtr
->dwExStyle
& WS_EX_ACCEPTFILES
) return 1;
481 case WM_QUERYDRAGICON
:
486 if( (hIcon
=wndPtr
->class->hCursor
) ) return (LRESULT
)hIcon
;
487 for(len
=1; len
<64; len
++)
488 if((hIcon
=LoadIcon16(wndPtr
->hInstance
,MAKEINTRESOURCE16(len
))))
489 return (LRESULT
)hIcon
;
490 return (LRESULT
)LoadIcon16(0,IDI_APPLICATION16
);
494 case WM_ISACTIVEICON
:
495 return ((wndPtr
->flags
& WIN_NCACTIVATED
) != 0);
497 case WM_NOTIFYFORMAT
:
498 if (IsWindowUnicode(wndPtr
->hwndSelf
)) return NFR_UNICODE
;
499 else return NFR_ANSI
;
502 case WM_QUERYENDSESSION
:
507 int index
= (wParam
!= ICON_SMALL
) ? GCL_HICON
: GCL_HICONSM
;
508 HICON16 hOldIcon
= GetClassLongA(wndPtr
->hwndSelf
, index
);
509 SetClassLongA(wndPtr
->hwndSelf
, index
, lParam
);
511 SetWindowPos(wndPtr
->hwndSelf
, 0, 0, 0, 0, 0, SWP_FRAMECHANGED
512 | SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
515 if( wndPtr
->flags
& WIN_NATIVE
)
516 wndPtr
->pDriver
->pSetHostAttr(wndPtr
, HAK_ICONS
, 0);
523 int index
= (wParam
!= ICON_SMALL
) ? GCL_HICON
: GCL_HICONSM
;
524 return GetClassLongA(wndPtr
->hwndSelf
, index
);
528 SendMessageA( wndPtr
->parent
->hwndSelf
, msg
, wParam
, lParam
);
537 /***********************************************************************
538 * DefWindowProc16 (USER.107)
540 LRESULT WINAPI
DefWindowProc16( HWND16 hwnd
, UINT16 msg
, WPARAM16 wParam
,
543 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
546 if (!wndPtr
) return 0;
547 SPY_EnterMessage( SPY_DEFWNDPROC16
, hwnd
, msg
, wParam
, lParam
);
553 CREATESTRUCT16
*cs
= (CREATESTRUCT16
*)PTR_SEG_TO_LIN(lParam
);
555 DEFWND_SetTextA( wndPtr
, (LPCSTR
)PTR_SEG_TO_LIN(cs
->lpszName
) );
563 CONV_RECT16TO32( (RECT16
*)PTR_SEG_TO_LIN(lParam
), &rect32
);
564 result
= NC_HandleNCCalcSize( wndPtr
, &rect32
);
565 CONV_RECT32TO16( &rect32
, (RECT16
*)PTR_SEG_TO_LIN(lParam
) );
569 case WM_WINDOWPOSCHANGING
:
570 result
= WINPOS_HandleWindowPosChanging16( wndPtr
,
571 (WINDOWPOS16
*)PTR_SEG_TO_LIN(lParam
) );
574 case WM_WINDOWPOSCHANGED
:
576 WINDOWPOS16
* winPos
= (WINDOWPOS16
*)PTR_SEG_TO_LIN(lParam
);
577 DEFWND_HandleWindowPosChanged( wndPtr
, winPos
->flags
);
582 if (wParam
&& wndPtr
->text
)
584 lstrcpynWtoA( (LPSTR
)PTR_SEG_TO_LIN(lParam
), wndPtr
->text
, wParam
);
585 result
= (LRESULT
)strlen( (LPSTR
)PTR_SEG_TO_LIN(lParam
) );
590 DEFWND_SetTextA( wndPtr
, (LPCSTR
)PTR_SEG_TO_LIN(lParam
) );
591 if( wndPtr
->dwStyle
& WS_CAPTION
) NC_HandleNCPaint( hwnd
, (HRGN
)1 );
595 result
= DEFWND_DefWinProc( wndPtr
, msg
, wParam
, lParam
);
599 WIN_ReleaseWndPtr(wndPtr
);
600 SPY_ExitMessage( SPY_RESULT_DEFWND16
, hwnd
, msg
, result
);
605 /***********************************************************************
606 * DefWindowProcA [USER32.126]
609 LRESULT WINAPI
DefWindowProcA( HWND hwnd
, UINT msg
, WPARAM wParam
,
612 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
615 if (!wndPtr
) return 0;
616 SPY_EnterMessage( SPY_DEFWNDPROC
, hwnd
, msg
, wParam
, lParam
);
622 CREATESTRUCTA
*cs
= (CREATESTRUCTA
*)lParam
;
623 if (cs
->lpszName
) DEFWND_SetTextA( wndPtr
, cs
->lpszName
);
629 result
= NC_HandleNCCalcSize( wndPtr
, (RECT
*)lParam
);
632 case WM_WINDOWPOSCHANGING
:
633 result
= WINPOS_HandleWindowPosChanging( wndPtr
,
634 (WINDOWPOS
*)lParam
);
637 case WM_WINDOWPOSCHANGED
:
639 WINDOWPOS
* winPos
= (WINDOWPOS
*)lParam
;
640 DEFWND_HandleWindowPosChanged( wndPtr
, winPos
->flags
);
645 if (wParam
&& wndPtr
->text
)
647 lstrcpynWtoA( (LPSTR
)lParam
, wndPtr
->text
, wParam
);
648 result
= (LRESULT
)strlen( (LPSTR
)lParam
);
653 DEFWND_SetTextA( wndPtr
, (LPCSTR
)lParam
);
654 NC_HandleNCPaint( hwnd
, (HRGN
)1 ); /* Repaint caption */
658 result
= DEFWND_DefWinProc( wndPtr
, msg
, wParam
, lParam
);
662 WIN_ReleaseWndPtr(wndPtr
);
663 SPY_ExitMessage( SPY_RESULT_DEFWND
, hwnd
, msg
, result
);
668 /***********************************************************************
669 * DefWindowProcW [USER32.127] Calls default window message handler
671 * Calls default window procedure for messages not processed
675 * Return value is dependent upon the message.
677 LRESULT WINAPI
DefWindowProcW(
678 HWND hwnd
, /* [in] window procedure recieving message */
679 UINT msg
, /* [in] message identifier */
680 WPARAM wParam
, /* [in] first message parameter */
681 LPARAM lParam
) /* [in] second message parameter */
683 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
686 if (!wndPtr
) return 0;
691 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
692 if (cs
->lpszName
) DEFWND_SetTextW( wndPtr
, cs
->lpszName
);
698 if (wParam
&& wndPtr
->text
)
700 lstrcpynW( (LPWSTR
)lParam
, wndPtr
->text
, wParam
);
701 result
= strlenW( (LPWSTR
)lParam
);
706 DEFWND_SetTextW( wndPtr
, (LPCWSTR
)lParam
);
707 NC_HandleNCPaint( hwnd
, (HRGN
)1 ); /* Repaint caption */
711 result
= DefWindowProcA( hwnd
, msg
, wParam
, lParam
);
714 WIN_ReleaseWndPtr(wndPtr
);