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