Misplacement of checkboxes with empty label fixed.
[wine] / dlls / user / input.c
1 /*
2  * USER Input processing
3  *
4  * Copyright 1993 Bob Amstadt
5  * Copyright 1996 Albrecht Kleine
6  * Copyright 1997 David Faure
7  * Copyright 1998 Morten Welinder
8  * Copyright 1998 Ulrich Weigand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "wine/server.h"
43 #include "user_private.h"
44 #include "winternl.h"
45 #include "wine/debug.h"
46 #include "winerror.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(key);
49 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
50
51
52 /***********************************************************************
53  *           get_key_state
54  */
55 static WORD get_key_state(void)
56 {
57     WORD ret = 0;
58
59     if (GetSystemMetrics( SM_SWAPBUTTON ))
60     {
61         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
62         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
63     }
64     else
65     {
66         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
67         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
68     }
69     if (GetAsyncKeyState(VK_MBUTTON) & 0x80)  ret |= MK_MBUTTON;
70     if (GetAsyncKeyState(VK_SHIFT) & 0x80)    ret |= MK_SHIFT;
71     if (GetAsyncKeyState(VK_CONTROL) & 0x80)  ret |= MK_CONTROL;
72     if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
73     if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
74     return ret;
75 }
76
77
78 /***********************************************************************
79  *              SendInput  (USER32.@)
80  */
81 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
82 {
83     return USER_Driver->pSendInput( count, inputs, size );
84 }
85
86
87 /***********************************************************************
88  *              keybd_event (USER32.@)
89  */
90 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
91                          DWORD dwFlags, ULONG_PTR dwExtraInfo )
92 {
93     INPUT input;
94
95     input.type = INPUT_KEYBOARD;
96     input.u.ki.wVk = bVk;
97     input.u.ki.wScan = bScan;
98     input.u.ki.dwFlags = dwFlags;
99     input.u.ki.time = GetTickCount();
100     input.u.ki.dwExtraInfo = dwExtraInfo;
101     SendInput( 1, &input, sizeof(input) );
102 }
103
104
105 /***********************************************************************
106  *              mouse_event (USER32.@)
107  */
108 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
109                          DWORD dwData, ULONG_PTR dwExtraInfo )
110 {
111     INPUT input;
112
113     input.type = INPUT_MOUSE;
114     input.u.mi.dx = dx;
115     input.u.mi.dy = dy;
116     input.u.mi.mouseData = dwData;
117     input.u.mi.dwFlags = dwFlags;
118     input.u.mi.time = GetCurrentTime();
119     input.u.mi.dwExtraInfo = dwExtraInfo;
120     SendInput( 1, &input, sizeof(input) );
121 }
122
123
124 /***********************************************************************
125  *              GetCursorPos (USER32.@)
126  */
127 BOOL WINAPI GetCursorPos( POINT *pt )
128 {
129     if (!pt) return FALSE;
130     return USER_Driver->pGetCursorPos( pt );
131 }
132
133
134 /***********************************************************************
135  *              GetCursorInfo (USER32.@)
136  */
137 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
138 {
139     if (!pci) return 0;
140     if (get_user_thread_info()->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
141     else pci->flags = 0;
142     GetCursorPos(&pci->ptScreenPos);
143     return 1;
144 }
145
146
147 /***********************************************************************
148  *              SetCursorPos (USER32.@)
149  */
150 BOOL WINAPI SetCursorPos( INT x, INT y )
151 {
152     return USER_Driver->pSetCursorPos( x, y );
153 }
154
155
156 /**********************************************************************
157  *              SetCapture (USER32.@)
158  */
159 HWND WINAPI SetCapture( HWND hwnd )
160 {
161     HWND previous = 0;
162
163     SERVER_START_REQ( set_capture_window )
164     {
165         req->handle = hwnd;
166         req->flags  = 0;
167         if (!wine_server_call_err( req ))
168         {
169             previous = reply->previous;
170             hwnd = reply->full_handle;
171         }
172     }
173     SERVER_END_REQ;
174
175     if (previous && previous != hwnd)
176         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
177     return previous;
178 }
179
180
181 /**********************************************************************
182  *              ReleaseCapture (USER32.@)
183  */
184 BOOL WINAPI ReleaseCapture(void)
185 {
186     BOOL ret;
187     HWND previous = 0;
188
189     SERVER_START_REQ( set_capture_window )
190     {
191         req->handle = 0;
192         req->flags  = 0;
193         if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
194     }
195     SERVER_END_REQ;
196
197     if (previous) SendMessageW( previous, WM_CAPTURECHANGED, 0, 0 );
198     return ret;
199 }
200
201
202 /**********************************************************************
203  *              GetCapture (USER32.@)
204  */
205 HWND WINAPI GetCapture(void)
206 {
207     HWND ret = 0;
208
209     SERVER_START_REQ( get_thread_input )
210     {
211         req->tid = GetCurrentThreadId();
212         if (!wine_server_call_err( req )) ret = reply->capture;
213     }
214     SERVER_END_REQ;
215     return ret;
216 }
217
218
219 /**********************************************************************
220  *              GetAsyncKeyState (USER32.@)
221  *
222  *      Determine if a key is or was pressed.  retval has high-order
223  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
224  * been pressed.
225  */
226 SHORT WINAPI GetAsyncKeyState(INT nKey)
227 {
228     return USER_Driver->pGetAsyncKeyState( nKey );
229 }
230
231
232 /***********************************************************************
233  *              GetQueueStatus (USER32.@)
234  */
235 DWORD WINAPI GetQueueStatus( UINT flags )
236 {
237     DWORD ret = 0;
238
239     /* check for pending X events */
240     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
241
242     SERVER_START_REQ( get_queue_status )
243     {
244         req->clear = 1;
245         wine_server_call( req );
246         ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
247     }
248     SERVER_END_REQ;
249     return ret;
250 }
251
252
253 /***********************************************************************
254  *              GetInputState   (USER32.@)
255  */
256 BOOL WINAPI GetInputState(void)
257 {
258     DWORD ret = 0;
259
260     /* check for pending X events */
261     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
262
263     SERVER_START_REQ( get_queue_status )
264     {
265         req->clear = 0;
266         wine_server_call( req );
267         ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
268     }
269     SERVER_END_REQ;
270     return ret;
271 }
272
273
274 /******************************************************************
275  *              GetLastInputInfo (USER32.@)
276  */
277 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
278 {
279     BOOL ret;
280
281     TRACE("%p\n", plii);
282
283     if (plii->cbSize != sizeof (*plii) )
284     {
285         SetLastError(ERROR_INVALID_PARAMETER);
286         return FALSE;
287     }
288
289     SERVER_START_REQ( get_last_input_time )
290     {
291         ret = !wine_server_call_err( req );
292         if (ret)
293             plii->dwTime = reply->time;
294     }
295     SERVER_END_REQ;
296     return ret;
297 }
298
299
300 /**********************************************************************
301  *              AttachThreadInput (USER32.@)
302  *
303  * Attaches the input processing mechanism of one thread to that of
304  * another thread.
305  */
306 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
307 {
308     BOOL ret;
309
310     SERVER_START_REQ( attach_thread_input )
311     {
312         req->tid_from = from;
313         req->tid_to   = to;
314         req->attach   = attach;
315         ret = !wine_server_call_err( req );
316     }
317     SERVER_END_REQ;
318     return ret;
319 }
320
321
322 /**********************************************************************
323  *              GetKeyState (USER32.@)
324  *
325  * An application calls the GetKeyState function in response to a
326  * keyboard-input message.  This function retrieves the state of the key
327  * at the time the input message was generated.
328  */
329 SHORT WINAPI GetKeyState(INT vkey)
330 {
331     SHORT retval = 0;
332
333     SERVER_START_REQ( get_key_state )
334     {
335         req->tid = GetCurrentThreadId();
336         req->key = vkey;
337         if (!wine_server_call( req )) retval = (signed char)reply->state;
338     }
339     SERVER_END_REQ;
340     TRACE("key (0x%x) -> %x\n", vkey, retval);
341     return retval;
342 }
343
344
345 /**********************************************************************
346  *              GetKeyboardState (USER32.@)
347  */
348 BOOL WINAPI GetKeyboardState( LPBYTE state )
349 {
350     BOOL ret;
351
352     TRACE("(%p)\n", state);
353
354     memset( state, 0, 256 );
355     SERVER_START_REQ( get_key_state )
356     {
357         req->tid = GetCurrentThreadId();
358         req->key = -1;
359         wine_server_set_reply( req, state, 256 );
360         ret = !wine_server_call_err( req );
361     }
362     SERVER_END_REQ;
363     return ret;
364 }
365
366
367 /**********************************************************************
368  *              SetKeyboardState (USER32.@)
369  */
370 BOOL WINAPI SetKeyboardState( LPBYTE state )
371 {
372     BOOL ret;
373
374     TRACE("(%p)\n", state);
375
376     SERVER_START_REQ( set_key_state )
377     {
378         req->tid = GetCurrentThreadId();
379         wine_server_add_data( req, state, 256 );
380         ret = !wine_server_call_err( req );
381     }
382     SERVER_END_REQ;
383     return ret;
384 }
385
386
387 /**********************************************************************
388  *              VkKeyScanA (USER32.@)
389  *
390  * VkKeyScan translates an ANSI character to a virtual-key and shift code
391  * for the current keyboard.
392  * high-order byte yields :
393  *      0       Unshifted
394  *      1       Shift
395  *      2       Ctrl
396  *      3-5     Shift-key combinations that are not used for characters
397  *      6       Ctrl-Alt
398  *      7       Ctrl-Alt-Shift
399  *      I.e. :  Shift = 1, Ctrl = 2, Alt = 4.
400  * FIXME : works ok except for dead chars :
401  * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
402  * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
403  */
404 SHORT WINAPI VkKeyScanA(CHAR cChar)
405 {
406     WCHAR wChar;
407
408     if (IsDBCSLeadByte(cChar)) return -1;
409
410     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
411     return VkKeyScanW(wChar);
412 }
413
414 /******************************************************************************
415  *              VkKeyScanW (USER32.@)
416  */
417 SHORT WINAPI VkKeyScanW(WCHAR cChar)
418 {
419     return VkKeyScanExW(cChar, GetKeyboardLayout(0));
420 }
421
422 /**********************************************************************
423  *              VkKeyScanExA (USER32.@)
424  */
425 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
426 {
427     WCHAR wChar;
428
429     if (IsDBCSLeadByte(cChar)) return -1;
430
431     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
432     return VkKeyScanExW(wChar, dwhkl);
433 }
434
435 /******************************************************************************
436  *              VkKeyScanExW (USER32.@)
437  */
438 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
439 {
440     return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
441 }
442
443 /**********************************************************************
444  *              OemKeyScan (USER32.@)
445  */
446 DWORD WINAPI OemKeyScan(WORD wOemChar)
447 {
448     TRACE("(%d)\n", wOemChar);
449     return wOemChar;
450 }
451
452 /******************************************************************************
453  *              GetKeyboardType (USER32.@)
454  */
455 INT WINAPI GetKeyboardType(INT nTypeFlag)
456 {
457     TRACE_(keyboard)("(%d)\n", nTypeFlag);
458     switch(nTypeFlag)
459     {
460     case 0:      /* Keyboard type */
461         return 4;    /* AT-101 */
462     case 1:      /* Keyboard Subtype */
463         return 0;    /* There are no defined subtypes */
464     case 2:      /* Number of F-keys */
465         return 12;   /* We're doing an 101 for now, so return 12 F-keys */
466     default:
467         WARN_(keyboard)("Unknown type\n");
468         return 0;    /* The book says 0 here, so 0 */
469     }
470 }
471
472 /******************************************************************************
473  *              MapVirtualKeyA (USER32.@)
474  */
475 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
476 {
477     return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
478 }
479
480 /******************************************************************************
481  *              MapVirtualKeyW (USER32.@)
482  */
483 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
484 {
485     return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
486 }
487
488 /******************************************************************************
489  *              MapVirtualKeyExA (USER32.@)
490  */
491 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
492 {
493     return MapVirtualKeyExW(code, maptype, hkl);
494 }
495
496 /******************************************************************************
497  *              MapVirtualKeyExW (USER32.@)
498  */
499 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
500 {
501     TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);
502
503     return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
504 }
505
506 /****************************************************************************
507  *              GetKBCodePage (USER32.@)
508  */
509 UINT WINAPI GetKBCodePage(void)
510 {
511     return GetOEMCP();
512 }
513
514 /***********************************************************************
515  *              GetKeyboardLayout (USER32.@)
516  *
517  *        - device handle for keyboard layout defaulted to
518  *          the language id. This is the way Windows default works.
519  *        - the thread identifier (dwLayout) is also ignored.
520  */
521 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
522 {
523     return USER_Driver->pGetKeyboardLayout(dwLayout);
524 }
525
526 /****************************************************************************
527  *              GetKeyboardLayoutNameA (USER32.@)
528  */
529 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
530 {
531     WCHAR buf[KL_NAMELENGTH];
532
533     if (GetKeyboardLayoutNameW(buf))
534         return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
535     return FALSE;
536 }
537
538 /****************************************************************************
539  *              GetKeyboardLayoutNameW (USER32.@)
540  */
541 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
542 {
543     return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
544 }
545
546 /****************************************************************************
547  *              GetKeyNameTextA (USER32.@)
548  */
549 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
550 {
551     WCHAR buf[256];
552     INT ret;
553
554     if (!GetKeyNameTextW(lParam, buf, 256))
555         return 0;
556     ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
557     if (!ret && nSize)
558     {
559         ret = nSize - 1;
560         lpBuffer[ret] = 0;
561     }
562     return ret;
563 }
564
565 /****************************************************************************
566  *              GetKeyNameTextW (USER32.@)
567  */
568 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
569 {
570     return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
571 }
572
573 /****************************************************************************
574  *              ToUnicode (USER32.@)
575  */
576 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
577                      LPWSTR lpwStr, int size, UINT flags)
578 {
579     return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
580 }
581
582 /****************************************************************************
583  *              ToUnicodeEx (USER32.@)
584  */
585 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
586                        LPWSTR lpwStr, int size, UINT flags, HKL hkl)
587 {
588     return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
589 }
590
591 /****************************************************************************
592  *              ToAscii (USER32.@)
593  */
594 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
595                     LPWORD lpChar, UINT flags )
596 {
597     return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
598 }
599
600 /****************************************************************************
601  *              ToAsciiEx (USER32.@)
602  */
603 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
604                       LPWORD lpChar, UINT flags, HKL dwhkl )
605 {
606     WCHAR uni_chars[2];
607     INT ret, n_ret;
608
609     ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
610     if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
611     else n_ret = ret;
612     WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
613     return ret;
614 }
615
616 /**********************************************************************
617  *              ActivateKeyboardLayout (USER32.@)
618  */
619 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
620 {
621     TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
622
623     return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
624 }
625
626
627 /***********************************************************************
628  *              GetKeyboardLayoutList (USER32.@)
629  *
630  * Return number of values available if either input parm is
631  *  0, per MS documentation.
632  */
633 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
634 {
635     TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
636
637     return USER_Driver->pGetKeyboardLayoutList(nBuff, layouts);
638 }
639
640
641 /***********************************************************************
642  *              RegisterHotKey (USER32.@)
643  */
644 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
645 {
646     FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
647     return TRUE;
648 }
649
650 /***********************************************************************
651  *              UnregisterHotKey (USER32.@)
652  */
653 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
654 {
655     FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
656     return TRUE;
657 }
658
659 /***********************************************************************
660  *              LoadKeyboardLayoutW (USER32.@)
661  */
662 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
663 {
664     TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
665
666     return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
667 }
668
669 /***********************************************************************
670  *              LoadKeyboardLayoutA (USER32.@)
671  */
672 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
673 {
674     HKL ret;
675     UNICODE_STRING pwszKLIDW;
676
677     if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
678     else pwszKLIDW.Buffer = NULL;
679
680     ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
681     RtlFreeUnicodeString(&pwszKLIDW);
682     return ret;
683 }
684
685
686 /***********************************************************************
687  *              UnloadKeyboardLayout (USER32.@)
688  */
689 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
690 {
691     TRACE_(keyboard)("(%p)\n", hkl);
692
693     return USER_Driver->pUnloadKeyboardLayout(hkl);
694 }
695
696 typedef struct __TRACKINGLIST {
697     TRACKMOUSEEVENT tme;
698     POINT pos; /* center of hover rectangle */
699     INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
700 } _TRACKINGLIST;
701
702 static _TRACKINGLIST TrackingList[10];
703 static int iTrackMax = 0;
704 static UINT_PTR timer;
705 static const INT iTimerInterval = 50; /* msec for timer interval */
706
707 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
708     DWORD dwTime)
709 {
710     int i = 0;
711     POINT pos;
712     POINT posClient;
713     HWND hwnd;
714     INT nonclient;
715     INT hoverwidth = 0, hoverheight = 0;
716     RECT client;
717
718     GetCursorPos(&pos);
719     hwnd = WindowFromPoint(pos);
720
721     SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
722     SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
723
724     /* loop through tracking events we are processing */
725     while (i < iTrackMax) {
726         if (TrackingList[i].tme.dwFlags & TME_NONCLIENT) {
727             nonclient = 1;
728         }
729         else {
730             nonclient = 0;
731         }
732
733         /* see if this tracking event is looking for TME_LEAVE and that the */
734         /* mouse has left the window */
735         if (TrackingList[i].tme.dwFlags & TME_LEAVE) {
736             if (TrackingList[i].tme.hwndTrack != hwnd) {
737                 if (nonclient) {
738                     PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
739                 }
740                 else {
741                     PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
742                 }
743
744                 /* remove the TME_LEAVE flag */
745                 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
746             }
747             else {
748                 GetClientRect(hwnd, &client);
749                 MapWindowPoints(hwnd, NULL, (LPPOINT)&client, 2);
750                 if(PtInRect(&client, pos)) {
751                     if (nonclient) {
752                         PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
753                         /* remove the TME_LEAVE flag */
754                         TrackingList[i].tme.dwFlags ^= TME_LEAVE;
755                     }
756                 }
757                 else {
758                     if (!nonclient) {
759                         PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
760                         /* remove the TME_LEAVE flag */
761                         TrackingList[i].tme.dwFlags ^= TME_LEAVE;
762                     }
763                 }
764             }
765         }
766
767         /* see if we are tracking hovering for this hwnd */
768         if(TrackingList[i].tme.dwFlags & TME_HOVER) {
769             /* add the timer interval to the hovering time */
770             TrackingList[i].iHoverTime+=iTimerInterval;
771
772             /* has the cursor moved outside the rectangle centered around pos? */
773             if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
774               || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
775             {
776                 /* record this new position as the current position and reset */
777                 /* the iHoverTime variable to 0 */
778                 TrackingList[i].pos = pos;
779                 TrackingList[i].iHoverTime = 0;
780             }
781
782             /* has the mouse hovered long enough? */
783             if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
784             {
785                 posClient.x = pos.x;
786                 posClient.y = pos.y;
787                 ScreenToClient(hwnd, &posClient);
788                 if (nonclient) {
789                     PostMessageW(TrackingList[i].tme.hwndTrack, WM_NCMOUSEHOVER,
790                                 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
791                 }
792                 else {
793                     PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
794                                 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
795                 }
796
797                 /* stop tracking mouse hover */
798                 TrackingList[i].tme.dwFlags ^= TME_HOVER;
799             }
800         }
801
802         /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
803         if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
804            (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
805             i++;
806         } else { /* remove this entry from the tracking list */
807             TrackingList[i] = TrackingList[--iTrackMax];
808         }
809     }
810
811     /* stop the timer if the tracking list is empty */
812     if(iTrackMax == 0) {
813         KillTimer(0, timer);
814         timer = 0;
815     }
816 }
817
818
819 /***********************************************************************
820  * TrackMouseEvent [USER32]
821  *
822  * Requests notification of mouse events
823  *
824  * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
825  * to the hwnd specified in the ptme structure.  After the event message
826  * is posted to the hwnd, the entry in the queue is removed.
827  *
828  * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
829  * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
830  * immediately and the TME_LEAVE flag being ignored.
831  *
832  * PARAMS
833  *     ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
834  *
835  * RETURNS
836  *     Success: non-zero
837  *     Failure: zero
838  *
839  */
840
841 BOOL WINAPI
842 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
843 {
844     DWORD flags = 0;
845     int i = 0;
846     BOOL cancel = 0, hover = 0, leave = 0, query = 0, nonclient = 0, inclient = 0;
847     HWND hwnd;
848     POINT pos;
849     RECT client;
850
851
852     pos.x = 0;
853     pos.y = 0;
854     SetRectEmpty(&client);
855
856     TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
857
858     if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
859         WARN("wrong TRACKMOUSEEVENT size from app\n");
860         SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
861         return FALSE;
862     }
863
864     flags = ptme->dwFlags;
865
866     /* if HOVER_DEFAULT was specified replace this with the systems current value */
867     if(ptme->dwHoverTime == HOVER_DEFAULT)
868         SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
869
870     GetCursorPos(&pos);
871     hwnd = WindowFromPoint(pos);
872
873     if ( flags & TME_CANCEL ) {
874         flags &= ~ TME_CANCEL;
875         cancel = 1;
876     }
877
878     if ( flags & TME_HOVER  ) {
879         flags &= ~ TME_HOVER;
880         hover = 1;
881     }
882
883     if ( flags & TME_LEAVE ) {
884         flags &= ~ TME_LEAVE;
885         leave = 1;
886     }
887
888     if ( flags & TME_NONCLIENT ) {
889         flags &= ~ TME_NONCLIENT;
890         nonclient = 1;
891     }
892
893     /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
894     if ( flags & TME_QUERY ) {
895         flags &= ~ TME_QUERY;
896         query = 1;
897         i = 0;
898
899         /* Find the tracking list entry with the matching hwnd */
900         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
901             i++;
902         }
903
904         /* hwnd found, fill in the ptme struct */
905         if(i < iTrackMax)
906             *ptme = TrackingList[i].tme;
907         else
908             ptme->dwFlags = 0;
909
910         return TRUE; /* return here, TME_QUERY is retrieving information */
911     }
912
913     if ( flags )
914         FIXME("Unknown flag(s) %08lx\n", flags );
915
916     if(cancel) {
917         /* find a matching hwnd if one exists */
918         i = 0;
919
920         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
921           i++;
922         }
923
924         if(i < iTrackMax) {
925             TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
926
927             /* if we aren't tracking on hover or leave remove this entry */
928             if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
929                  (TrackingList[i].tme.dwFlags & TME_LEAVE)))
930             {
931                 TrackingList[i] = TrackingList[--iTrackMax];
932
933                 if(iTrackMax == 0) {
934                     KillTimer(0, timer);
935                     timer = 0;
936                 }
937             }
938         }
939     } else {
940         /* see if hwndTrack isn't the current window */
941         if(ptme->hwndTrack != hwnd) {
942             if(leave) {
943                 if(nonclient) {
944                     PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
945                 }
946                 else {
947                     PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
948                 }
949             }
950         } else {
951             GetClientRect(ptme->hwndTrack, &client);
952             MapWindowPoints(ptme->hwndTrack, NULL, (LPPOINT)&client, 2);
953             if(PtInRect(&client, pos)) {
954                 inclient = 1;
955             }
956             if(nonclient && inclient) {
957                 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
958                 return TRUE;
959             }
960             else if(!nonclient && !inclient) {
961                 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
962                 return TRUE;
963             }
964
965             /* See if this hwnd is already being tracked and update the tracking flags */
966             for(i = 0; i < iTrackMax; i++) {
967                 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
968                     TrackingList[i].tme.dwFlags = 0;
969
970                     if(hover) {
971                         TrackingList[i].tme.dwFlags |= TME_HOVER;
972                         TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
973                     }
974
975                     if(leave)
976                         TrackingList[i].tme.dwFlags |= TME_LEAVE;
977
978                     if(nonclient)
979                         TrackingList[i].tme.dwFlags |= TME_NONCLIENT;
980
981                     /* reset iHoverTime as per winapi specs */
982                     TrackingList[i].iHoverTime = 0;
983
984                     return TRUE;
985                 }
986             }
987
988             /* if the tracking list is full return FALSE */
989             if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
990                 return FALSE;
991             }
992
993             /* Adding new mouse event to the tracking list */
994             TrackingList[iTrackMax].tme = *ptme;
995
996             /* Initialize HoverInfo variables even if not hover tracking */
997             TrackingList[iTrackMax].iHoverTime = 0;
998             TrackingList[iTrackMax].pos = pos;
999
1000             iTrackMax++;
1001
1002             if (!timer) {
1003                 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1004             }
1005         }
1006     }
1007
1008     return TRUE;
1009 }