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 LONG Style
= GetWindowLongPtrW(hWnd
, GWL_STYLE
);
113 /* check if updating is needed */
114 UINT bIsDisabled
= (Style
& WS_DISABLED
);
115 if ( (bIsDisabled
&& bEnable
) || (!bIsDisabled
&& !bEnable
) )
119 Style
&= ~WS_DISABLED
;
123 Style
|= WS_DISABLED
;
124 /* Remove keyboard focus from that window if it had focus */
125 if (hWnd
== GetFocus())
130 NtUserSetWindowLong(hWnd
, GWL_STYLE
, Style
, FALSE
);
132 SendMessageA(hWnd
, WM_ENABLE
, (LPARAM
) IsWindowEnabled(hWnd
), 0);
134 // Return nonzero if it was disabled, or zero if it wasn't:
135 return IsWindowEnabled(hWnd
);
143 GetAsyncKeyState(int vKey
)
145 if (vKey
< 0 || vKey
> 256)
147 return (SHORT
) NtUserGetAsyncKeyState((DWORD
) vKey
);
155 GetKeyboardLayout(DWORD idThread
)
157 return (HKL
)NtUserCallOneParam((DWORD_PTR
) idThread
, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT
);
175 GetKeyNameTextA(LONG lParam
,
179 LPWSTR intermediateString
=
180 HeapAlloc(GetProcessHeap(),0,nSize
* sizeof(WCHAR
));
183 BOOL defChar
= FALSE
;
185 if( !intermediateString
) return 0;
186 ret
= GetKeyNameTextW(lParam
,intermediateString
,nSize
);
187 if( ret
== 0 ) { lpString
[0] = 0; return 0; }
189 wstrLen
= wcslen( intermediateString
);
190 ret
= WideCharToMultiByte(CP_ACP
, 0,
191 intermediateString
, wstrLen
,
192 lpString
, nSize
, ".", &defChar
);
194 HeapFree(GetProcessHeap(),0,intermediateString
);
203 GetKeyNameTextW(LONG lParam
,
207 return NtUserGetKeyNameText( lParam
, lpString
, nSize
);
215 GetKeyState(int nVirtKey
)
217 return (SHORT
) NtUserGetKeyState((DWORD
) nVirtKey
);
225 GetKeyboardLayoutNameA(LPSTR pwszKLID
)
227 WCHAR buf
[KL_NAMELENGTH
];
229 if (GetKeyboardLayoutNameW(buf
))
230 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pwszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
239 GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
241 return NtUserGetKeyboardLayoutName( pwszKLID
);
249 GetKeyboardType(int nTypeFlag
)
251 return (int)NtUserCallOneParam((DWORD_PTR
) nTypeFlag
, ONEPARAM_ROUTINE_GETKEYBOARDTYPE
);
259 GetLastInputInfo(PLASTINPUTINFO plii
)
261 return NtUserGetLastInputInfo(plii
);
269 LoadKeyboardLayoutA(LPCSTR pwszKLID
,
272 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
273 strtoul(pwszKLID
, NULL
, 16),
282 LoadKeyboardLayoutW(LPCWSTR pwszKLID
,
285 // Look at revision 25596 to see how it's done in windows.
286 // We will do things our own way. Also be compatible too!
287 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
288 wcstoul(pwszKLID
, NULL
, 16),
297 MapVirtualKeyA(UINT uCode
,
300 return MapVirtualKeyExA( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
308 MapVirtualKeyExA(UINT uCode
,
312 return MapVirtualKeyExW( uCode
, uMapType
, dwhkl
);
320 MapVirtualKeyExW(UINT uCode
,
324 return NtUserMapVirtualKeyEx( uCode
, uMapType
, 0, dwhkl
);
332 MapVirtualKeyW(UINT uCode
,
335 return MapVirtualKeyExW( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
343 OemKeyScan(WORD wOemChar
)
349 MultiByteToWideChar(CP_OEMCP
, 0, (PCSTR
)&wOemChar
, 1, &p
, 1);
351 Scan
= MapVirtualKeyW((Vk
& 0x00ff), 0);
354 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
355 scan code and high word has the shift state.
357 return ((Vk
& 0xff00) << 8) | Scan
;
365 SetDoubleClickTime(UINT uInterval
)
367 return (BOOL
)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME
,
382 return NtUserSwapMouseButton(fSwap
);
390 ToAscii(UINT uVirtKey
,
392 CONST PBYTE lpKeyState
,
396 return ToAsciiEx(uVirtKey
, uScanCode
, lpKeyState
, lpChar
, uFlags
, 0);
404 ToAsciiEx(UINT uVirtKey
,
406 CONST PBYTE lpKeyState
,
414 Ret
= ToUnicodeEx(uVirtKey
, uScanCode
, lpKeyState
, UniChars
, 2, uFlags
, dwhkl
);
415 CharCount
= (Ret
< 0 ? 1 : Ret
);
416 WideCharToMultiByte(CP_ACP
, 0, UniChars
, CharCount
, (LPSTR
) lpChar
, 2, NULL
, NULL
);
426 ToUnicode(UINT wVirtKey
,
428 CONST PBYTE lpKeyState
,
433 return ToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
442 ToUnicodeEx(UINT wVirtKey
,
444 CONST PBYTE lpKeyState
,
450 return NtUserToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
464 if (IsDBCSLeadByte(ch
)) return -1;
466 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
467 return VkKeyScanW(wChar
);
475 VkKeyScanExA(CHAR ch
,
480 if (IsDBCSLeadByte(ch
)) return -1;
482 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
483 return VkKeyScanExW(wChar
, dwhkl
);
491 VkKeyScanExW(WCHAR ch
,
494 return (SHORT
) NtUserVkKeyScanEx(ch
, dwhkl
, TRUE
);
504 return (SHORT
) NtUserVkKeyScanEx(ch
, 0, FALSE
);
517 ULONG_PTR dwExtraInfo
)
523 Input
.type
= INPUT_KEYBOARD
;
525 Input
.ki
.wScan
= bScan
;
526 Input
.ki
.dwFlags
= dwFlags
;
528 Input
.ki
.dwExtraInfo
= dwExtraInfo
;
530 NtUserSendInput(1, &Input
, sizeof(INPUT
));
544 ULONG_PTR dwExtraInfo
)
548 Input
.type
= INPUT_MOUSE
;
551 Input
.mi
.mouseData
= dwData
;
552 Input
.mi
.dwFlags
= dwFlags
;
554 Input
.mi
.dwExtraInfo
= dwExtraInfo
;
556 NtUserSendInput(1, &Input
, sizeof(INPUT
));
560 /***********************************************************************
563 static WORD
get_key_state(void)
567 if (GetSystemMetrics( SM_SWAPBUTTON
))
569 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
570 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
574 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
575 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
577 if (GetAsyncKeyState(VK_MBUTTON
) & 0x80) ret
|= MK_MBUTTON
;
578 if (GetAsyncKeyState(VK_SHIFT
) & 0x80) ret
|= MK_SHIFT
;
579 if (GetAsyncKeyState(VK_CONTROL
) & 0x80) ret
|= MK_CONTROL
;
580 if (GetAsyncKeyState(VK_XBUTTON1
) & 0x80) ret
|= MK_XBUTTON1
;
581 if (GetAsyncKeyState(VK_XBUTTON2
) & 0x80) ret
|= MK_XBUTTON2
;
585 static void CALLBACK
TrackMouseEventProc(HWND hwndUnused
, UINT uMsg
, UINT_PTR idEvent
,
591 INT hoverwidth
= 0, hoverheight
= 0;
593 PUSER32_TRACKINGLIST ptracking_info
;
595 ptracking_info
= & User32GetThreadData()->tracking_info
;
598 hwnd
= WindowFromPoint(pos
);
600 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
601 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
603 /* see if this tracking event is looking for TME_LEAVE and that the */
604 /* mouse has left the window */
605 if (ptracking_info
->tme
.dwFlags
& TME_LEAVE
)
607 if (ptracking_info
->tme
.hwndTrack
!= hwnd
)
609 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
611 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
615 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
618 /* remove the TME_LEAVE flag */
619 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
623 GetClientRect(hwnd
, &client
);
624 MapWindowPoints(hwnd
, NULL
, (LPPOINT
)&client
, 2);
625 if (PtInRect(&client
, pos
))
627 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
629 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
630 /* remove the TME_LEAVE flag */
631 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
636 if (!(ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
))
638 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
639 /* remove the TME_LEAVE flag */
640 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
646 /* see if we are tracking hovering for this hwnd */
647 if (ptracking_info
->tme
.dwFlags
& TME_HOVER
)
649 /* has the cursor moved outside the rectangle centered around pos? */
650 if ((abs(pos
.x
- ptracking_info
->pos
.x
) > (hoverwidth
/ 2.0)) ||
651 (abs(pos
.y
- ptracking_info
->pos
.y
) > (hoverheight
/ 2.0)))
653 /* record this new position as the current position and reset */
654 /* the iHoverTime variable to 0 */
655 ptracking_info
->pos
= pos
;
661 ScreenToClient(hwnd
, &posClient
);
663 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
665 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSEHOVER
,
666 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
670 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSEHOVER
,
671 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
674 /* stop tracking mouse hover */
675 ptracking_info
->tme
.dwFlags
&= ~TME_HOVER
;
679 /* stop the timer if the tracking list is empty */
680 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
682 KillTimer(0, ptracking_info
->timer
);
683 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
688 /***********************************************************************
689 * TrackMouseEvent [USER32]
691 * Requests notification of mouse events
693 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
694 * to the hwnd specified in the ptme structure. After the event message
695 * is posted to the hwnd, the entry in the queue is removed.
697 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
698 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
699 * immediately and the TME_LEAVE flag being ignored.
702 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
715 LPTRACKMOUSEEVENT ptme
)
720 PUSER32_TRACKINGLIST ptracking_info
;
722 TRACE("%lx, %lx, %p, %lx\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
724 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
725 WARN("wrong TRACKMOUSEEVENT size from app\n");
726 SetLastError(ERROR_INVALID_PARAMETER
);
730 ptracking_info
= & User32GetThreadData()->tracking_info
;
732 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
733 if (ptme
->dwFlags
& TME_QUERY
)
735 *ptme
= ptracking_info
->tme
;
736 ptme
->cbSize
= sizeof(TRACKMOUSEEVENT
);
738 return TRUE
; /* return here, TME_QUERY is retrieving information */
741 if (!IsWindow(ptme
->hwndTrack
))
743 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
747 hover_time
= ptme
->dwHoverTime
;
749 /* if HOVER_DEFAULT was specified replace this with the systems current value */
750 if (hover_time
== HOVER_DEFAULT
|| hover_time
== 0)
752 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME
, 0, &hover_time
, 0);
756 hwnd
= WindowFromPoint(pos
);
758 if (ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
))
760 FIXME("Unknown flag(s) %08lx\n", ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
));
763 if (ptme
->dwFlags
& TME_CANCEL
)
765 if (ptracking_info
->tme
.hwndTrack
== ptme
->hwndTrack
)
767 ptracking_info
->tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
769 /* if we aren't tracking on hover or leave remove this entry */
770 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
772 KillTimer(0, ptracking_info
->timer
);
773 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
777 if (ptme
->hwndTrack
== hwnd
)
779 /* Adding new mouse event to the tracking list */
780 ptracking_info
->tme
= *ptme
;
781 ptracking_info
->tme
.dwHoverTime
= hover_time
;
783 /* Initialize HoverInfo variables even if not hover tracking */
784 ptracking_info
->pos
= pos
;
786 if (!ptracking_info
->timer
)
788 ptracking_info
->timer
= SetTimer(0, 0, hover_time
, TrackMouseEventProc
);