Implemented VkKeyScan, GetKeyboardTypeand, GetKeyboardLayout and some Wine ports.
[reactos.git] / reactos / lib / user32 / windows / input.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
23 * PURPOSE: Input
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * UPDATE HISTORY:
26 * 09-05-2001 CSH Created
27 */
28
29 /* INCLUDES ******************************************************************/
30
31 #include <user32.h>
32
33 /* GLOBALS *******************************************************************/
34
35
36 typedef struct __TRACKINGLIST {
37 TRACKMOUSEEVENT tme;
38 POINT pos; /* center of hover rectangle */
39 INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
40 } _TRACKINGLIST;
41
42 static _TRACKINGLIST TrackingList[10];
43 static int iTrackMax = 0;
44 static UINT_PTR timer;
45 static const INT iTimerInterval = 50; /* msec for timer interval */
46
47
48 /* FUNCTIONS *****************************************************************/
49
50
51 /*
52 * @implemented
53 */
54 BOOL
55 STDCALL
56 DragDetect(
57 HWND hWnd,
58 POINT pt)
59 {
60 #if 0
61 return NtUserDragDetect(hWnd, pt.x, pt.y);
62 #else
63 MSG msg;
64 RECT rect;
65 POINT tmp;
66 ULONG dx = NtUserGetSystemMetrics(SM_CXDRAG);
67 ULONG dy = NtUserGetSystemMetrics(SM_CYDRAG);
68
69 rect.left = pt.x - dx;
70 rect.right = pt.x + dx;
71 rect.top = pt.y - dy;
72 rect.bottom = pt.y + dy;
73
74 SetCapture(hWnd);
75
76 for (;;)
77 {
78 while (PeekMessageW(&msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
79 {
80 if (msg.message == WM_LBUTTONUP)
81 {
82 ReleaseCapture();
83 return 0;
84 }
85 if (msg.message == WM_MOUSEMOVE)
86 {
87 tmp.x = LOWORD(msg.lParam);
88 tmp.y = HIWORD(msg.lParam);
89 if (!PtInRect(&rect, tmp))
90 {
91 ReleaseCapture();
92 return 1;
93 }
94 }
95 }
96 WaitMessage();
97 }
98 return 0;
99 #endif
100 }
101
102
103 /*
104 * @unimplemented
105 */
106 HKL STDCALL
107 ActivateKeyboardLayout(HKL hkl,
108 UINT Flags)
109 {
110 UNIMPLEMENTED;
111 return (HKL)0;
112 }
113
114
115 /*
116 * @implemented
117 */
118 BOOL STDCALL
119 BlockInput(BOOL fBlockIt)
120 {
121 return NtUserBlockInput(fBlockIt);
122 }
123
124
125 /*
126 * @implemented
127 */
128 BOOL STDCALL
129 EnableWindow(HWND hWnd,
130 BOOL bEnable)
131 {
132 LONG Style = NtUserGetWindowLong(hWnd, GWL_STYLE, FALSE);
133 Style = bEnable ? Style & ~WS_DISABLED : Style | WS_DISABLED;
134 NtUserSetWindowLong(hWnd, GWL_STYLE, Style, FALSE);
135
136 SendMessageA(hWnd, WM_ENABLE, (LPARAM) IsWindowEnabled(hWnd), 0);
137
138 // Return nonzero if it was disabled, or zero if it wasn't:
139 return IsWindowEnabled(hWnd);
140 }
141
142
143 /*
144 * @implemented
145 */
146 SHORT STDCALL
147 GetAsyncKeyState(int vKey)
148 {
149 return (SHORT) NtUserGetAsyncKeyState((DWORD) vKey);
150 }
151
152
153 /*
154 * @implemented
155 */
156 UINT
157 STDCALL
158 GetDoubleClickTime(VOID)
159 {
160 return NtUserGetDoubleClickTime();
161 }
162
163
164 /*
165 * @implemented
166 */
167 HKL STDCALL
168 GetKeyboardLayout(DWORD idThread)
169 {
170 return (HKL)NtUserCallOneParam((DWORD) idThread, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT);
171 }
172
173
174 /*
175 * @implemented
176 */
177 UINT STDCALL
178 GetKBCodePage(VOID)
179 {
180 return GetOEMCP();
181 }
182
183
184 /*
185 * @implemented
186 */
187 int STDCALL
188 GetKeyNameTextA(LONG lParam,
189 LPSTR lpString,
190 int nSize)
191 {
192 LPWSTR intermediateString =
193 HeapAlloc(GetProcessHeap(),0,nSize * sizeof(WCHAR));
194 int ret = 0;
195 UINT wstrLen = 0;
196 BOOL defChar = FALSE;
197
198 if( !intermediateString ) return 0;
199 ret = GetKeyNameTextW(lParam,intermediateString,nSize);
200 if( ret == 0 ) { lpString[0] = 0; return 0; }
201
202 wstrLen = wcslen( intermediateString );
203 ret = WideCharToMultiByte(CP_ACP, 0,
204 intermediateString, wstrLen,
205 lpString, nSize, ".", &defChar );
206 lpString[ret] = 0;
207 HeapFree(GetProcessHeap(),0,intermediateString);
208
209 return ret;
210 }
211
212 /*
213 * @implemented
214 */
215 int STDCALL
216 GetKeyNameTextW(LONG lParam,
217 LPWSTR lpString,
218 int nSize)
219 {
220 return NtUserGetKeyNameText( lParam, lpString, nSize );
221 }
222
223
224 /*
225 * @implemented
226 */
227 SHORT STDCALL
228 GetKeyState(int nVirtKey)
229 {
230 return (SHORT) NtUserGetKeyState((DWORD) nVirtKey);
231 }
232
233
234 /*
235 * @unimplemented
236 */
237 UINT STDCALL
238 GetKeyboardLayoutList(int nBuff,
239 HKL FAR *lpList)
240 {
241 UNIMPLEMENTED;
242 return 0;
243 }
244
245
246 /*
247 * @implemented
248 */
249 BOOL STDCALL
250 GetKeyboardLayoutNameA(LPSTR pwszKLID)
251 {
252 WCHAR buf[KL_NAMELENGTH];
253
254 if (GetKeyboardLayoutNameW(buf))
255 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pwszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
256 return FALSE;
257 }
258
259
260 /*
261 * @unimplemented
262 */
263 BOOL STDCALL
264 GetKeyboardLayoutNameW(LPWSTR pwszKLID)
265 {
266 UNIMPLEMENTED;
267 return FALSE;
268 }
269
270
271 /*
272 * @implemented
273 */
274 BOOL STDCALL
275 GetKeyboardState(PBYTE lpKeyState)
276 {
277
278 return (BOOL) NtUserGetKeyboardState((LPBYTE) lpKeyState);
279 }
280
281
282 /*
283 * @implemented
284 */
285 int STDCALL
286 GetKeyboardType(int nTypeFlag)
287 {
288 return (int)NtUserCallOneParam((DWORD) nTypeFlag, ONEPARAM_ROUTINE_GETKEYBOARDTYPE);
289 }
290
291
292 /*
293 * @unimplemented
294 */
295 BOOL STDCALL
296 GetLastInputInfo(PLASTINPUTINFO plii)
297 {
298 UNIMPLEMENTED;
299 return FALSE;
300 }
301
302
303 /*
304 * @implemented
305 */
306 HKL STDCALL
307 LoadKeyboardLayoutA(LPCSTR pwszKLID,
308 UINT Flags)
309 {
310 HKL ret;
311 UNICODE_STRING pwszKLIDW;
312
313 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
314 else pwszKLIDW.Buffer = NULL;
315
316 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
317 RtlFreeUnicodeString(&pwszKLIDW);
318 return ret;
319
320 }
321
322
323 /*
324 * @unimplemented
325 */
326 HKL STDCALL
327 LoadKeyboardLayoutW(LPCWSTR pwszKLID,
328 UINT Flags)
329 {
330 UNIMPLEMENTED;
331 return (HKL)0;
332 }
333
334
335 /*
336 * @implemented
337 */
338 UINT STDCALL
339 MapVirtualKeyA(UINT uCode,
340 UINT uMapType)
341 {
342 return MapVirtualKeyExA( uCode, uMapType, GetKeyboardLayout( 0 ) );
343 }
344
345
346 /*
347 * @implemented
348 */
349 UINT STDCALL
350 MapVirtualKeyExA(UINT uCode,
351 UINT uMapType,
352 HKL dwhkl)
353 {
354 return MapVirtualKeyExW( uCode, uMapType, dwhkl );
355 }
356
357
358 /*
359 * @implemented
360 */
361 UINT STDCALL
362 MapVirtualKeyExW(UINT uCode,
363 UINT uMapType,
364 HKL dwhkl)
365 {
366 return NtUserMapVirtualKeyEx( uCode, uMapType, 0, dwhkl );
367 }
368
369
370 /*
371 * @implemented
372 */
373 UINT STDCALL
374 MapVirtualKeyW(UINT uCode,
375 UINT uMapType)
376 {
377 return MapVirtualKeyExW( uCode, uMapType, GetKeyboardLayout( 0 ) );
378 }
379
380
381 /*
382 * @implemented
383 */
384 DWORD STDCALL
385 OemKeyScan(WORD wOemChar)
386 {
387 WCHAR p;
388 SHORT Vk;
389 UINT Scan;
390
391 MultiByteToWideChar(CP_OEMCP, 0, (PCSTR)&wOemChar, 1, &p, 1);
392 Vk = VkKeyScanW(p);
393 Scan = MapVirtualKeyW((Vk & 0x00ff), 0);
394 if(!Scan) return -1;
395 /*
396 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
397 scan code and high word has the shift state.
398 */
399 return ((Vk & 0xff00) << 8) | Scan;
400 }
401
402
403 /*
404 * @implemented
405 */
406 BOOL STDCALL
407 RegisterHotKey(HWND hWnd,
408 int id,
409 UINT fsModifiers,
410 UINT vk)
411 {
412 return (BOOL)NtUserRegisterHotKey(hWnd,
413 id,
414 fsModifiers,
415 vk);
416 }
417
418
419 /*
420 * @implemented
421 */
422 BOOL STDCALL
423 SetDoubleClickTime(UINT uInterval)
424 {
425 return (BOOL)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME,
426 uInterval,
427 NULL,
428 0);
429 }
430
431
432 /*
433 * @implemented
434 */
435 HWND STDCALL
436 SetFocus(HWND hWnd)
437 {
438 return NtUserSetFocus(hWnd);
439 }
440
441
442 /*
443 * @implemented
444 */
445 BOOL STDCALL
446 SetKeyboardState(LPBYTE lpKeyState)
447 {
448 return (BOOL) NtUserSetKeyboardState((LPBYTE)lpKeyState);
449 }
450
451
452 /*
453 * @implemented
454 */
455 BOOL
456 STDCALL
457 SwapMouseButton(
458 BOOL fSwap)
459 {
460 return NtUserSwapMouseButton(fSwap);
461 }
462
463
464 /*
465 * @implemented
466 */
467 int STDCALL
468 ToAscii(UINT uVirtKey,
469 UINT uScanCode,
470 CONST PBYTE lpKeyState,
471 LPWORD lpChar,
472 UINT uFlags)
473 {
474 return ToAsciiEx(uVirtKey, uScanCode, lpKeyState, lpChar, uFlags, 0);
475 }
476
477
478 /*
479 * @implemented
480 */
481 int STDCALL
482 ToAsciiEx(UINT uVirtKey,
483 UINT uScanCode,
484 CONST PBYTE lpKeyState,
485 LPWORD lpChar,
486 UINT uFlags,
487 HKL dwhkl)
488 {
489 WCHAR UniChars[2];
490 int Ret, CharCount;
491
492 Ret = ToUnicodeEx(uVirtKey, uScanCode, lpKeyState, UniChars, 2, uFlags, dwhkl);
493 CharCount = (Ret < 0 ? 1 : Ret);
494 WideCharToMultiByte(CP_ACP, 0, UniChars, CharCount, (LPSTR) lpChar, 2, NULL, NULL);
495
496 return Ret;
497 }
498
499
500 /*
501 * @implemented
502 */
503 int STDCALL
504 ToUnicode(UINT wVirtKey,
505 UINT wScanCode,
506 CONST PBYTE lpKeyState,
507 LPWSTR pwszBuff,
508 int cchBuff,
509 UINT wFlags)
510 {
511 return ToUnicodeEx( wVirtKey, wScanCode, lpKeyState, pwszBuff, cchBuff,
512 wFlags, 0 );
513 }
514
515
516 /*
517 * @implemented
518 */
519 int STDCALL
520 ToUnicodeEx(UINT wVirtKey,
521 UINT wScanCode,
522 CONST PBYTE lpKeyState,
523 LPWSTR pwszBuff,
524 int cchBuff,
525 UINT wFlags,
526 HKL dwhkl)
527 {
528 return NtUserToUnicodeEx( wVirtKey, wScanCode, lpKeyState, pwszBuff, cchBuff,
529 wFlags, dwhkl );
530 }
531
532
533 /*
534 * @unimplemented
535 */
536 BOOL STDCALL
537 UnloadKeyboardLayout(HKL hkl)
538 {
539 UNIMPLEMENTED;
540 return FALSE;
541 }
542
543
544 /*
545 * @implemented
546 */
547 BOOL STDCALL
548 UnregisterHotKey(HWND hWnd,
549 int id)
550 {
551 return (BOOL)NtUserUnregisterHotKey(hWnd, id);
552 }
553
554
555 /*
556 * @implemented
557 */
558 SHORT STDCALL
559 VkKeyScanA(CHAR ch)
560 {
561 WCHAR wChar;
562
563 if (IsDBCSLeadByte(ch)) return -1;
564
565 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wChar, 1);
566 return VkKeyScanW(wChar);
567 }
568
569
570 /*
571 * @implemented
572 */
573 SHORT STDCALL
574 VkKeyScanExA(CHAR ch,
575 HKL dwhkl)
576 {
577 WCHAR wChar;
578
579 if (IsDBCSLeadByte(ch)) return -1;
580
581 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wChar, 1);
582 return VkKeyScanExW(wChar, dwhkl);
583 }
584
585
586 /*
587 * @implemented
588 */
589 SHORT STDCALL
590 VkKeyScanExW(WCHAR ch,
591 HKL dwhkl)
592 {
593 return (SHORT) NtUserVkKeyScanEx((DWORD) ch,(DWORD) dwhkl,(DWORD)NULL);
594 }
595
596
597 /*
598 * @implemented
599 */
600 SHORT STDCALL
601 VkKeyScanW(WCHAR ch)
602 {
603 return VkKeyScanExW(ch, GetKeyboardLayout(0));
604 }
605
606
607 /*
608 * @implemented
609 */
610 UINT
611 STDCALL
612 SendInput(
613 UINT nInputs,
614 LPINPUT pInputs,
615 int cbSize)
616 {
617 return NtUserSendInput(nInputs, pInputs, cbSize);
618 }
619
620 /*
621 * Private call for CSRSS
622 */
623 VOID
624 STDCALL
625 PrivateCsrssRegisterPrimitive(VOID)
626 {
627 NtUserCallNoParam(NOPARAM_ROUTINE_REGISTER_PRIMITIVE);
628 }
629
630 /*
631 * Another private call for CSRSS
632 */
633 VOID
634 STDCALL
635 PrivateCsrssAcquireOrReleaseInputOwnership(BOOL Release)
636 {
637 NtUserAcquireOrReleaseInputOwnership(Release);
638 }
639
640 /*
641 * @implemented
642 */
643 VOID
644 STDCALL
645 keybd_event(
646 BYTE bVk,
647 BYTE bScan,
648 DWORD dwFlags,
649 ULONG_PTR dwExtraInfo)
650
651
652 {
653 INPUT Input;
654
655 Input.type = INPUT_KEYBOARD;
656 Input.ki.wVk = bVk;
657 Input.ki.wScan = bScan;
658 Input.ki.dwFlags = dwFlags;
659 Input.ki.time = 0;
660 Input.ki.dwExtraInfo = dwExtraInfo;
661
662 NtUserSendInput(1, &Input, sizeof(INPUT));
663 }
664
665
666 /*
667 * @implemented
668 */
669 VOID
670 STDCALL
671 mouse_event(
672 DWORD dwFlags,
673 DWORD dx,
674 DWORD dy,
675 DWORD dwData,
676 ULONG_PTR dwExtraInfo)
677 {
678 INPUT Input;
679
680 Input.type = INPUT_MOUSE;
681 Input.mi.dx = dx;
682 Input.mi.dy = dy;
683 Input.mi.mouseData = dwData;
684 Input.mi.dwFlags = dwFlags;
685 Input.mi.time = 0;
686 Input.mi.dwExtraInfo = dwExtraInfo;
687
688 NtUserSendInput(1, &Input, sizeof(INPUT));
689 }
690
691
692 /***********************************************************************
693 * get_key_state
694 */
695 static WORD get_key_state(void)
696 {
697 WORD ret = 0;
698
699 if (GetSystemMetrics( SM_SWAPBUTTON ))
700 {
701 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
702 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
703 }
704 else
705 {
706 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
707 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
708 }
709 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
710 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
711 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
712 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
713 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
714 return ret;
715 }
716
717
718 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
719 DWORD dwTime)
720 {
721 int i = 0;
722 POINT pos;
723 POINT posClient;
724 HWND hwnd;
725 INT nonclient;
726 INT hoverwidth = 0, hoverheight = 0;
727 RECT client;
728
729 GetCursorPos(&pos);
730 hwnd = WindowFromPoint(pos);
731
732 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
733 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
734
735 /* loop through tracking events we are processing */
736 while (i < iTrackMax) {
737 if (TrackingList[i].tme.dwFlags & TME_NONCLIENT) {
738 nonclient = 1;
739 }
740 else {
741 nonclient = 0;
742 }
743
744 /* see if this tracking event is looking for TME_LEAVE and that the */
745 /* mouse has left the window */
746 if (TrackingList[i].tme.dwFlags & TME_LEAVE) {
747 if (TrackingList[i].tme.hwndTrack != hwnd) {
748 if (nonclient) {
749 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
750 }
751 else {
752 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
753 }
754
755 /* remove the TME_LEAVE flag */
756 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
757 }
758 else {
759 GetClientRect(hwnd, &client);
760 MapWindowPoints(hwnd, NULL, (LPPOINT)&client, 2);
761 if(PtInRect(&client, pos)) {
762 if (nonclient) {
763 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
764 /* remove the TME_LEAVE flag */
765 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
766 }
767 }
768 else {
769 if (!nonclient) {
770 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
771 /* remove the TME_LEAVE flag */
772 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
773 }
774 }
775 }
776 }
777
778 /* see if we are tracking hovering for this hwnd */
779 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
780 /* add the timer interval to the hovering time */
781 TrackingList[i].iHoverTime+=iTimerInterval;
782
783 /* has the cursor moved outside the rectangle centered around pos? */
784 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
785 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
786 {
787 /* record this new position as the current position and reset */
788 /* the iHoverTime variable to 0 */
789 TrackingList[i].pos = pos;
790 TrackingList[i].iHoverTime = 0;
791 }
792
793 /* has the mouse hovered long enough? */
794 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
795 {
796 posClient.x = pos.x;
797 posClient.y = pos.y;
798 ScreenToClient(hwnd, &posClient);
799 if (nonclient) {
800 PostMessageW(TrackingList[i].tme.hwndTrack, WM_NCMOUSEHOVER,
801 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
802 }
803 else {
804 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
805 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
806 }
807
808 /* stop tracking mouse hover */
809 TrackingList[i].tme.dwFlags ^= TME_HOVER;
810 }
811 }
812
813 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
814 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
815 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
816 i++;
817 } else { /* remove this entry from the tracking list */
818 TrackingList[i] = TrackingList[--iTrackMax];
819 }
820 }
821
822 /* stop the timer if the tracking list is empty */
823 if(iTrackMax == 0) {
824 KillTimer(0, timer);
825 timer = 0;
826 }
827 }
828
829
830 /***********************************************************************
831 * TrackMouseEvent [USER32]
832 *
833 * Requests notification of mouse events
834 *
835 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
836 * to the hwnd specified in the ptme structure. After the event message
837 * is posted to the hwnd, the entry in the queue is removed.
838 *
839 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
840 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
841 * immediately and the TME_LEAVE flag being ignored.
842 *
843 * PARAMS
844 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
845 *
846 * RETURNS
847 * Success: non-zero
848 * Failure: zero
849 *
850 */
851 /*
852 * @unimplemented
853 */
854 BOOL
855 STDCALL
856 TrackMouseEvent(
857 LPTRACKMOUSEEVENT ptme)
858 {
859 DWORD flags = 0;
860 int i = 0;
861 BOOL cancel = 0, hover = 0, leave = 0, query = 0, nonclient = 0, inclient = 0;
862 HWND hwnd;
863 POINT pos;
864 RECT client;
865
866
867 pos.x = 0;
868 pos.y = 0;
869 SetRectEmpty(&client);
870
871 DbgPrint("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
872 UNIMPLEMENTED;
873
874 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
875 DPRINT("wrong TRACKMOUSEEVENT size from app\n");
876 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
877 return FALSE;
878 }
879
880 flags = ptme->dwFlags;
881
882 /* if HOVER_DEFAULT was specified replace this with the systems current value */
883 if(ptme->dwHoverTime == HOVER_DEFAULT)
884 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
885
886 GetCursorPos(&pos);
887 hwnd = WindowFromPoint(pos);
888
889 if ( flags & TME_CANCEL ) {
890 flags &= ~ TME_CANCEL;
891 cancel = 1;
892 }
893
894 if ( flags & TME_HOVER ) {
895 flags &= ~ TME_HOVER;
896 hover = 1;
897 }
898
899 if ( flags & TME_LEAVE ) {
900 flags &= ~ TME_LEAVE;
901 leave = 1;
902 }
903
904 if ( flags & TME_NONCLIENT ) {
905 flags &= ~ TME_NONCLIENT;
906 nonclient = 1;
907 }
908
909 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
910 if ( flags & TME_QUERY ) {
911 flags &= ~ TME_QUERY;
912 query = 1;
913 i = 0;
914
915 /* Find the tracking list entry with the matching hwnd */
916 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
917 i++;
918 }
919
920 /* hwnd found, fill in the ptme struct */
921 if(i < iTrackMax)
922 *ptme = TrackingList[i].tme;
923 else
924 ptme->dwFlags = 0;
925
926 return TRUE; /* return here, TME_QUERY is retrieving information */
927 }
928
929 if ( flags )
930 DPRINT("Unknown flag(s) %08lx\n", flags );
931
932 if(cancel) {
933 /* find a matching hwnd if one exists */
934 i = 0;
935
936 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
937 i++;
938 }
939
940 if(i < iTrackMax) {
941 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
942
943 /* if we aren't tracking on hover or leave remove this entry */
944 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
945 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
946 {
947 TrackingList[i] = TrackingList[--iTrackMax];
948
949 if(iTrackMax == 0) {
950 KillTimer(0, timer);
951 timer = 0;
952 }
953 }
954 }
955 } else {
956 /* see if hwndTrack isn't the current window */
957 if(ptme->hwndTrack != hwnd) {
958 if(leave) {
959 if(nonclient) {
960 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
961 }
962 else {
963 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
964 }
965 }
966 } else {
967 GetClientRect(ptme->hwndTrack, &client);
968 MapWindowPoints(ptme->hwndTrack, NULL, (LPPOINT)&client, 2);
969 if(PtInRect(&client, pos)) {
970 inclient = 1;
971 }
972 if(nonclient && inclient) {
973 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
974 return TRUE;
975 }
976 else if(!nonclient && !inclient) {
977 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
978 return TRUE;
979 }
980
981 /* See if this hwnd is already being tracked and update the tracking flags */
982 for(i = 0; i < iTrackMax; i++) {
983 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
984 TrackingList[i].tme.dwFlags = 0;
985
986 if(hover) {
987 TrackingList[i].tme.dwFlags |= TME_HOVER;
988 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
989 }
990
991 if(leave)
992 TrackingList[i].tme.dwFlags |= TME_LEAVE;
993
994 if(nonclient)
995 TrackingList[i].tme.dwFlags |= TME_NONCLIENT;
996
997 /* reset iHoverTime as per winapi specs */
998 TrackingList[i].iHoverTime = 0;
999
1000 return TRUE;
1001 }
1002 }
1003
1004 /* if the tracking list is full return FALSE */
1005 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1006 return FALSE;
1007 }
1008
1009 /* Adding new mouse event to the tracking list */
1010 TrackingList[iTrackMax].tme = *ptme;
1011
1012 /* Initialize HoverInfo variables even if not hover tracking */
1013 TrackingList[iTrackMax].iHoverTime = 0;
1014 TrackingList[iTrackMax].pos = pos;
1015
1016 iTrackMax++;
1017
1018 if (!timer) {
1019 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1020 }
1021 }
1022 }
1023
1024 return TRUE;
1025 }
1026
1027 /* EOF */