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.
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * 09-05-2001 CSH Created
29 /* INCLUDES ******************************************************************/
33 #include <wine/debug.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
36 /* GLOBALS *******************************************************************/
40 /* FUNCTIONS *****************************************************************/
53 return NtUserDragDetect(hWnd
, pt
);
58 ULONG dx
= GetSystemMetrics(SM_CXDRAG
);
59 ULONG dy
= GetSystemMetrics(SM_CYDRAG
);
61 rect
.left
= pt
.x
- dx
;
62 rect
.right
= pt
.x
+ dx
;
64 rect
.bottom
= pt
.y
+ dy
;
71 PeekMessageW(&msg
, 0, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_REMOVE
) ||
72 PeekMessageW(&msg
, 0, WM_KEYFIRST
, WM_KEYLAST
, PM_REMOVE
)
75 if (msg
.message
== WM_LBUTTONUP
)
80 if (msg
.message
== WM_MOUSEMOVE
)
82 tmp
.x
= LOWORD(msg
.lParam
);
83 tmp
.y
= HIWORD(msg
.lParam
);
84 if (!PtInRect(&rect
, tmp
))
90 if (msg
.message
== WM_KEYDOWN
)
92 if (msg
.wParam
== VK_ESCAPE
)
110 EnableWindow(HWND hWnd
,
113 LONG Style
= GetWindowLongPtrW(hWnd
, GWL_STYLE
);
114 /* check if updating is needed */
115 UINT bIsDisabled
= (Style
& WS_DISABLED
);
116 if ( (bIsDisabled
&& bEnable
) || (!bIsDisabled
&& !bEnable
) )
120 Style
&= ~WS_DISABLED
;
124 Style
|= WS_DISABLED
;
125 /* Remove keyboard focus from that window if it had focus */
126 if (hWnd
== GetFocus())
131 NtUserSetWindowLong(hWnd
, GWL_STYLE
, Style
, FALSE
);
133 SendMessageA(hWnd
, WM_ENABLE
, (LPARAM
) IsWindowEnabled(hWnd
), 0);
135 // Return nonzero if it was disabled, or zero if it wasn't:
136 return IsWindowEnabled(hWnd
);
144 GetAsyncKeyState(int vKey
)
146 if (vKey
< 0 || vKey
> 256)
148 return (SHORT
) NtUserGetAsyncKeyState((DWORD
) vKey
);
156 GetKeyboardLayout(DWORD idThread
)
158 return (HKL
)NtUserCallOneParam((DWORD_PTR
) idThread
, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT
);
176 GetKeyNameTextA(LONG lParam
,
180 LPWSTR intermediateString
=
181 HeapAlloc(GetProcessHeap(),0,nSize
* sizeof(WCHAR
));
184 BOOL defChar
= FALSE
;
186 if( !intermediateString
) return 0;
187 ret
= GetKeyNameTextW(lParam
,intermediateString
,nSize
);
188 if( ret
== 0 ) { lpString
[0] = 0; return 0; }
190 wstrLen
= wcslen( intermediateString
);
191 ret
= WideCharToMultiByte(CP_ACP
, 0,
192 intermediateString
, wstrLen
,
193 lpString
, nSize
, ".", &defChar
);
195 HeapFree(GetProcessHeap(),0,intermediateString
);
204 GetKeyNameTextW(LONG lParam
,
208 return NtUserGetKeyNameText( lParam
, lpString
, nSize
);
216 GetKeyState(int nVirtKey
)
218 return (SHORT
) NtUserGetKeyState((DWORD
) nVirtKey
);
226 GetKeyboardLayoutNameA(LPSTR pwszKLID
)
228 WCHAR buf
[KL_NAMELENGTH
];
230 if (GetKeyboardLayoutNameW(buf
))
231 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pwszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
240 GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
242 return NtUserGetKeyboardLayoutName( pwszKLID
);
250 GetKeyboardType(int nTypeFlag
)
252 return (int)NtUserCallOneParam((DWORD_PTR
) nTypeFlag
, ONEPARAM_ROUTINE_GETKEYBOARDTYPE
);
260 GetLastInputInfo(PLASTINPUTINFO plii
)
262 return NtUserGetLastInputInfo(plii
);
270 LoadKeyboardLayoutA(LPCSTR pwszKLID
,
273 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
274 strtoul(pwszKLID
, NULL
, 16),
283 LoadKeyboardLayoutW(LPCWSTR pwszKLID
,
286 // Look at revision 25596 to see how it's done in windows.
287 // We will do things our own way. Also be compatible too!
288 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
289 wcstoul(pwszKLID
, NULL
, 16),
298 MapVirtualKeyA(UINT uCode
,
301 return MapVirtualKeyExA( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
309 MapVirtualKeyExA(UINT uCode
,
313 return MapVirtualKeyExW( uCode
, uMapType
, dwhkl
);
321 MapVirtualKeyExW(UINT uCode
,
325 return NtUserMapVirtualKeyEx( uCode
, uMapType
, 0, dwhkl
);
333 MapVirtualKeyW(UINT uCode
,
336 return MapVirtualKeyExW( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
344 OemKeyScan(WORD wOemChar
)
350 MultiByteToWideChar(CP_OEMCP
, 0, (PCSTR
)&wOemChar
, 1, &p
, 1);
352 Scan
= MapVirtualKeyW((Vk
& 0x00ff), 0);
355 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
356 scan code and high word has the shift state.
358 return ((Vk
& 0xff00) << 8) | Scan
;
366 SetDoubleClickTime(UINT uInterval
)
368 return (BOOL
)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME
,
383 return NtUserSwapMouseButton(fSwap
);
391 ToAscii(UINT uVirtKey
,
393 CONST PBYTE lpKeyState
,
397 return ToAsciiEx(uVirtKey
, uScanCode
, lpKeyState
, lpChar
, uFlags
, 0);
405 ToAsciiEx(UINT uVirtKey
,
407 CONST PBYTE lpKeyState
,
415 Ret
= ToUnicodeEx(uVirtKey
, uScanCode
, lpKeyState
, UniChars
, 2, uFlags
, dwhkl
);
416 CharCount
= (Ret
< 0 ? 1 : Ret
);
417 WideCharToMultiByte(CP_ACP
, 0, UniChars
, CharCount
, (LPSTR
) lpChar
, 2, NULL
, NULL
);
427 ToUnicode(UINT wVirtKey
,
429 CONST PBYTE lpKeyState
,
434 return ToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
443 ToUnicodeEx(UINT wVirtKey
,
445 CONST PBYTE lpKeyState
,
451 return NtUserToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
465 if (IsDBCSLeadByte(ch
)) return -1;
467 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
468 return VkKeyScanW(wChar
);
476 VkKeyScanExA(CHAR ch
,
481 if (IsDBCSLeadByte(ch
)) return -1;
483 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
484 return VkKeyScanExW(wChar
, dwhkl
);
492 VkKeyScanExW(WCHAR ch
,
495 return (SHORT
) NtUserVkKeyScanEx(ch
, dwhkl
, TRUE
);
505 return (SHORT
) NtUserVkKeyScanEx(ch
, 0, FALSE
);
518 ULONG_PTR dwExtraInfo
)
524 Input
.type
= INPUT_KEYBOARD
;
526 Input
.ki
.wScan
= bScan
;
527 Input
.ki
.dwFlags
= dwFlags
;
529 Input
.ki
.dwExtraInfo
= dwExtraInfo
;
531 NtUserSendInput(1, &Input
, sizeof(INPUT
));
545 ULONG_PTR dwExtraInfo
)
549 Input
.type
= INPUT_MOUSE
;
552 Input
.mi
.mouseData
= dwData
;
553 Input
.mi
.dwFlags
= dwFlags
;
555 Input
.mi
.dwExtraInfo
= dwExtraInfo
;
557 NtUserSendInput(1, &Input
, sizeof(INPUT
));
561 /***********************************************************************
564 static WORD
get_key_state(void)
568 if (GetSystemMetrics( SM_SWAPBUTTON
))
570 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
571 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
575 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
576 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
578 if (GetAsyncKeyState(VK_MBUTTON
) & 0x80) ret
|= MK_MBUTTON
;
579 if (GetAsyncKeyState(VK_SHIFT
) & 0x80) ret
|= MK_SHIFT
;
580 if (GetAsyncKeyState(VK_CONTROL
) & 0x80) ret
|= MK_CONTROL
;
581 if (GetAsyncKeyState(VK_XBUTTON1
) & 0x80) ret
|= MK_XBUTTON1
;
582 if (GetAsyncKeyState(VK_XBUTTON2
) & 0x80) ret
|= MK_XBUTTON2
;
586 static void CALLBACK
TrackMouseEventProc(HWND hwndUnused
, UINT uMsg
, UINT_PTR idEvent
,
592 INT hoverwidth
= 0, hoverheight
= 0;
594 PUSER32_TRACKINGLIST ptracking_info
;
596 ptracking_info
= & User32GetThreadData()->tracking_info
;
599 hwnd
= WindowFromPoint(pos
);
601 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
602 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
604 /* see if this tracking event is looking for TME_LEAVE and that the */
605 /* mouse has left the window */
606 if (ptracking_info
->tme
.dwFlags
& TME_LEAVE
)
608 if (ptracking_info
->tme
.hwndTrack
!= hwnd
)
610 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
612 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
616 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
619 /* remove the TME_LEAVE flag */
620 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
624 GetClientRect(hwnd
, &client
);
625 MapWindowPoints(hwnd
, NULL
, (LPPOINT
)&client
, 2);
626 if (PtInRect(&client
, pos
))
628 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
630 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
631 /* remove the TME_LEAVE flag */
632 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
637 if (!(ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
))
639 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
640 /* remove the TME_LEAVE flag */
641 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
647 /* see if we are tracking hovering for this hwnd */
648 if (ptracking_info
->tme
.dwFlags
& TME_HOVER
)
650 /* has the cursor moved outside the rectangle centered around pos? */
651 if ((abs(pos
.x
- ptracking_info
->pos
.x
) > (hoverwidth
/ 2.0)) ||
652 (abs(pos
.y
- ptracking_info
->pos
.y
) > (hoverheight
/ 2.0)))
654 /* record this new position as the current position and reset */
655 /* the iHoverTime variable to 0 */
656 ptracking_info
->pos
= pos
;
662 ScreenToClient(hwnd
, &posClient
);
664 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
666 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSEHOVER
,
667 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
671 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSEHOVER
,
672 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
675 /* stop tracking mouse hover */
676 ptracking_info
->tme
.dwFlags
&= ~TME_HOVER
;
680 /* stop the timer if the tracking list is empty */
681 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
683 KillTimer(0, ptracking_info
->timer
);
684 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
689 /***********************************************************************
690 * TrackMouseEvent [USER32]
692 * Requests notification of mouse events
694 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
695 * to the hwnd specified in the ptme structure. After the event message
696 * is posted to the hwnd, the entry in the queue is removed.
698 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
699 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
700 * immediately and the TME_LEAVE flag being ignored.
703 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
716 LPTRACKMOUSEEVENT ptme
)
721 PUSER32_TRACKINGLIST ptracking_info
;
723 TRACE("%lx, %lx, %p, %lx\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
725 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
726 WARN("wrong TRACKMOUSEEVENT size from app\n");
727 SetLastError(ERROR_INVALID_PARAMETER
);
731 ptracking_info
= & User32GetThreadData()->tracking_info
;
733 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
734 if (ptme
->dwFlags
& TME_QUERY
)
736 *ptme
= ptracking_info
->tme
;
737 ptme
->cbSize
= sizeof(TRACKMOUSEEVENT
);
739 return TRUE
; /* return here, TME_QUERY is retrieving information */
742 if (!IsWindow(ptme
->hwndTrack
))
744 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
748 hover_time
= ptme
->dwHoverTime
;
750 /* if HOVER_DEFAULT was specified replace this with the systems current value */
751 if (hover_time
== HOVER_DEFAULT
|| hover_time
== 0)
753 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME
, 0, &hover_time
, 0);
757 hwnd
= WindowFromPoint(pos
);
759 if (ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
))
761 FIXME("Unknown flag(s) %08lx\n", ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
));
764 if (ptme
->dwFlags
& TME_CANCEL
)
766 if (ptracking_info
->tme
.hwndTrack
== ptme
->hwndTrack
)
768 ptracking_info
->tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
770 /* if we aren't tracking on hover or leave remove this entry */
771 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
773 KillTimer(0, ptracking_info
->timer
);
774 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
778 if (ptme
->hwndTrack
== hwnd
)
780 /* Adding new mouse event to the tracking list */
781 ptracking_info
->tme
= *ptme
;
782 ptracking_info
->tme
.dwHoverTime
= hover_time
;
784 /* Initialize HoverInfo variables even if not hover tracking */
785 ptracking_info
->pos
= pos
;
787 if (!ptracking_info
->timer
)
789 ptracking_info
->timer
= SetTimer(0, 0, hover_time
, TrackMouseEventProc
);