3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * PROJECT: ReactOS user32.dll
21 * FILE: dll/win32/user32/windows/input.c
23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * 09-05-2001 CSH Created
28 /* INCLUDES ******************************************************************/
32 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
35 /* GLOBALS *******************************************************************/
39 /* FUNCTIONS *****************************************************************/
52 return NtUserDragDetect(hWnd
, pt
);
57 ULONG dx
= GetSystemMetrics(SM_CXDRAG
);
58 ULONG dy
= GetSystemMetrics(SM_CYDRAG
);
60 rect
.left
= pt
.x
- dx
;
61 rect
.right
= pt
.x
+ dx
;
63 rect
.bottom
= pt
.y
+ dy
;
70 PeekMessageW(&msg
, 0, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_REMOVE
) ||
71 PeekMessageW(&msg
, 0, WM_KEYFIRST
, WM_KEYLAST
, PM_REMOVE
)
74 if (msg
.message
== WM_LBUTTONUP
)
79 if (msg
.message
== WM_MOUSEMOVE
)
81 tmp
.x
= LOWORD(msg
.lParam
);
82 tmp
.y
= HIWORD(msg
.lParam
);
83 if (!PtInRect(&rect
, tmp
))
89 if (msg
.message
== WM_KEYDOWN
)
91 if (msg
.wParam
== VK_ESCAPE
)
109 EnableWindow(HWND hWnd
,
112 // This will soon be moved to win32k.
114 LONG Style
= GetWindowLongPtrW(hWnd
, GWL_STYLE
);
115 /* check if updating is needed */
116 UINT bIsDisabled
= (Style
& WS_DISABLED
);
117 Update
= bIsDisabled
;
121 Style
&= ~WS_DISABLED
;
125 Update
= !bIsDisabled
;
127 SendMessageW( hWnd
, WM_CANCELMODE
, 0, 0);
129 /* Remove keyboard focus from that window if it had focus */
130 if (hWnd
== GetFocus())
134 Style
|= WS_DISABLED
;
137 NtUserSetWindowLong(hWnd
, GWL_STYLE
, Style
, FALSE
);
141 SendMessageW(hWnd
, WM_ENABLE
, (LPARAM
)bEnable
, 0);
143 // Return nonzero if it was disabled, or zero if it wasn't:
152 GetAsyncKeyState(int vKey
)
154 if (vKey
< 0 || vKey
> 256)
156 return (SHORT
) NtUserGetAsyncKeyState((DWORD
) vKey
);
164 GetKeyboardLayout(DWORD idThread
)
166 return (HKL
)NtUserCallOneParam((DWORD_PTR
) idThread
, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT
);
184 GetKeyNameTextA(LONG lParam
,
188 LPWSTR intermediateString
=
189 HeapAlloc(GetProcessHeap(),0,nSize
* sizeof(WCHAR
));
192 BOOL defChar
= FALSE
;
194 if( !intermediateString
) return 0;
195 ret
= GetKeyNameTextW(lParam
,intermediateString
,nSize
);
196 if( ret
== 0 ) { lpString
[0] = 0; return 0; }
198 wstrLen
= wcslen( intermediateString
);
199 ret
= WideCharToMultiByte(CP_ACP
, 0,
200 intermediateString
, wstrLen
,
201 lpString
, nSize
, ".", &defChar
);
203 HeapFree(GetProcessHeap(),0,intermediateString
);
212 GetKeyNameTextW(LONG lParam
,
216 return NtUserGetKeyNameText( lParam
, lpString
, nSize
);
224 GetKeyState(int nVirtKey
)
226 return (SHORT
) NtUserGetKeyState((DWORD
) nVirtKey
);
234 GetKeyboardLayoutNameA(LPSTR pwszKLID
)
236 WCHAR buf
[KL_NAMELENGTH
];
238 if (GetKeyboardLayoutNameW(buf
))
239 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pwszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
248 GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
250 return NtUserGetKeyboardLayoutName( pwszKLID
);
258 GetKeyboardType(int nTypeFlag
)
260 return (int)NtUserCallOneParam((DWORD_PTR
) nTypeFlag
, ONEPARAM_ROUTINE_GETKEYBOARDTYPE
);
268 GetLastInputInfo(PLASTINPUTINFO plii
)
270 return NtUserGetLastInputInfo(plii
);
278 LoadKeyboardLayoutA(LPCSTR pwszKLID
,
281 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
282 strtoul(pwszKLID
, NULL
, 16),
291 LoadKeyboardLayoutW(LPCWSTR pwszKLID
,
294 // Look at revision 25596 to see how it's done in windows.
295 // We will do things our own way. Also be compatible too!
296 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
297 wcstoul(pwszKLID
, NULL
, 16),
306 MapVirtualKeyA(UINT uCode
,
309 return MapVirtualKeyExA( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
317 MapVirtualKeyExA(UINT uCode
,
321 return MapVirtualKeyExW( uCode
, uMapType
, dwhkl
);
329 MapVirtualKeyExW(UINT uCode
,
333 return NtUserMapVirtualKeyEx( uCode
, uMapType
, 0, dwhkl
);
341 MapVirtualKeyW(UINT uCode
,
344 return MapVirtualKeyExW( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
352 OemKeyScan(WORD wOemChar
)
358 MultiByteToWideChar(CP_OEMCP
, 0, (PCSTR
)&wOemChar
, 1, &p
, 1);
360 Scan
= MapVirtualKeyW((Vk
& 0x00ff), 0);
363 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
364 scan code and high word has the shift state.
366 return ((Vk
& 0xff00) << 8) | Scan
;
374 SetDoubleClickTime(UINT uInterval
)
376 return (BOOL
)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME
,
391 return NtUserSwapMouseButton(fSwap
);
399 ToAscii(UINT uVirtKey
,
401 CONST BYTE
*lpKeyState
,
405 return ToAsciiEx(uVirtKey
, uScanCode
, lpKeyState
, lpChar
, uFlags
, 0);
413 ToAsciiEx(UINT uVirtKey
,
415 CONST BYTE
*lpKeyState
,
423 Ret
= ToUnicodeEx(uVirtKey
, uScanCode
, lpKeyState
, UniChars
, 2, uFlags
, dwhkl
);
424 CharCount
= (Ret
< 0 ? 1 : Ret
);
425 WideCharToMultiByte(CP_ACP
, 0, UniChars
, CharCount
, (LPSTR
) lpChar
, 2, NULL
, NULL
);
435 ToUnicode(UINT wVirtKey
,
437 CONST BYTE
*lpKeyState
,
442 return ToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
451 ToUnicodeEx(UINT wVirtKey
,
453 CONST BYTE
*lpKeyState
,
459 return NtUserToUnicodeEx( wVirtKey
, wScanCode
, (PBYTE
)lpKeyState
, pwszBuff
, cchBuff
,
473 if (IsDBCSLeadByte(ch
)) return -1;
475 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
476 return VkKeyScanW(wChar
);
484 VkKeyScanExA(CHAR ch
,
489 if (IsDBCSLeadByte(ch
)) return -1;
491 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
492 return VkKeyScanExW(wChar
, dwhkl
);
500 VkKeyScanExW(WCHAR ch
,
503 return (SHORT
) NtUserVkKeyScanEx(ch
, dwhkl
, TRUE
);
513 return (SHORT
) NtUserVkKeyScanEx(ch
, 0, FALSE
);
526 ULONG_PTR dwExtraInfo
)
532 Input
.type
= INPUT_KEYBOARD
;
534 Input
.ki
.wScan
= bScan
;
535 Input
.ki
.dwFlags
= dwFlags
;
537 Input
.ki
.dwExtraInfo
= dwExtraInfo
;
539 NtUserSendInput(1, &Input
, sizeof(INPUT
));
553 ULONG_PTR dwExtraInfo
)
557 Input
.type
= INPUT_MOUSE
;
560 Input
.mi
.mouseData
= dwData
;
561 Input
.mi
.dwFlags
= dwFlags
;
563 Input
.mi
.dwExtraInfo
= dwExtraInfo
;
565 NtUserSendInput(1, &Input
, sizeof(INPUT
));
569 /***********************************************************************
572 static WORD
get_key_state(void)
576 if (GetSystemMetrics( SM_SWAPBUTTON
))
578 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
579 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
583 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
584 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
586 if (GetAsyncKeyState(VK_MBUTTON
) & 0x80) ret
|= MK_MBUTTON
;
587 if (GetAsyncKeyState(VK_SHIFT
) & 0x80) ret
|= MK_SHIFT
;
588 if (GetAsyncKeyState(VK_CONTROL
) & 0x80) ret
|= MK_CONTROL
;
589 if (GetAsyncKeyState(VK_XBUTTON1
) & 0x80) ret
|= MK_XBUTTON1
;
590 if (GetAsyncKeyState(VK_XBUTTON2
) & 0x80) ret
|= MK_XBUTTON2
;
594 static void CALLBACK
TrackMouseEventProc(HWND hwndUnused
, UINT uMsg
, UINT_PTR idEvent
,
600 INT hoverwidth
= 0, hoverheight
= 0;
602 PUSER32_TRACKINGLIST ptracking_info
;
604 ptracking_info
= & User32GetThreadData()->tracking_info
;
607 hwnd
= WindowFromPoint(pos
);
609 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
610 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
612 /* see if this tracking event is looking for TME_LEAVE and that the */
613 /* mouse has left the window */
614 if (ptracking_info
->tme
.dwFlags
& TME_LEAVE
)
616 if (ptracking_info
->tme
.hwndTrack
!= hwnd
)
618 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
620 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
624 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
627 /* remove the TME_LEAVE flag */
628 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
632 GetClientRect(hwnd
, &client
);
633 MapWindowPoints(hwnd
, NULL
, (LPPOINT
)&client
, 2);
634 if (PtInRect(&client
, pos
))
636 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
638 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
639 /* remove the TME_LEAVE flag */
640 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
645 if (!(ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
))
647 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
648 /* remove the TME_LEAVE flag */
649 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
655 /* see if we are tracking hovering for this hwnd */
656 if (ptracking_info
->tme
.dwFlags
& TME_HOVER
)
658 /* has the cursor moved outside the rectangle centered around pos? */
659 if ((abs(pos
.x
- ptracking_info
->pos
.x
) > (hoverwidth
/ 2.0)) ||
660 (abs(pos
.y
- ptracking_info
->pos
.y
) > (hoverheight
/ 2.0)))
662 /* record this new position as the current position and reset */
663 /* the iHoverTime variable to 0 */
664 ptracking_info
->pos
= pos
;
670 ScreenToClient(hwnd
, &posClient
);
672 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
674 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSEHOVER
,
675 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
679 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSEHOVER
,
680 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
683 /* stop tracking mouse hover */
684 ptracking_info
->tme
.dwFlags
&= ~TME_HOVER
;
688 /* stop the timer if the tracking list is empty */
689 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
691 KillTimer(0, ptracking_info
->timer
);
692 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
697 /***********************************************************************
698 * TrackMouseEvent [USER32]
700 * Requests notification of mouse events
702 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
703 * to the hwnd specified in the ptme structure. After the event message
704 * is posted to the hwnd, the entry in the queue is removed.
706 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
707 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
708 * immediately and the TME_LEAVE flag being ignored.
711 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
724 LPTRACKMOUSEEVENT ptme
)
729 PUSER32_TRACKINGLIST ptracking_info
;
731 TRACE("%lx, %lx, %p, %lx\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
733 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
734 WARN("wrong TRACKMOUSEEVENT size from app\n");
735 SetLastError(ERROR_INVALID_PARAMETER
);
739 ptracking_info
= & User32GetThreadData()->tracking_info
;
741 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
742 if (ptme
->dwFlags
& TME_QUERY
)
744 *ptme
= ptracking_info
->tme
;
745 ptme
->cbSize
= sizeof(TRACKMOUSEEVENT
);
747 return TRUE
; /* return here, TME_QUERY is retrieving information */
750 if (!IsWindow(ptme
->hwndTrack
))
752 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
756 hover_time
= ptme
->dwHoverTime
;
758 /* if HOVER_DEFAULT was specified replace this with the systems current value */
759 if (hover_time
== HOVER_DEFAULT
|| hover_time
== 0)
761 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME
, 0, &hover_time
, 0);
765 hwnd
= WindowFromPoint(pos
);
767 if (ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
))
769 FIXME("Unknown flag(s) %08lx\n", ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
));
772 if (ptme
->dwFlags
& TME_CANCEL
)
774 if (ptracking_info
->tme
.hwndTrack
== ptme
->hwndTrack
)
776 ptracking_info
->tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
778 /* if we aren't tracking on hover or leave remove this entry */
779 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
781 KillTimer(0, ptracking_info
->timer
);
782 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
786 if (ptme
->hwndTrack
== hwnd
)
788 /* Adding new mouse event to the tracking list */
789 ptracking_info
->tme
= *ptme
;
790 ptracking_info
->tme
.dwHoverTime
= hover_time
;
792 /* Initialize HoverInfo variables even if not hover tracking */
793 ptracking_info
->pos
= pos
;
795 if (!ptracking_info
->timer
)
797 ptracking_info
->timer
= SetTimer(0, 0, hover_time
, TrackMouseEventProc
);