[Win32k]
[reactos.git] / reactos / win32ss / user / ntuser / defwnd.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Miscellaneous User functions
5 * FILE: win32ss/user/ntuser/defwnd.c
6 * PROGRAMER:
7 */
8
9 #include <win32k.h>
10 #include <windowsx.h>
11
12 DBG_DEFAULT_CHANNEL(UserDefwnd);
13
14 INT WINAPI DrawTextExWorker( HDC hdc, LPWSTR str, INT i_count,
15 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp );
16
17 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
18 {
19 DRAWTEXTPARAMS dtp;
20
21 memset (&dtp, 0, sizeof(dtp));
22 dtp.cbSize = sizeof(dtp);
23 if (flags & DT_TABSTOP)
24 {
25 dtp.iTabLength = (flags >> 8) & 0xff;
26 flags &= 0xffff00ff;
27 }
28 return DrawTextExWorker(hdc, (LPWSTR)str, count, rect, flags, &dtp);
29 }
30
31
32 HBRUSH FASTCALL
33 DefWndControlColor(HDC hDC, UINT ctlType)
34 {
35 if (ctlType == CTLCOLOR_SCROLLBAR)
36 {
37 HBRUSH hb = IntGetSysColorBrush(COLOR_SCROLLBAR);
38 COLORREF bk = IntGetSysColor(COLOR_3DHILIGHT);
39 IntGdiSetTextColor(hDC, IntGetSysColor(COLOR_3DFACE));
40 IntGdiSetBkColor(hDC, bk);
41
42 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
43 * we better use 0x55aa bitmap brush to make scrollbar's background
44 * look different from the window background.
45 */
46 if ( bk == IntGetSysColor(COLOR_WINDOW))
47 return gpsi->hbrGray;
48
49 NtGdiUnrealizeObject( hb );
50 return hb;
51 }
52
53 IntGdiSetTextColor(hDC, IntGetSysColor(COLOR_WINDOWTEXT));
54
55 if ((ctlType == CTLCOLOR_EDIT) || (ctlType == CTLCOLOR_LISTBOX))
56 {
57 IntGdiSetBkColor(hDC, IntGetSysColor(COLOR_WINDOW));
58 }
59 else
60 {
61 IntGdiSetBkColor(hDC, IntGetSysColor(COLOR_3DFACE));
62 return IntGetSysColorBrush(COLOR_3DFACE);
63 }
64
65 return IntGetSysColorBrush(COLOR_WINDOW);
66 }
67
68 LRESULT FASTCALL
69 DefWndHandleWindowPosChanging(PWND pWnd, WINDOWPOS* Pos)
70 {
71 POINT maxTrack, minTrack;
72 LONG style = pWnd->style;
73
74 if (Pos->flags & SWP_NOSIZE) return 0;
75 if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
76 {
77 co_WinPosGetMinMaxInfo(pWnd, NULL, NULL, &minTrack, &maxTrack);
78 Pos->cx = min(Pos->cx, maxTrack.x);
79 Pos->cy = min(Pos->cy, maxTrack.y);
80 if (!(style & WS_MINIMIZE))
81 {
82 if (Pos->cx < minTrack.x) Pos->cx = minTrack.x;
83 if (Pos->cy < minTrack.y) Pos->cy = minTrack.y;
84 }
85 }
86 else
87 {
88 Pos->cx = max(Pos->cx, 0);
89 Pos->cy = max(Pos->cy, 0);
90 }
91 return 0;
92 }
93
94 LRESULT FASTCALL
95 DefWndHandleWindowPosChanged(PWND pWnd, WINDOWPOS* Pos)
96 {
97 RECT Rect;
98 LONG style = pWnd->style;
99
100 IntGetClientRect(pWnd, &Rect);
101 IntMapWindowPoints(pWnd, (style & WS_CHILD ? IntGetParent(pWnd) : NULL), (LPPOINT) &Rect, 2);
102
103 if (!(Pos->flags & SWP_NOCLIENTMOVE))
104 {
105 co_IntSendMessage(UserHMGetHandle(pWnd), WM_MOVE, 0, MAKELONG(Rect.left, Rect.top));
106 }
107
108 if (!(Pos->flags & SWP_NOCLIENTSIZE) || (Pos->flags & SWP_STATECHANGED))
109 {
110 if (style & WS_MINIMIZE) co_IntSendMessage(UserHMGetHandle(pWnd), WM_SIZE, SIZE_MINIMIZED, 0 );
111 else
112 {
113 WPARAM wp = (style & WS_MAXIMIZE) ? SIZE_MAXIMIZED : SIZE_RESTORED;
114 co_IntSendMessage(UserHMGetHandle(pWnd), WM_SIZE, wp, MAKELONG(Rect.right - Rect.left, Rect.bottom - Rect.top));
115 }
116 }
117 return 0;
118 }
119
120 //
121 // Handle a WM_SYSCOMMAND message. Called from DefWindowProc().
122 //
123 LRESULT FASTCALL
124 DefWndHandleSysCommand(PWND pWnd, WPARAM wParam, LPARAM lParam)
125 {
126 LRESULT lResult = 0;
127 BOOL Hook = FALSE;
128
129 if (ISITHOOKED(WH_CBT) || (pWnd->head.rpdesk->pDeskInfo->fsHooks & HOOKID_TO_FLAG(WH_CBT)))
130 {
131 Hook = TRUE;
132 lResult = co_HOOK_CallHooks(WH_CBT, HCBT_SYSCOMMAND, wParam, lParam);
133
134 if (lResult) return lResult;
135 }
136
137 switch (wParam & 0xfff0)
138 {
139 case SC_MOVE:
140 case SC_SIZE:
141 DefWndDoSizeMove(pWnd, wParam);
142 break;
143
144 case SC_MINIMIZE:
145 if (UserHMGetHandle(pWnd) == UserGetActiveWindow())
146 IntShowOwnedPopups(pWnd,FALSE); // This is done in ShowWindow! Need to retest!
147 co_WinPosShowWindow( pWnd, SW_MINIMIZE );
148 break;
149
150 case SC_MAXIMIZE:
151 if (((pWnd->style & WS_MINIMIZE) != 0) && UserHMGetHandle(pWnd) == UserGetActiveWindow())
152 IntShowOwnedPopups(pWnd,TRUE);
153 co_WinPosShowWindow( pWnd, SW_MAXIMIZE );
154 break;
155
156 case SC_RESTORE:
157 if (((pWnd->style & WS_MINIMIZE) != 0) && UserHMGetHandle(pWnd) == UserGetActiveWindow())
158 IntShowOwnedPopups(pWnd,TRUE);
159 co_WinPosShowWindow( pWnd, SW_RESTORE );
160 break;
161
162 case SC_CLOSE:
163 return co_IntSendMessage(UserHMGetHandle(pWnd), WM_CLOSE, 0, 0);
164
165 case SC_SCREENSAVE:
166 ERR("Screensaver Called!\n");
167 UserPostMessage(hwndSAS, WM_LOGONNOTIFY, LN_START_SCREENSAVE, 0); // always lParam 0 == not Secure
168 break;
169
170 case SC_HOTKEY:
171 {
172 USER_REFERENCE_ENTRY Ref;
173
174 pWnd = ValidateHwndNoErr((HWND)lParam);
175 if (pWnd)
176 {
177 if (pWnd->spwndLastActive)
178 {
179 pWnd = pWnd->spwndLastActive;
180 }
181 UserRefObjectCo(pWnd, &Ref);
182 co_IntSetForegroundWindow(pWnd);
183 UserDerefObjectCo(pWnd);
184 if (pWnd->style & WS_MINIMIZE)
185 {
186 UserPostMessage(UserHMGetHandle(pWnd), WM_SYSCOMMAND, SC_RESTORE, 0);
187 }
188 }
189 }
190 break;
191 // case SC_DEFAULT:
192 case SC_MOUSEMENU:
193 {
194 POINT Pt;
195 Pt.x = (short)LOWORD(lParam);
196 Pt.y = (short)HIWORD(lParam);
197 MENU_TrackMouseMenuBar(pWnd, wParam & 0x000f, Pt);
198 }
199 break;
200
201 case SC_KEYMENU:
202 MENU_TrackKbdMenuBar(pWnd, wParam, (WCHAR)lParam);
203 break;
204
205
206 default:
207 // We do not support anything else here so we should return normal even when sending a hook.
208 return 0;
209 }
210
211 return(Hook ? 1 : 0); // Don't call us again from user space.
212 }
213
214 PWND FASTCALL
215 co_IntFindChildWindowToOwner(PWND Root, PWND Owner)
216 {
217 PWND Ret;
218 PWND Child, OwnerWnd;
219
220 for(Child = Root->spwndChild; Child; Child = Child->spwndNext)
221 {
222 OwnerWnd = Child->spwndOwner;
223 if(!OwnerWnd)
224 continue;
225
226 if (!(Child->style & WS_POPUP) ||
227 !(Child->style & WS_VISIBLE) ||
228 /* Fixes CMD pop up properties window from having foreground. */
229 Owner->head.pti->MessageQueue != Child->head.pti->MessageQueue)
230 continue;
231
232 if(OwnerWnd == Owner)
233 {
234 Ret = Child;
235 return Ret;
236 }
237 }
238 return NULL;
239 }
240
241 LRESULT
242 DefWndHandleSetCursor(PWND pWnd, WPARAM wParam, LPARAM lParam)
243 {
244 PWND pwndPopUP = NULL;
245 WORD Msg = HIWORD(lParam);
246
247 /* Not for child windows. */
248 if (UserHMGetHandle(pWnd) != (HWND)wParam)
249 {
250 return FALSE;
251 }
252
253 switch((short)LOWORD(lParam))
254 {
255 case HTERROR:
256 {
257 //// This is the real fix for CORE-6129! This was a "Code hole".
258 USER_REFERENCE_ENTRY Ref;
259
260 if (Msg == WM_LBUTTONDOWN)
261 {
262 // Find a pop up window to bring active.
263 pwndPopUP = co_IntFindChildWindowToOwner(UserGetDesktopWindow(), pWnd);
264 if (pwndPopUP)
265 {
266 // Not a child pop up from desktop.
267 if ( pwndPopUP != UserGetDesktopWindow()->spwndChild )
268 {
269 // Get original active window.
270 PWND pwndOrigActive = gpqForeground->spwndActive;
271
272 co_WinPosSetWindowPos(pWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
273
274 UserRefObjectCo(pwndPopUP, &Ref);
275 //UserSetActiveWindow(pwndPopUP);
276 co_IntSetForegroundWindow(pwndPopUP); // HACK
277 UserDerefObjectCo(pwndPopUP);
278
279 // If the change was made, break out.
280 if (pwndOrigActive != gpqForeground->spwndActive)
281 break;
282 }
283 }
284 }
285 ////
286 if (Msg == WM_LBUTTONDOWN || Msg == WM_MBUTTONDOWN ||
287 Msg == WM_RBUTTONDOWN || Msg == WM_XBUTTONDOWN)
288 {
289 if (pwndPopUP)
290 {
291 FLASHWINFO fwi =
292 {sizeof(FLASHWINFO),
293 UserHMGetHandle(pwndPopUP),
294 FLASHW_ALL,
295 gspv.dwForegroundFlashCount,
296 (gpsi->dtCaretBlink >> 3)};
297
298 // Now shake that window!
299 IntFlashWindowEx(pwndPopUP, &fwi);
300 }
301 UserPostMessage(hwndSAS, WM_LOGONNOTIFY, LN_MESSAGE_BEEP, 0);
302 }
303 break;
304 }
305
306 case HTCLIENT:
307 {
308 if (pWnd->pcls->spcur)
309 {
310 UserSetCursor(pWnd->pcls->spcur, FALSE);
311 }
312 return FALSE;
313 }
314
315 case HTLEFT:
316 case HTRIGHT:
317 {
318 if (pWnd->style & WS_MAXIMIZE)
319 {
320 break;
321 }
322 UserSetCursor(SYSTEMCUR(SIZEWE), FALSE);
323 return TRUE;
324 }
325
326 case HTTOP:
327 case HTBOTTOM:
328 {
329 if (pWnd->style & WS_MAXIMIZE)
330 {
331 break;
332 }
333 UserSetCursor(SYSTEMCUR(SIZENS), FALSE);
334 return TRUE;
335 }
336
337 case HTTOPLEFT:
338 case HTBOTTOMRIGHT:
339 {
340 if (pWnd->style & WS_MAXIMIZE)
341 {
342 break;
343 }
344 UserSetCursor(SYSTEMCUR(SIZENWSE), FALSE);
345 return TRUE;
346 }
347
348 case HTBOTTOMLEFT:
349 case HTTOPRIGHT:
350 {
351 if (pWnd->style & WS_MAXIMIZE)
352 {
353 break;
354 }
355 UserSetCursor(SYSTEMCUR(SIZENESW), FALSE);
356 return TRUE;
357 }
358 }
359 UserSetCursor(SYSTEMCUR(ARROW), FALSE);
360 return FALSE;
361 }
362
363 VOID FASTCALL DefWndPrint( PWND pwnd, HDC hdc, ULONG uFlags)
364 {
365 /*
366 * Visibility flag.
367 */
368 if ( (uFlags & PRF_CHECKVISIBLE) &&
369 !IntIsWindowVisible(pwnd) )
370 return;
371
372 /*
373 * Unimplemented flags.
374 */
375 if ( (uFlags & PRF_CHILDREN) ||
376 (uFlags & PRF_OWNED) ||
377 (uFlags & PRF_NONCLIENT) )
378 {
379 FIXME("WM_PRINT message with unsupported flags\n");
380 }
381
382 /*
383 * Background
384 */
385 if ( uFlags & PRF_ERASEBKGND)
386 co_IntSendMessage(UserHMGetHandle(pwnd), WM_ERASEBKGND, (WPARAM)hdc, 0);
387
388 /*
389 * Client area
390 */
391 if ( uFlags & PRF_CLIENT)
392 co_IntSendMessage(UserHMGetHandle(pwnd), WM_PRINTCLIENT, (WPARAM)hdc, uFlags);
393 }
394
395 BOOL
396 UserPaintCaption(PWND pWnd, INT Flags)
397 {
398 BOOL Ret = FALSE;
399
400 if ( pWnd->style & WS_VISIBLE && (pWnd->style & WS_CAPTION) == WS_CAPTION )
401 {
402
403 if (pWnd->state & WNDS_HASCAPTION && pWnd->head.pti->MessageQueue == gpqForeground)
404 Flags |= DC_ACTIVE;
405 /*
406 * When themes are not enabled we can go on and paint the non client area.
407 * However if we do that with themes enabled we will draw a classic frame.
408 * This is sovled by sending a themes specific message to notify the themes
409 * engine that the caption needs to be redrawn
410 */
411 if (gpsi->dwSRVIFlags & SRVINFO_APIHOOK)
412 {
413 /*
414 * This will cause uxtheme to either paint the themed caption or call
415 * RealUserDrawCaption in order to draw the classic caption when themes
416 * are disabled but the themes service is enabled
417 */
418 TRACE("UDCB Flags %08x\n");
419 co_IntSendMessage(UserHMGetHandle(pWnd), WM_NCUAHDRAWCAPTION, Flags, 0);
420 }
421 else
422 {
423 HDC hDC = UserGetDCEx(pWnd, NULL, DCX_WINDOW|DCX_USESTYLE);
424 UserDrawCaptionBar(pWnd, hDC, Flags);
425 UserReleaseDC(pWnd, hDC, FALSE);
426 }
427 Ret = TRUE;
428 }
429 // Support window tray
430 return Ret;
431 }
432
433 // WM_SETICON
434 LRESULT FASTCALL
435 DefWndSetIcon(PWND pWnd, WPARAM wParam, LPARAM lParam)
436 {
437 HICON hIcon, hIconSmall, hIconOld;
438
439 if ( wParam > ICON_SMALL2 )
440 {
441 EngSetLastError(ERROR_INVALID_PARAMETER);
442 return 0;
443 }
444 hIconSmall = UserGetProp(pWnd, gpsi->atomIconSmProp);
445 hIcon = UserGetProp(pWnd, gpsi->atomIconProp);
446
447 hIconOld = wParam == ICON_BIG ? hIcon : hIconSmall;
448
449 switch(wParam)
450 {
451 case ICON_BIG:
452 hIcon = (HICON)lParam;
453 break;
454 case ICON_SMALL:
455 hIconSmall = (HICON)lParam;
456 break;
457 case ICON_SMALL2:
458 ERR("FIXME: Set ICON_SMALL2 support!\n");
459 default:
460 break;
461 }
462
463 IntSetProp(pWnd, gpsi->atomIconProp, hIcon);
464 IntSetProp(pWnd, gpsi->atomIconSmProp, hIconSmall);
465
466 if ((pWnd->style & WS_CAPTION ) == WS_CAPTION)
467 UserPaintCaption(pWnd, DC_ICON);
468
469 return (LRESULT)hIconOld;
470 }
471
472 LRESULT FASTCALL
473 DefWndGetIcon(PWND pWnd, WPARAM wParam, LPARAM lParam)
474 {
475 HICON hIconRet;
476 if ( wParam > ICON_SMALL2 )
477 {
478 EngSetLastError(ERROR_INVALID_PARAMETER);
479 return 0;
480 }
481 switch(wParam)
482 {
483 case ICON_BIG:
484 hIconRet = UserGetProp(pWnd, gpsi->atomIconProp);
485 break;
486 case ICON_SMALL:
487 case ICON_SMALL2:
488 hIconRet = UserGetProp(pWnd, gpsi->atomIconSmProp);
489 break;
490 default:
491 break;
492 }
493 return (LRESULT)hIconRet;
494 }
495
496 VOID FASTCALL
497 DefWndScreenshot(PWND pWnd)
498 {
499 RECT rect;
500 HDC hdc;
501 INT w;
502 INT h;
503 HBITMAP hbitmap;
504 HDC hdc2;
505 SETCLIPBDATA scd = {FALSE, FALSE};
506
507 UserOpenClipboard(UserHMGetHandle(pWnd));
508 UserEmptyClipboard();
509
510 hdc = UserGetWindowDC(pWnd);
511 IntGetWindowRect(pWnd, &rect);
512 w = rect.right - rect.left;
513 h = rect.bottom - rect.top;
514
515 hbitmap = NtGdiCreateCompatibleBitmap(hdc, w, h);
516 hdc2 = NtGdiCreateCompatibleDC(hdc);
517 NtGdiSelectBitmap(hdc2, hbitmap);
518
519 NtGdiBitBlt(hdc2, 0, 0, w, h, hdc, 0, 0, SRCCOPY, 0, 0);
520
521 UserSetClipboardData(CF_BITMAP, hbitmap, &scd);
522
523 UserReleaseDC(pWnd, hdc, FALSE);
524 UserReleaseDC(pWnd, hdc2, FALSE);
525
526 UserCloseClipboard();
527 }
528
529 /*
530 Win32k counterpart of User DefWindowProc
531 */
532 LRESULT FASTCALL
533 IntDefWindowProc(
534 PWND Wnd,
535 UINT Msg,
536 WPARAM wParam,
537 LPARAM lParam,
538 BOOL Ansi)
539 {
540 PTHREADINFO pti = PsGetCurrentThreadWin32Thread();
541 LRESULT lResult = 0;
542 USER_REFERENCE_ENTRY Ref;
543
544 if (Msg > WM_USER) return 0;
545
546 switch (Msg)
547 {
548 case WM_GETTEXTLENGTH:
549 {
550 PWSTR buf;
551 ULONG len;
552
553 if (Wnd != NULL && Wnd->strName.Length != 0)
554 {
555 buf = Wnd->strName.Buffer;
556 if (buf != NULL &&
557 NT_SUCCESS(RtlUnicodeToMultiByteSize(&len,
558 buf,
559 Wnd->strName.Length)))
560 {
561 lResult = (LRESULT) (Wnd->strName.Length / sizeof(WCHAR));
562 }
563 }
564 else lResult = 0L;
565
566 break;
567 }
568
569 case WM_GETTEXT: // FIXME: Handle Ansi
570 {
571 PWSTR buf = NULL;
572 PWSTR outbuf = (PWSTR)lParam;
573
574 if (Wnd != NULL && wParam != 0)
575 {
576 if (Wnd->strName.Buffer != NULL)
577 buf = Wnd->strName.Buffer;
578 else
579 outbuf[0] = L'\0';
580
581 if (buf != NULL)
582 {
583 if (Wnd->strName.Length != 0)
584 {
585 lResult = min(Wnd->strName.Length / sizeof(WCHAR), wParam - 1);
586 RtlCopyMemory(outbuf,
587 buf,
588 lResult * sizeof(WCHAR));
589 outbuf[lResult] = L'\0';
590 }
591 else
592 outbuf[0] = L'\0';
593 }
594 }
595 break;
596 }
597
598 case WM_SETTEXT: // FIXME: Handle Ansi
599 {
600 DefSetText(Wnd, (PCWSTR)lParam);
601
602 if ((Wnd->style & WS_CAPTION) == WS_CAPTION)
603 UserPaintCaption(Wnd, DC_TEXT);
604 IntNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, Wnd, OBJID_WINDOW, CHILDID_SELF, 0);
605 lResult = 1;
606 break;
607 }
608
609 case WM_SYSCOMMAND:
610 {
611 TRACE("hwnd %p WM_SYSCOMMAND %lx %lx\n", Wnd->head.h, wParam, lParam );
612 lResult = DefWndHandleSysCommand(Wnd, wParam, lParam);
613 break;
614 }
615
616 case WM_SHOWWINDOW:
617 {
618 if ((Wnd->style & WS_VISIBLE) && wParam) break;
619 if (!(Wnd->style & WS_VISIBLE) && !wParam) break;
620 if (!Wnd->spwndOwner) break;
621 if (LOWORD(lParam))
622 {
623 if (wParam)
624 {
625 if (!(Wnd->state & WNDS_HIDDENPOPUP)) break;
626 Wnd->state &= ~WNDS_HIDDENPOPUP;
627 }
628 else
629 Wnd->state |= WNDS_HIDDENPOPUP;
630
631 co_WinPosShowWindow(Wnd, wParam ? SW_SHOWNOACTIVATE : SW_HIDE);
632 }
633 }
634 break;
635
636 case WM_CLIENTSHUTDOWN:
637 return IntClientShutdown(Wnd, wParam, lParam);
638
639 case WM_APPCOMMAND:
640 if ( (Wnd->style & (WS_POPUP|WS_CHILD)) != WS_CHILD &&
641 Wnd != co_GetDesktopWindow(Wnd) )
642 {
643 if (!co_HOOK_CallHooks(WH_SHELL, HSHELL_APPCOMMAND, wParam, lParam))
644 co_IntShellHookNotify(HSHELL_APPCOMMAND, wParam, lParam);
645 break;
646 }
647 UserRefObjectCo(Wnd->spwndParent, &Ref);
648 lResult = co_IntSendMessage(UserHMGetHandle(Wnd->spwndParent), WM_APPCOMMAND, wParam, lParam);
649 UserDerefObjectCo(Wnd->spwndParent);
650 break;
651
652 case WM_KEYF1:
653 {
654 HELPINFO hi;
655 HMENU hMenu = UlongToHandle(Wnd->IDMenu);
656 PWND pwndActive = MENU_IsMenuActive();
657 hi.cbSize = sizeof(HELPINFO);
658 hi.MousePos = gpsi->ptCursor;
659 hi.iContextType = HELPINFO_MENUITEM;
660 hi.hItemHandle = pwndActive ? UserHMGetHandle(pwndActive) : UserHMGetHandle(Wnd);
661 hi.iCtrlId = (Wnd->style & (WS_POPUP|WS_CHILD)) == WS_CHILD ? IntMenuItemFromPoint(Wnd, hMenu, hi.MousePos) : 0;
662 hi.dwContextId = IntGetWindowContextHelpId(Wnd);
663
664 co_IntSendMessage( UserHMGetHandle(Wnd), WM_HELP, 0, (LPARAM)&hi );
665 break;
666 }
667
668 case WM_SETICON:
669 {
670 return DefWndSetIcon(Wnd, wParam, lParam);
671 }
672
673 case WM_GETICON:
674 {
675 return DefWndGetIcon(Wnd, wParam, lParam);
676 }
677
678 case WM_HELP:
679 {
680 PWND Parent = IntGetParent(Wnd);
681 co_IntSendMessage(UserHMGetHandle(Parent), Msg, wParam, lParam);
682 break;
683 }
684
685 case WM_LBUTTONDOWN:
686 case WM_RBUTTONDOWN:
687 case WM_MBUTTONDOWN:
688 pti->MessageQueue->QF_flags &= ~(QF_FMENUSTATUS|QF_FMENUSTATUSBREAK);
689 break;
690
691 case WM_NCLBUTTONDOWN:
692 return (NC_HandleNCLButtonDown( Wnd, wParam, lParam));
693
694 case WM_LBUTTONDBLCLK:
695 return (NC_HandleNCLButtonDblClk( Wnd, HTCLIENT, lParam));
696
697 case WM_NCLBUTTONDBLCLK:
698 return (NC_HandleNCLButtonDblClk( Wnd, wParam, lParam));
699
700 case WM_NCRBUTTONDOWN:
701 return NC_HandleNCRButtonDown( Wnd, wParam, lParam );
702
703 case WM_RBUTTONUP:
704 {
705 POINT Pt;
706
707 Pt.x = GET_X_LPARAM(lParam);
708 Pt.y = GET_Y_LPARAM(lParam);
709 IntClientToScreen(Wnd, &Pt);
710 lParam = MAKELPARAM(Pt.x, Pt.y);
711 co_IntSendMessage(UserHMGetHandle(Wnd), WM_CONTEXTMENU, (WPARAM)UserHMGetHandle(Wnd), lParam);
712 break;
713 }
714
715 case WM_NCRBUTTONUP:
716 /*
717 * FIXME : we must NOT send WM_CONTEXTMENU on a WM_NCRBUTTONUP (checked
718 * in Windows), but what _should_ we do? According to MSDN :
719 * "If it is appropriate to do so, the system sends the WM_SYSCOMMAND
720 * message to the window". When is it appropriate?
721 */
722 ERR("WM_NCRBUTTONUP\n");
723 break;
724
725 case WM_CONTEXTMENU:
726 {
727 if (Wnd->style & WS_CHILD)
728 {
729 co_IntSendMessage(UserHMGetHandle(IntGetParent(Wnd)), Msg, wParam, lParam);
730 }
731 else
732 {
733 POINT Pt;
734 LONG_PTR Style;
735 LONG HitCode;
736
737 Style = Wnd->style;
738
739 Pt.x = GET_X_LPARAM(lParam);
740 Pt.y = GET_Y_LPARAM(lParam);
741 if (Style & WS_CHILD)
742 {
743 IntScreenToClient(IntGetParent(Wnd), &Pt);
744 }
745
746 HitCode = GetNCHitEx(Wnd, Pt);
747
748 if (HitCode == HTCAPTION || HitCode == HTSYSMENU)
749 {
750 PMENU SystemMenu;
751 UINT Flags;
752
753 if((SystemMenu = IntGetSystemMenu(Wnd, FALSE)))
754 {
755 MENU_InitSysMenuPopup(SystemMenu, Wnd->style, Wnd->pcls->style, HitCode);
756
757 if(HitCode == HTCAPTION)
758 Flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON;
759 else
760 Flags = TPM_LEFTBUTTON;
761
762 IntTrackPopupMenuEx(SystemMenu, Flags|TPM_SYSTEM_MENU, Pt.x, Pt.y, Wnd, NULL);
763 }
764 }
765 if (HitCode == HTHSCROLL || HitCode == HTVSCROLL)
766 {
767 WARN("Scroll Menu Not Supported\n");
768 }
769 }
770 break;
771 }
772
773 case WM_KEYDOWN:
774 if (wParam == VK_F10)
775 {
776 pti->MessageQueue->QF_flags |= QF_FF10STATUS;
777
778 if (UserGetKeyState(VK_SHIFT) & 0x8000)
779 {
780 co_IntSendMessage(UserHMGetHandle(Wnd), WM_CONTEXTMENU, (WPARAM)UserHMGetHandle(Wnd), MAKELPARAM(-1, -1));
781 }
782 }
783 break;
784
785 case WM_SYSKEYDOWN:
786 {
787 if (HIWORD(lParam) & KF_ALTDOWN)
788 { /* Previous state, if the key was down before this message,
789 this is a cheap way to ignore autorepeat keys. */
790 if ( !(HIWORD(lParam) & KF_REPEAT) )
791 {
792 if ( ( wParam == VK_MENU ||
793 wParam == VK_LMENU ||
794 wParam == VK_RMENU ) && !(pti->MessageQueue->QF_flags & QF_FMENUSTATUS)) //iMenuSysKey )
795 pti->MessageQueue->QF_flags |= QF_FMENUSTATUS; //iMenuSysKey = 1;
796 else
797 pti->MessageQueue->QF_flags &= ~QF_FMENUSTATUS; //iMenuSysKey = 0;
798 }
799
800 pti->MessageQueue->QF_flags &= ~QF_FF10STATUS; //iF10Key = 0;
801
802 if (wParam == VK_F4) /* Try to close the window */
803 {
804 PWND top = UserGetAncestor(Wnd, GA_ROOT);
805 if (!(top->style & CS_NOCLOSE))
806 UserPostMessage(UserHMGetHandle(top), WM_SYSCOMMAND, SC_CLOSE, 0);
807 }
808 else if (wParam == VK_SNAPSHOT) // Alt-VK_SNAPSHOT?
809 {
810 PWND pwnd = Wnd;
811 while (IntGetParent(pwnd) != NULL)
812 {
813 pwnd = IntGetParent(pwnd);
814 }
815 ERR("DefWndScreenshot\n");
816 DefWndScreenshot(pwnd);
817 }
818 else if ( wParam == VK_ESCAPE || wParam == VK_TAB ) // Alt-Tab/ESC Alt-Shift-Tab/ESC
819 {
820 WPARAM wParamTmp;
821 HWND Active = UserGetActiveWindow(); // Noticed MDI problem.
822 if (!Active)
823 {
824 FIXME("WM_SYSKEYDOWN VK_ESCAPE no active\n");
825 break;
826 }
827 wParamTmp = UserGetKeyState(VK_SHIFT) & 0x8000 ? SC_PREVWINDOW : SC_NEXTWINDOW;
828 co_IntSendMessage( Active, WM_SYSCOMMAND, wParamTmp, wParam );
829 }
830 }
831 else if( wParam == VK_F10 )
832 {
833 if (UserGetKeyState(VK_SHIFT) & 0x8000)
834 co_IntSendMessage( UserHMGetHandle(Wnd), WM_CONTEXTMENU, (WPARAM)UserHMGetHandle(Wnd), MAKELPARAM(-1, -1) );
835 pti->MessageQueue->QF_flags |= QF_FF10STATUS; //iF10Key = 1;
836 }
837 else if( wParam == VK_ESCAPE && (UserGetKeyState(VK_SHIFT) & 0x8000))
838 co_IntSendMessage( UserHMGetHandle(Wnd), WM_SYSCOMMAND, SC_KEYMENU, ' ' );
839 break;
840 }
841
842 case WM_KEYUP:
843 case WM_SYSKEYUP:
844 {
845 /* Press and release F10 or ALT */
846 if (((wParam == VK_MENU || wParam == VK_LMENU || wParam == VK_RMENU)
847 && (pti->MessageQueue->QF_flags & (QF_FMENUSTATUS|QF_FMENUSTATUSBREAK)) == QF_FMENUSTATUS /*iMenuSysKey*/) ||
848 ((wParam == VK_F10) && pti->MessageQueue->QF_flags & QF_FF10STATUS /*iF10Key*/))
849 co_IntSendMessage( UserHMGetHandle(UserGetAncestor( Wnd, GA_ROOT )), WM_SYSCOMMAND, SC_KEYMENU, 0L );
850 pti->MessageQueue->QF_flags &= ~(QF_FMENUSTATUS|QF_FMENUSTATUSBREAK|QF_FF10STATUS); //iMenuSysKey = iF10Key = 0;
851 break;
852 }
853
854 case WM_SYSCHAR:
855 {
856 pti->MessageQueue->QF_flags &= ~(QF_FMENUSTATUS|QF_FMENUSTATUSBREAK); //iMenuSysKey = 0;
857 if (wParam == VK_RETURN && (Wnd->style & WS_MINIMIZE) != 0)
858 {
859 UserPostMessage( UserHMGetHandle(Wnd), WM_SYSCOMMAND, SC_RESTORE, 0L );
860 break;
861 }
862 if ((HIWORD(lParam) & KF_ALTDOWN) && wParam)
863 {
864 if (wParam == VK_TAB || wParam == VK_ESCAPE) break;
865 if (wParam == VK_SPACE && Wnd->style & WS_CHILD)
866 co_IntSendMessage( UserHMGetHandle(IntGetParent(Wnd)), Msg, wParam, lParam );
867 else
868 co_IntSendMessage( UserHMGetHandle(Wnd), WM_SYSCOMMAND, SC_KEYMENU, wParam );
869 }
870 else /* check for Ctrl-Esc */
871 if (wParam != VK_ESCAPE) UserPostMessage(hwndSAS, WM_LOGONNOTIFY, LN_MESSAGE_BEEP, 0); //MessageBeep(0);
872 break;
873 }
874
875 case WM_CANCELMODE:
876 {
877 pti->MessageQueue->QF_flags &= ~(QF_FMENUSTATUS|QF_FMENUSTATUSBREAK);
878
879 MENU_EndMenu( Wnd );
880 if (IntGetCaptureWindow() == UserHMGetHandle(Wnd))
881 {
882 IntReleaseCapture();
883 }
884 break;
885 }
886
887 case WM_CLOSE:
888 co_UserDestroyWindow(Wnd);
889 break;
890
891 case WM_CTLCOLORMSGBOX:
892 case WM_CTLCOLOREDIT:
893 case WM_CTLCOLORLISTBOX:
894 case WM_CTLCOLORBTN:
895 case WM_CTLCOLORDLG:
896 case WM_CTLCOLORSTATIC:
897 case WM_CTLCOLORSCROLLBAR:
898 return (LRESULT) DefWndControlColor((HDC)wParam, Msg - WM_CTLCOLORMSGBOX);
899
900 case WM_CTLCOLOR:
901 return (LRESULT) DefWndControlColor((HDC)wParam, HIWORD(lParam));
902
903 case WM_SETCURSOR:
904 {
905 if (Wnd->style & WS_CHILD)
906 {
907 /* with the exception of the border around a resizable wnd,
908 * give the parent first chance to set the cursor */
909 if (LOWORD(lParam) < HTLEFT || LOWORD(lParam) > HTBOTTOMRIGHT)
910 {
911 PWND parent = Wnd->spwndParent;//IntGetParent( Wnd );
912 if (parent != UserGetDesktopWindow() &&
913 co_IntSendMessage( UserHMGetHandle(parent), WM_SETCURSOR, wParam, lParam))
914 return TRUE;
915 }
916 }
917 return DefWndHandleSetCursor(Wnd, wParam, lParam);
918 }
919
920 case WM_MOUSEACTIVATE:
921 if (Wnd->style & WS_CHILD)
922 {
923 LONG Ret;
924 HWND hwndParent;
925 PWND pwndParent = IntGetParent(Wnd);
926 hwndParent = pwndParent ? UserHMGetHandle(pwndParent) : NULL;
927 if (hwndParent) Ret = co_IntSendMessage(hwndParent, WM_MOUSEACTIVATE, wParam, lParam);
928 if (Ret) return (Ret);
929 }
930 return ( (HIWORD(lParam) == WM_LBUTTONDOWN && LOWORD(lParam) == HTCAPTION) ? MA_NOACTIVATE : MA_ACTIVATE );
931
932 case WM_ACTIVATE:
933 /* The default action in Windows is to set the keyboard focus to
934 * the window, if it's being activated and not minimized */
935 if (LOWORD(wParam) != WA_INACTIVE &&
936 !(Wnd->style & WS_MINIMIZE))
937 {
938 //ERR("WM_ACTIVATE %p\n",hWnd);
939 co_UserSetFocus(Wnd);
940 }
941 break;
942
943 case WM_MOUSEWHEEL:
944 if (Wnd->style & WS_CHILD)
945 {
946 HWND hwndParent;
947 PWND pwndParent = IntGetParent(Wnd);
948 hwndParent = pwndParent ? UserHMGetHandle(pwndParent) : NULL;
949 return co_IntSendMessage( hwndParent, WM_MOUSEWHEEL, wParam, lParam);
950 }
951 break;
952
953 case WM_ERASEBKGND:
954 case WM_ICONERASEBKGND:
955 {
956 RECT Rect;
957 HBRUSH hBrush = Wnd->pcls->hbrBackground;
958 if (!hBrush) return 0;
959 if (hBrush <= (HBRUSH)COLOR_MENUBAR)
960 {
961 hBrush = IntGetSysColorBrush((INT)hBrush);
962 }
963 if (Wnd->pcls->style & CS_PARENTDC)
964 {
965 /* can't use GetClipBox with a parent DC or we fill the whole parent */
966 IntGetClientRect(Wnd, &Rect);
967 GreDPtoLP((HDC)wParam, (LPPOINT)&Rect, 2);
968 }
969 else
970 {
971 GdiGetClipBox((HDC)wParam, &Rect);
972 }
973 FillRect((HDC)wParam, &Rect, hBrush);
974 return (1);
975 }
976
977 case WM_GETHOTKEY:
978 //ERR("WM_GETHOTKEY\n");
979 return DefWndGetHotKey(Wnd);
980 case WM_SETHOTKEY:
981 //ERR("WM_SETHOTKEY\n");
982 return DefWndSetHotKey(Wnd, wParam);
983
984 case WM_NCHITTEST:
985 {
986 POINT Point;
987 Point.x = GET_X_LPARAM(lParam);
988 Point.y = GET_Y_LPARAM(lParam);
989 return GetNCHitEx(Wnd, Point);
990 }
991
992 case WM_PRINT:
993 {
994 DefWndPrint(Wnd, (HDC)wParam, lParam);
995 return (0);
996 }
997
998 case WM_SYSCOLORCHANGE:
999 {
1000 /* force to redraw non-client area */
1001 UserPaintCaption(Wnd, DC_NC);
1002 /* Use InvalidateRect to redraw client area, enable
1003 * erase to redraw all subcontrols otherwise send the
1004 * WM_SYSCOLORCHANGE to child windows/controls is required
1005 */
1006 co_UserRedrawWindow( Wnd, NULL, NULL, RDW_ALLCHILDREN|RDW_INVALIDATE|RDW_ERASE);
1007 return (0);
1008 }
1009
1010 case WM_PAINTICON:
1011 case WM_PAINT:
1012 {
1013 PAINTSTRUCT Ps;
1014 HDC hDC;
1015
1016 /* If already in Paint and Client area is not empty just return. */
1017 if (Wnd->state2 & WNDS2_STARTPAINT && !RECTL_bIsEmptyRect(&Wnd->rcClient))
1018 {
1019 ERR("In Paint and Client area is not empty!\n");
1020 return 0;
1021 }
1022
1023 hDC = IntBeginPaint(Wnd, &Ps);
1024 if (hDC)
1025 {
1026 if (((Wnd->style & WS_MINIMIZE) != 0) && (Wnd->pcls->spicn))
1027 {
1028 RECT ClientRect;
1029 INT x, y;
1030
1031 ERR("Doing Paint and Client area is empty!\n");
1032 IntGetClientRect(Wnd, &ClientRect);
1033 x = (ClientRect.right - ClientRect.left - UserGetSystemMetrics(SM_CXICON)) / 2;
1034 y = (ClientRect.bottom - ClientRect.top - UserGetSystemMetrics(SM_CYICON)) / 2;
1035 UserReferenceObject(Wnd->pcls->spicn);
1036 UserDrawIconEx(hDC, x, y, Wnd->pcls->spicn, 0, 0, 0, 0, DI_NORMAL | DI_COMPAT | DI_DEFAULTSIZE);
1037 UserDereferenceObject(Wnd->pcls->spicn);
1038 }
1039
1040 IntEndPaint(Wnd, &Ps);
1041 }
1042 return (0);
1043 }
1044
1045 case WM_SYNCPAINT:
1046 {
1047 HRGN hRgn;
1048 Wnd->state &= ~WNDS_SYNCPAINTPENDING;
1049 TRACE("WM_SYNCPAINT\n");
1050 hRgn = NtGdiCreateRectRgn(0, 0, 0, 0);
1051 if (hRgn)
1052 {
1053 if (co_UserGetUpdateRgn(Wnd, hRgn, FALSE) != NULLREGION)
1054 {
1055 PREGION pRgn = REGION_LockRgn(hRgn);
1056 if (pRgn) REGION_UnlockRgn(pRgn);
1057 if (!wParam)
1058 wParam = (RDW_ERASENOW | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN);
1059 co_UserRedrawWindow(Wnd, NULL, pRgn, wParam);
1060 }
1061 GreDeleteObject(hRgn);
1062 }
1063 return 0;
1064 }
1065
1066 case WM_SETREDRAW:
1067 if (wParam)
1068 {
1069 if (!(Wnd->style & WS_VISIBLE))
1070 {
1071 IntSetStyle( Wnd, WS_VISIBLE, 0 );
1072 Wnd->state |= WNDS_SENDNCPAINT;
1073 }
1074 }
1075 else
1076 {
1077 if (Wnd->style & WS_VISIBLE)
1078 {
1079 co_UserRedrawWindow( Wnd, NULL, NULL, RDW_ALLCHILDREN | RDW_VALIDATE );
1080 IntSetStyle( Wnd, 0, WS_VISIBLE );
1081 }
1082 }
1083 return 0;
1084
1085 case WM_WINDOWPOSCHANGING:
1086 {
1087 return (DefWndHandleWindowPosChanging(Wnd, (WINDOWPOS*)lParam));
1088 }
1089
1090 case WM_WINDOWPOSCHANGED:
1091 {
1092 return (DefWndHandleWindowPosChanged(Wnd, (WINDOWPOS*)lParam));
1093 }
1094
1095 case WM_NCCALCSIZE:
1096 {
1097 return NC_HandleNCCalcSize( Wnd, wParam, (RECTL *)lParam );
1098 }
1099
1100 case WM_NCACTIVATE:
1101 {
1102 return NC_HandleNCActivate( Wnd, wParam, lParam );
1103 }
1104
1105 //
1106 // NC Paint mode.
1107 //
1108 case WM_NCPAINT:
1109 {
1110 HDC hDC = UserGetDCEx(Wnd, (HRGN)wParam, DCX_WINDOW | DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN);
1111 Wnd->state |= WNDS_FORCEMENUDRAW;
1112 NC_DoNCPaint(Wnd, hDC, -1);
1113 Wnd->state &= ~WNDS_FORCEMENUDRAW;
1114 UserReleaseDC(Wnd, hDC, FALSE);
1115 return 0;
1116 }
1117 //
1118 // Draw Caption mode.
1119 //
1120 // wParam are DC_* flags.
1121 //
1122 case WM_NCUAHDRAWCAPTION:
1123 {
1124 HDC hDC = UserGetDCEx(Wnd, NULL, DCX_WINDOW|DCX_USESTYLE);
1125 TRACE("WM_NCUAHDRAWCAPTION: wParam DC_ flags %08x\n",wParam);
1126 UserDrawCaptionBar(Wnd, hDC, wParam|DC_FRAME); // Include DC_FRAME to comp for drawing glich.
1127 UserReleaseDC(Wnd, hDC, FALSE);
1128 return 0;
1129 }
1130 //
1131 // Draw Frame mode.
1132 //
1133 // wParam is HDC, lParam are DC_ACTIVE and or DC_REDRAWHUNGWND.
1134 //
1135 case WM_NCUAHDRAWFRAME:
1136 {
1137 TRACE("WM_NCUAHDRAWFRAME: wParam hDC %p lParam DC_ flags %08x\n",wParam,lParam);
1138 NC_DoNCPaint(Wnd, (HDC)wParam, lParam|DC_NC);
1139 return 0;
1140 }
1141
1142 /* ReactOS only. */
1143 case WM_CBT:
1144 {
1145 switch (wParam)
1146 {
1147 case HCBT_MOVESIZE:
1148 {
1149 RECTL rt;
1150
1151 if (lParam)
1152 {
1153 _SEH2_TRY
1154 {
1155 ProbeForRead((PVOID)lParam,
1156 sizeof(RECT),
1157 1);
1158
1159 RtlCopyMemory(&rt,
1160 (PVOID)lParam,
1161 sizeof(RECT));
1162 }
1163 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1164 {
1165 lResult = 1;
1166 }
1167 _SEH2_END;
1168 }
1169 if (!lResult)
1170 lResult = co_HOOK_CallHooks(WH_CBT, HCBT_MOVESIZE, (WPARAM)Wnd->head.h, lParam ? (LPARAM)&rt : 0);
1171 }
1172 break;
1173 }
1174 break;
1175 }
1176 break;
1177 }
1178 return lResult;
1179 }
1180
1181 /* EOF */