Fix the USER32 DLL initialization and cleanup routines to prevent memory/resource...
[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 * @unimplemented
264 */
265 BOOL STDCALL
266 GetKeyboardLayoutNameW(LPWSTR pwszKLID)
267 {
268 UNIMPLEMENTED;
269 return FALSE;
270 }
271
272
273 /*
274 * @implemented
275 */
276 BOOL STDCALL
277 GetKeyboardState(PBYTE lpKeyState)
278 {
279
280 return (BOOL) NtUserGetKeyboardState((LPBYTE) lpKeyState);
281 }
282
283
284 /*
285 * @implemented
286 */
287 int STDCALL
288 GetKeyboardType(int nTypeFlag)
289 {
290 return (int)NtUserCallOneParam((DWORD) nTypeFlag, ONEPARAM_ROUTINE_GETKEYBOARDTYPE);
291 }
292
293
294 /*
295 * @unimplemented
296 */
297 BOOL STDCALL
298 GetLastInputInfo(PLASTINPUTINFO plii)
299 {
300 UNIMPLEMENTED;
301 return FALSE;
302 }
303
304
305 /*
306 * @implemented
307 */
308 HKL STDCALL
309 LoadKeyboardLayoutA(LPCSTR pwszKLID,
310 UINT Flags)
311 {
312 HKL ret;
313 UNICODE_STRING pwszKLIDW;
314
315 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
316 else pwszKLIDW.Buffer = NULL;
317
318 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
319 RtlFreeUnicodeString(&pwszKLIDW);
320 return ret;
321
322 }
323
324
325 /*
326 * @unimplemented
327 */
328 HKL STDCALL
329 LoadKeyboardLayoutW(LPCWSTR pwszKLID,
330 UINT Flags)
331 {
332 UNIMPLEMENTED;
333 return (HKL)0;
334 }
335
336
337 /*
338 * @implemented
339 */
340 UINT STDCALL
341 MapVirtualKeyA(UINT uCode,
342 UINT uMapType)
343 {
344 return MapVirtualKeyExA( uCode, uMapType, GetKeyboardLayout( 0 ) );
345 }
346
347
348 /*
349 * @implemented
350 */
351 UINT STDCALL
352 MapVirtualKeyExA(UINT uCode,
353 UINT uMapType,
354 HKL dwhkl)
355 {
356 return MapVirtualKeyExW( uCode, uMapType, dwhkl );
357 }
358
359
360 /*
361 * @implemented
362 */
363 UINT STDCALL
364 MapVirtualKeyExW(UINT uCode,
365 UINT uMapType,
366 HKL dwhkl)
367 {
368 return NtUserMapVirtualKeyEx( uCode, uMapType, 0, dwhkl );
369 }
370
371
372 /*
373 * @implemented
374 */
375 UINT STDCALL
376 MapVirtualKeyW(UINT uCode,
377 UINT uMapType)
378 {
379 return MapVirtualKeyExW( uCode, uMapType, GetKeyboardLayout( 0 ) );
380 }
381
382
383 /*
384 * @implemented
385 */
386 DWORD STDCALL
387 OemKeyScan(WORD wOemChar)
388 {
389 WCHAR p;
390 SHORT Vk;
391 UINT Scan;
392
393 MultiByteToWideChar(CP_OEMCP, 0, (PCSTR)&wOemChar, 1, &p, 1);
394 Vk = VkKeyScanW(p);
395 Scan = MapVirtualKeyW((Vk & 0x00ff), 0);
396 if(!Scan) return -1;
397 /*
398 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
399 scan code and high word has the shift state.
400 */
401 return ((Vk & 0xff00) << 8) | Scan;
402 }
403
404
405 /*
406 * @implemented
407 */
408 BOOL STDCALL
409 RegisterHotKey(HWND hWnd,
410 int id,
411 UINT fsModifiers,
412 UINT vk)
413 {
414 return (BOOL)NtUserRegisterHotKey(hWnd,
415 id,
416 fsModifiers,
417 vk);
418 }
419
420
421 /*
422 * @implemented
423 */
424 BOOL STDCALL
425 SetDoubleClickTime(UINT uInterval)
426 {
427 return (BOOL)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME,
428 uInterval,
429 NULL,
430 0);
431 }
432
433
434 /*
435 * @implemented
436 */
437 HWND STDCALL
438 SetFocus(HWND hWnd)
439 {
440 return NtUserSetFocus(hWnd);
441 }
442
443
444 /*
445 * @implemented
446 */
447 BOOL STDCALL
448 SetKeyboardState(LPBYTE lpKeyState)
449 {
450 return (BOOL) NtUserSetKeyboardState((LPBYTE)lpKeyState);
451 }
452
453
454 /*
455 * @implemented
456 */
457 BOOL
458 STDCALL
459 SwapMouseButton(
460 BOOL fSwap)
461 {
462 return NtUserSwapMouseButton(fSwap);
463 }
464
465
466 /*
467 * @implemented
468 */
469 int STDCALL
470 ToAscii(UINT uVirtKey,
471 UINT uScanCode,
472 CONST PBYTE lpKeyState,
473 LPWORD lpChar,
474 UINT uFlags)
475 {
476 return ToAsciiEx(uVirtKey, uScanCode, lpKeyState, lpChar, uFlags, 0);
477 }
478
479
480 /*
481 * @implemented
482 */
483 int STDCALL
484 ToAsciiEx(UINT uVirtKey,
485 UINT uScanCode,
486 CONST PBYTE lpKeyState,
487 LPWORD lpChar,
488 UINT uFlags,
489 HKL dwhkl)
490 {
491 WCHAR UniChars[2];
492 int Ret, CharCount;
493
494 Ret = ToUnicodeEx(uVirtKey, uScanCode, lpKeyState, UniChars, 2, uFlags, dwhkl);
495 CharCount = (Ret < 0 ? 1 : Ret);
496 WideCharToMultiByte(CP_ACP, 0, UniChars, CharCount, (LPSTR) lpChar, 2, NULL, NULL);
497
498 return Ret;
499 }
500
501
502 /*
503 * @implemented
504 */
505 int STDCALL
506 ToUnicode(UINT wVirtKey,
507 UINT wScanCode,
508 CONST PBYTE lpKeyState,
509 LPWSTR pwszBuff,
510 int cchBuff,
511 UINT wFlags)
512 {
513 return ToUnicodeEx( wVirtKey, wScanCode, lpKeyState, pwszBuff, cchBuff,
514 wFlags, 0 );
515 }
516
517
518 /*
519 * @implemented
520 */
521 int STDCALL
522 ToUnicodeEx(UINT wVirtKey,
523 UINT wScanCode,
524 CONST PBYTE lpKeyState,
525 LPWSTR pwszBuff,
526 int cchBuff,
527 UINT wFlags,
528 HKL dwhkl)
529 {
530 return NtUserToUnicodeEx( wVirtKey, wScanCode, lpKeyState, pwszBuff, cchBuff,
531 wFlags, dwhkl );
532 }
533
534
535 /*
536 * @unimplemented
537 */
538 BOOL STDCALL
539 UnloadKeyboardLayout(HKL hkl)
540 {
541 UNIMPLEMENTED;
542 return FALSE;
543 }
544
545
546 /*
547 * @implemented
548 */
549 BOOL STDCALL
550 UnregisterHotKey(HWND hWnd,
551 int id)
552 {
553 return (BOOL)NtUserUnregisterHotKey(hWnd, id);
554 }
555
556
557 /*
558 * @implemented
559 */
560 SHORT STDCALL
561 VkKeyScanA(CHAR ch)
562 {
563 WCHAR wChar;
564
565 if (IsDBCSLeadByte(ch)) return -1;
566
567 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wChar, 1);
568 return VkKeyScanW(wChar);
569 }
570
571
572 /*
573 * @implemented
574 */
575 SHORT STDCALL
576 VkKeyScanExA(CHAR ch,
577 HKL dwhkl)
578 {
579 WCHAR wChar;
580
581 if (IsDBCSLeadByte(ch)) return -1;
582
583 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wChar, 1);
584 return VkKeyScanExW(wChar, dwhkl);
585 }
586
587
588 /*
589 * @implemented
590 */
591 SHORT STDCALL
592 VkKeyScanExW(WCHAR ch,
593 HKL dwhkl)
594 {
595 return (SHORT) NtUserVkKeyScanEx((DWORD) ch,(DWORD) dwhkl,(DWORD)NULL);
596 }
597
598
599 /*
600 * @implemented
601 */
602 SHORT STDCALL
603 VkKeyScanW(WCHAR ch)
604 {
605 return VkKeyScanExW(ch, GetKeyboardLayout(0));
606 }
607
608
609 /*
610 * @implemented
611 */
612 UINT
613 STDCALL
614 SendInput(
615 UINT nInputs,
616 LPINPUT pInputs,
617 int cbSize)
618 {
619 return NtUserSendInput(nInputs, pInputs, cbSize);
620 }
621
622 /*
623 * Private call for CSRSS
624 */
625 VOID
626 STDCALL
627 PrivateCsrssRegisterPrimitive(VOID)
628 {
629 NtUserCallNoParam(NOPARAM_ROUTINE_REGISTER_PRIMITIVE);
630 }
631
632 /*
633 * Another private call for CSRSS
634 */
635 VOID
636 STDCALL
637 PrivateCsrssAcquireOrReleaseInputOwnership(BOOL Release)
638 {
639 NtUserAcquireOrReleaseInputOwnership(Release);
640 }
641
642 /*
643 * @implemented
644 */
645 VOID
646 STDCALL
647 keybd_event(
648 BYTE bVk,
649 BYTE bScan,
650 DWORD dwFlags,
651 ULONG_PTR dwExtraInfo)
652
653
654 {
655 INPUT Input;
656
657 Input.type = INPUT_KEYBOARD;
658 Input.ki.wVk = bVk;
659 Input.ki.wScan = bScan;
660 Input.ki.dwFlags = dwFlags;
661 Input.ki.time = 0;
662 Input.ki.dwExtraInfo = dwExtraInfo;
663
664 NtUserSendInput(1, &Input, sizeof(INPUT));
665 }
666
667
668 /*
669 * @implemented
670 */
671 VOID
672 STDCALL
673 mouse_event(
674 DWORD dwFlags,
675 DWORD dx,
676 DWORD dy,
677 DWORD dwData,
678 ULONG_PTR dwExtraInfo)
679 {
680 INPUT Input;
681
682 Input.type = INPUT_MOUSE;
683 Input.mi.dx = dx;
684 Input.mi.dy = dy;
685 Input.mi.mouseData = dwData;
686 Input.mi.dwFlags = dwFlags;
687 Input.mi.time = 0;
688 Input.mi.dwExtraInfo = dwExtraInfo;
689
690 NtUserSendInput(1, &Input, sizeof(INPUT));
691 }
692
693
694 /***********************************************************************
695 * get_key_state
696 */
697 static WORD get_key_state(void)
698 {
699 WORD ret = 0;
700
701 if (GetSystemMetrics( SM_SWAPBUTTON ))
702 {
703 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
704 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
705 }
706 else
707 {
708 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
709 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
710 }
711 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
712 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
713 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
714 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
715 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
716 return ret;
717 }
718
719
720 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
721 DWORD dwTime)
722 {
723 int i = 0;
724 POINT pos;
725 POINT posClient;
726 HWND hwnd;
727 INT nonclient;
728 INT hoverwidth = 0, hoverheight = 0;
729 RECT client;
730
731 GetCursorPos(&pos);
732 hwnd = WindowFromPoint(pos);
733
734 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
735 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
736
737 /* loop through tracking events we are processing */
738 while (i < iTrackMax) {
739 if (TrackingList[i].tme.dwFlags & TME_NONCLIENT) {
740 nonclient = 1;
741 }
742 else {
743 nonclient = 0;
744 }
745
746 /* see if this tracking event is looking for TME_LEAVE and that the */
747 /* mouse has left the window */
748 if (TrackingList[i].tme.dwFlags & TME_LEAVE) {
749 if (TrackingList[i].tme.hwndTrack != hwnd) {
750 if (nonclient) {
751 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
752 }
753 else {
754 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
755 }
756
757 /* remove the TME_LEAVE flag */
758 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
759 }
760 else {
761 GetClientRect(hwnd, &client);
762 MapWindowPoints(hwnd, NULL, (LPPOINT)&client, 2);
763 if(PtInRect(&client, pos)) {
764 if (nonclient) {
765 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
766 /* remove the TME_LEAVE flag */
767 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
768 }
769 }
770 else {
771 if (!nonclient) {
772 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
773 /* remove the TME_LEAVE flag */
774 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
775 }
776 }
777 }
778 }
779
780 /* see if we are tracking hovering for this hwnd */
781 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
782 /* add the timer interval to the hovering time */
783 TrackingList[i].iHoverTime+=iTimerInterval;
784
785 /* has the cursor moved outside the rectangle centered around pos? */
786 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
787 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
788 {
789 /* record this new position as the current position and reset */
790 /* the iHoverTime variable to 0 */
791 TrackingList[i].pos = pos;
792 TrackingList[i].iHoverTime = 0;
793 }
794
795 /* has the mouse hovered long enough? */
796 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
797 {
798 posClient.x = pos.x;
799 posClient.y = pos.y;
800 ScreenToClient(hwnd, &posClient);
801 if (nonclient) {
802 PostMessageW(TrackingList[i].tme.hwndTrack, WM_NCMOUSEHOVER,
803 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
804 }
805 else {
806 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
807 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
808 }
809
810 /* stop tracking mouse hover */
811 TrackingList[i].tme.dwFlags ^= TME_HOVER;
812 }
813 }
814
815 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
816 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
817 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
818 i++;
819 } else { /* remove this entry from the tracking list */
820 TrackingList[i] = TrackingList[--iTrackMax];
821 }
822 }
823
824 /* stop the timer if the tracking list is empty */
825 if(iTrackMax == 0) {
826 KillTimer(0, timer);
827 timer = 0;
828 }
829 }
830
831
832 /***********************************************************************
833 * TrackMouseEvent [USER32]
834 *
835 * Requests notification of mouse events
836 *
837 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
838 * to the hwnd specified in the ptme structure. After the event message
839 * is posted to the hwnd, the entry in the queue is removed.
840 *
841 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
842 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
843 * immediately and the TME_LEAVE flag being ignored.
844 *
845 * PARAMS
846 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
847 *
848 * RETURNS
849 * Success: non-zero
850 * Failure: zero
851 *
852 */
853 /*
854 * @unimplemented
855 */
856 BOOL
857 STDCALL
858 TrackMouseEvent(
859 LPTRACKMOUSEEVENT ptme)
860 {
861 DWORD flags = 0;
862 int i = 0;
863 BOOL cancel = 0, hover = 0, leave = 0, query = 0, nonclient = 0, inclient = 0;
864 HWND hwnd;
865 POINT pos;
866 RECT client;
867
868
869 pos.x = 0;
870 pos.y = 0;
871 SetRectEmpty(&client);
872
873 DPRINT("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
874
875 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
876 DPRINT("wrong TRACKMOUSEEVENT size from app\n");
877 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
878 return FALSE;
879 }
880
881 flags = ptme->dwFlags;
882
883 /* if HOVER_DEFAULT was specified replace this with the systems current value */
884 if(ptme->dwHoverTime == HOVER_DEFAULT)
885 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
886
887 GetCursorPos(&pos);
888 hwnd = WindowFromPoint(pos);
889
890 if ( flags & TME_CANCEL ) {
891 flags &= ~ TME_CANCEL;
892 cancel = 1;
893 }
894
895 if ( flags & TME_HOVER ) {
896 flags &= ~ TME_HOVER;
897 hover = 1;
898 }
899
900 if ( flags & TME_LEAVE ) {
901 flags &= ~ TME_LEAVE;
902 leave = 1;
903 }
904
905 if ( flags & TME_NONCLIENT ) {
906 flags &= ~ TME_NONCLIENT;
907 nonclient = 1;
908 }
909
910 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
911 if ( flags & TME_QUERY ) {
912 flags &= ~ TME_QUERY;
913 query = 1;
914 i = 0;
915
916 /* Find the tracking list entry with the matching hwnd */
917 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
918 i++;
919 }
920
921 /* hwnd found, fill in the ptme struct */
922 if(i < iTrackMax)
923 *ptme = TrackingList[i].tme;
924 else
925 ptme->dwFlags = 0;
926
927 return TRUE; /* return here, TME_QUERY is retrieving information */
928 }
929
930 if ( flags )
931 DPRINT("Unknown flag(s) %08lx\n", flags );
932
933 if(cancel) {
934 /* find a matching hwnd if one exists */
935 i = 0;
936
937 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
938 i++;
939 }
940
941 if(i < iTrackMax) {
942 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
943
944 /* if we aren't tracking on hover or leave remove this entry */
945 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
946 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
947 {
948 TrackingList[i] = TrackingList[--iTrackMax];
949
950 if(iTrackMax == 0) {
951 KillTimer(0, timer);
952 timer = 0;
953 }
954 }
955 }
956 } else {
957 /* see if hwndTrack isn't the current window */
958 if(ptme->hwndTrack != hwnd) {
959 if(leave) {
960 if(nonclient) {
961 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
962 }
963 else {
964 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
965 }
966 }
967 } else {
968 GetClientRect(ptme->hwndTrack, &client);
969 MapWindowPoints(ptme->hwndTrack, NULL, (LPPOINT)&client, 2);
970 if(PtInRect(&client, pos)) {
971 inclient = 1;
972 }
973 if(nonclient && inclient) {
974 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
975 return TRUE;
976 }
977 else if(!nonclient && !inclient) {
978 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
979 return TRUE;
980 }
981
982 /* See if this hwnd is already being tracked and update the tracking flags */
983 for(i = 0; i < iTrackMax; i++) {
984 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
985 TrackingList[i].tme.dwFlags = 0;
986
987 if(hover) {
988 TrackingList[i].tme.dwFlags |= TME_HOVER;
989 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
990 }
991
992 if(leave)
993 TrackingList[i].tme.dwFlags |= TME_LEAVE;
994
995 if(nonclient)
996 TrackingList[i].tme.dwFlags |= TME_NONCLIENT;
997
998 /* reset iHoverTime as per winapi specs */
999 TrackingList[i].iHoverTime = 0;
1000
1001 return TRUE;
1002 }
1003 }
1004
1005 /* if the tracking list is full return FALSE */
1006 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1007 return FALSE;
1008 }
1009
1010 /* Adding new mouse event to the tracking list */
1011 TrackingList[iTrackMax].tme = *ptme;
1012
1013 /* Initialize HoverInfo variables even if not hover tracking */
1014 TrackingList[iTrackMax].iHoverTime = 0;
1015 TrackingList[iTrackMax].pos = pos;
1016
1017 iTrackMax++;
1018
1019 if (!timer) {
1020 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1021 }
1022 }
1023 }
1024
1025 return TRUE;
1026 }
1027
1028 /* EOF */