2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Window accelerator
5 * FILE: subsystems/win32/win32k/ntuser/accelerator.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * 06-06-2001 CSH Created
12 * Copyright 1993 Martin Ayotte
13 * Copyright 1994 Alexandre Julliard
14 * Copyright 1997 Morten Welinder
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 /* INCLUDES ******************************************************************/
35 DBG_DEFAULT_CHANNEL(UserAccel
);
37 UINT FASTCALL
IntFindSubMenu(HMENU
*hMenu
, HMENU hSubTarget
);
38 HMENU FASTCALL
IntGetSubMenu( HMENU hMenu
, int nPos
);
39 UINT FASTCALL
IntGetMenuState( HMENU hMenu
, UINT uId
, UINT uFlags
);
41 /* FUNCTIONS *****************************************************************/
46 InitAcceleratorImpl(VOID
)
48 return(STATUS_SUCCESS
);
52 CleanupAcceleratorImpl(VOID
)
54 return(STATUS_SUCCESS
);
58 PACCELERATOR_TABLE FASTCALL
UserGetAccelObject(HACCEL hAccel
)
60 PACCELERATOR_TABLE Accel
;
64 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
68 Accel
= UserGetObject(gHandleTable
, hAccel
, otAccel
);
71 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
75 ASSERT(Accel
->head
.cLockObj
>= 0);
83 co_IntTranslateAccelerator(
96 ASSERT_REFS_CO(Window
);
98 hWnd
= Window
->head
.h
;
100 TRACE("IntTranslateAccelerator(hwnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x)\n",
101 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
108 TRACE("NtUserGetKeyState(VK_CONTROL) = 0x%x\n",UserGetKeyState(VK_CONTROL
));
109 TRACE("NtUserGetKeyState(VK_MENU) = 0x%x\n",UserGetKeyState(VK_MENU
));
110 TRACE("NtUserGetKeyState(VK_SHIFT) = 0x%x\n",UserGetKeyState(VK_SHIFT
));
112 if (UserGetKeyState(VK_CONTROL
) & 0x8000) mask
|= FCONTROL
;
113 if (UserGetKeyState(VK_MENU
) & 0x8000) mask
|= FALT
;
114 if (UserGetKeyState(VK_SHIFT
) & 0x8000) mask
|= FSHIFT
;
115 TRACE("Mask 0x%x\n",mask
);
117 if (message
== WM_CHAR
|| message
== WM_SYSCHAR
)
119 if ( !(fVirt
& FVIRTKEY
) && (mask
& FALT
) == (fVirt
& FALT
) )
121 TRACE("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam
) & 0xff);
127 if (fVirt
& FVIRTKEY
)
129 TRACE("found accel for virt_key %04x (scan %04x)\n",
130 wParam
, 0xff & HIWORD(lParam
));
132 if (mask
== (fVirt
& (FSHIFT
| FCONTROL
| FALT
))) goto found
;
133 TRACE("but incorrect SHIFT/CTRL/ALT-state mask %x fVirt %x\n",mask
,fVirt
);
137 if (!(lParam
& 0x01000000)) /* no special_key */
139 if ((fVirt
& FALT
) && (lParam
& 0x20000000))
140 { /* ^^ ALT pressed */
141 TRACE("found accel for Alt-%c\n", LOWORD(wParam
) & 0xff);
148 TRACE("IntTranslateAccelerator(hwnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x) = FALSE\n",
149 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
154 if (message
== WM_KEYUP
|| message
== WM_SYSKEYUP
)
158 HMENU hMenu
, hSubMenu
, hSysMenu
;
159 UINT uSysStat
= (UINT
)-1, uStat
= (UINT
)-1, nPos
;
160 PMENU_OBJECT MenuObject
, SubMenu
;
163 hMenu
= (Window
->style
& WS_CHILD
) ? 0 : (HMENU
)Window
->IDMenu
;
164 hSysMenu
= Window
->SystemMenu
;
165 MenuObject
= IntGetMenuObject(Window
->SystemMenu
);
167 /* find menu item and ask application to initialize it */
168 /* 1. in the system menu */
172 nPos
= IntGetMenuItemByFlag( MenuObject
,
179 if (MenuItem
&& (nPos
!= (UINT
)-1))
181 hSubMenu
= MenuItem
->hSubMenu
;
183 if (IntGetCaptureWindow())
185 if (Window
->style
& WS_DISABLED
)
189 co_IntSendMessage(hWnd
, WM_INITMENU
, (WPARAM
)hSysMenu
, 0L);
190 if (hSubMenu
!= hSysMenu
)
192 nPos
= IntFindSubMenu(&hSysMenu
, hSubMenu
);
193 TRACE("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu
, hSubMenu
, nPos
);
194 co_IntSendMessage(hWnd
, WM_INITMENUPOPUP
, (WPARAM
)hSubMenu
, MAKELPARAM(nPos
, TRUE
));
196 uSysStat
= IntGetMenuState(IntGetSubMenu(hSysMenu
, 0), cmd
, MF_BYCOMMAND
);
199 else /* 2. in the window's menu */
201 MenuObject
= IntGetMenuObject(hMenu
);
205 nPos
= IntGetMenuItemByFlag( MenuObject
,
212 if (MenuItem
&& (nPos
!= (UINT
)-1))
214 if (IntGetCaptureWindow())
216 if (Window
->style
& WS_DISABLED
)
220 co_IntSendMessage(hWnd
, WM_INITMENU
, (WPARAM
)hMenu
, 0L);
221 if (hSubMenu
!= hMenu
)
223 nPos
= IntFindSubMenu(&hMenu
, hSubMenu
);
224 TRACE("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu
, hSubMenu
, nPos
);
225 co_IntSendMessage(hWnd
, WM_INITMENUPOPUP
, (WPARAM
)hSubMenu
, MAKELPARAM(nPos
, FALSE
));
227 uStat
= IntGetMenuState(hMenu
, cmd
, MF_BYCOMMAND
);
235 if (uSysStat
!= (UINT
)-1)
237 if (uSysStat
& (MF_DISABLED
|MF_GRAYED
))
244 if (uStat
!= (UINT
)-1)
246 if (Window
->style
& WS_MINIMIZE
)
250 if (uStat
& (MF_DISABLED
|MF_GRAYED
))
264 if (mesg
== WM_COMMAND
)
266 TRACE(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd
);
267 co_IntSendMessage(Window
->head
.h
, mesg
, 0x10000 | cmd
, 0L);
269 else if (mesg
== WM_SYSCOMMAND
)
271 TRACE(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd
);
272 co_IntSendMessage(Window
->head
.h
, mesg
, cmd
, 0x00010000L
);
276 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
277 * #0: unknown (please report!)
278 * #1: for WM_KEYUP,WM_SYSKEYUP
279 * #2: mouse is captured
280 * #3: window is disabled
281 * #4: it's a disabled system menu option
282 * #5: it's a menu option, but window is iconic
283 * #6: it's a menu option, but disabled
285 ERR(", but won't send WM_{SYS}COMMAND, reason is #%d\n", mesg
);
288 ERR(" unknown reason - please report!");
292 TRACE("IntTranslateAccelerator(hWnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x) = TRUE\n",
293 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
299 /* SYSCALLS *****************************************************************/
304 NtUserCopyAcceleratorTable(
309 PACCELERATOR_TABLE Accel
;
314 TRACE("Enter NtUserCopyAcceleratorTable\n");
317 Accel
= UserGetAccelObject(hAccel
);
319 if ((Entries
&& (EntriesCount
< 1)) || ((hAccel
== NULL
) || (!Accel
)))
324 if (Accel
->Count
< EntriesCount
)
325 EntriesCount
= Accel
->Count
;
333 Entries
[Ret
].fVirt
= Accel
->Table
[Ret
].fVirt
& 0x7f;
334 Entries
[Ret
].key
= Accel
->Table
[Ret
].key
;
335 Entries
[Ret
].cmd
= Accel
->Table
[Ret
].cmd
;
337 if(Ret
+ 1 == EntriesCount
) Done
= TRUE
;
340 if((Accel
->Table
[Ret
].fVirt
& 0x80) != 0) Done
= TRUE
;
348 TRACE("Leave NtUserCopyAcceleratorTable, ret=%i\n",_ret_
);
355 NtUserCreateAcceleratorTable(
359 PACCELERATOR_TABLE Accel
;
362 DECLARE_RETURN(HACCEL
);
364 TRACE("Enter NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d)\n",
365 Entries
, EntriesCount
);
366 UserEnterExclusive();
368 if (!Entries
|| EntriesCount
< 1)
370 SetLastNtError(STATUS_INVALID_PARAMETER
);
371 RETURN( (HACCEL
) NULL
);
374 Accel
= UserCreateObject(gHandleTable
, NULL
, (PHANDLE
)&hAccel
, otAccel
, sizeof(ACCELERATOR_TABLE
));
378 SetLastNtError(STATUS_NO_MEMORY
);
379 RETURN( (HACCEL
) NULL
);
382 Accel
->Count
= EntriesCount
;
383 if (Accel
->Count
> 0)
385 Accel
->Table
= ExAllocatePoolWithTag(PagedPool
, EntriesCount
* sizeof(ACCEL
), USERTAG_ACCEL
);
386 if (Accel
->Table
== NULL
)
388 UserDereferenceObject(Accel
);
389 UserDeleteObject(hAccel
, otAccel
);
390 SetLastNtError(STATUS_NO_MEMORY
);
391 RETURN( (HACCEL
) NULL
);
394 for (Index
= 0; Index
< EntriesCount
; Index
++)
396 Accel
->Table
[Index
].fVirt
= Entries
[Index
].fVirt
&0x7f;
397 if(Accel
->Table
[Index
].fVirt
& FVIRTKEY
)
399 Accel
->Table
[Index
].key
= Entries
[Index
].key
;
403 RtlMultiByteToUnicodeN(&Accel
->Table
[Index
].key
,
406 (PCSTR
)&Entries
[Index
].key
,
410 Accel
->Table
[Index
].cmd
= Entries
[Index
].cmd
;
413 /* Set the end-of-table terminator. */
414 Accel
->Table
[EntriesCount
- 1].fVirt
|= 0x80;
417 /* FIXME: Save HandleTable in a list somewhere so we can clean it up again */
422 TRACE("Leave NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d) = %x\n",
423 Entries
, EntriesCount
,_ret_
);
430 NtUserDestroyAcceleratorTable(
433 PACCELERATOR_TABLE Accel
;
434 DECLARE_RETURN(BOOLEAN
);
436 /* FIXME: If the handle table is from a call to LoadAcceleratorTable, decrement it's
437 usage count (and return TRUE).
438 FIXME: Destroy only tables created using CreateAcceleratorTable.
441 TRACE("NtUserDestroyAcceleratorTable(Table %x)\n", hAccel
);
442 UserEnterExclusive();
444 if (!(Accel
= UserGetAccelObject(hAccel
)))
449 if (Accel
->Table
!= NULL
)
451 ExFreePoolWithTag(Accel
->Table
, USERTAG_ACCEL
);
455 UserDeleteObject(hAccel
, otAccel
);
460 TRACE("Leave NtUserDestroyAcceleratorTable(Table %x) = %i\n", hAccel
,_ret_
);
467 NtUserTranslateAccelerator(
473 PACCELERATOR_TABLE Accel
= NULL
;
475 USER_REFERENCE_ENTRY AccelRef
, WindowRef
;
478 TRACE("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p)\n",
479 hWnd
, hAccel
, Message
);
484 SetLastNtError(STATUS_INVALID_PARAMETER
);
488 if ((Message
->message
!= WM_KEYDOWN
) &&
489 (Message
->message
!= WM_SYSKEYDOWN
) &&
490 (Message
->message
!= WM_SYSCHAR
) &&
491 (Message
->message
!= WM_CHAR
))
496 if (!(Accel
= UserGetAccelObject(hAccel
)))
501 UserRefObjectCo(Accel
, &AccelRef
);
503 if (!(Window
= UserGetWindowObject(hWnd
)))
508 UserRefObjectCo(Window
, &WindowRef
);
511 /* FIXME: Associate AcceleratorTable with the current thread */
513 for (i
= 0; i
< Accel
->Count
; i
++)
515 if (co_IntTranslateAccelerator(Window
, Message
->message
, Message
->wParam
, Message
->lParam
,
516 Accel
->Table
[i
].fVirt
, Accel
->Table
[i
].key
,
517 Accel
->Table
[i
].cmd
))
519 TRACE("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p) = %i end\n",
520 hWnd
, hAccel
, Message
, 1);
523 if (((Accel
->Table
[i
].fVirt
& 0x80) > 0))
532 if (Window
) UserDerefObjectCo(Window
);
533 if (Accel
) UserDerefObjectCo(Accel
);
535 TRACE("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p) = %i end\n",
536 hWnd
, hAccel
, Message
, 0);