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 ******************************************************************/
38 UINT FASTCALL
IntFindSubMenu(HMENU
*hMenu
, HMENU hSubTarget
);
39 HMENU FASTCALL
IntGetSubMenu( HMENU hMenu
, int nPos
);
40 UINT FASTCALL
IntGetMenuState( HMENU hMenu
, UINT uId
, UINT uFlags
);
42 /* FUNCTIONS *****************************************************************/
47 InitAcceleratorImpl(VOID
)
49 return(STATUS_SUCCESS
);
53 CleanupAcceleratorImpl(VOID
)
55 return(STATUS_SUCCESS
);
59 PACCELERATOR_TABLE FASTCALL
UserGetAccelObject(HACCEL hAccel
)
61 PACCELERATOR_TABLE Accel
;
65 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
69 Accel
= UserGetObject(gHandleTable
, hAccel
, otAccel
);
72 EngSetLastError(ERROR_INVALID_ACCEL_HANDLE
);
76 ASSERT(Accel
->head
.cLockObj
>= 0);
84 co_IntTranslateAccelerator(
97 ASSERT_REFS_CO(Window
);
99 hWnd
= Window
->head
.h
;
101 DPRINT("IntTranslateAccelerator(hwnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x)\n",
102 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
109 DPRINT("NtUserGetKeyState(VK_CONTROL) = 0x%x\n",UserGetKeyState(VK_CONTROL
));
110 DPRINT("NtUserGetKeyState(VK_MENU) = 0x%x\n",UserGetKeyState(VK_MENU
));
111 DPRINT("NtUserGetKeyState(VK_SHIFT) = 0x%x\n",UserGetKeyState(VK_SHIFT
));
113 if (UserGetKeyState(VK_CONTROL
) & 0x8000) mask
|= FCONTROL
;
114 if (UserGetKeyState(VK_MENU
) & 0x8000) mask
|= FALT
;
115 if (UserGetKeyState(VK_SHIFT
) & 0x8000) mask
|= FSHIFT
;
116 DPRINT("Mask 0x%x\n",mask
);
118 if (message
== WM_CHAR
|| message
== WM_SYSCHAR
)
120 if ( !(fVirt
& FVIRTKEY
) && (mask
& FALT
) == (fVirt
& FALT
) )
122 DPRINT("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam
) & 0xff);
128 if (fVirt
& FVIRTKEY
)
130 DPRINT("found accel for virt_key %04x (scan %04x)\n",
131 wParam
, 0xff & HIWORD(lParam
));
133 if (mask
== (fVirt
& (FSHIFT
| FCONTROL
| FALT
))) goto found
;
134 DPRINT("but incorrect SHIFT/CTRL/ALT-state mask %x fVirt %x\n",mask
,fVirt
);
138 if (!(lParam
& 0x01000000)) /* no special_key */
140 if ((fVirt
& FALT
) && (lParam
& 0x20000000))
141 { /* ^^ ALT pressed */
142 DPRINT("found accel for Alt-%c\n", LOWORD(wParam
) & 0xff);
149 DPRINT("IntTranslateAccelerator(hwnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x) = FALSE\n",
150 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
155 if (message
== WM_KEYUP
|| message
== WM_SYSKEYUP
)
159 HMENU hMenu
, hSubMenu
, hSysMenu
;
160 UINT uSysStat
= (UINT
)-1, uStat
= (UINT
)-1, nPos
;
161 PMENU_OBJECT MenuObject
, SubMenu
;
164 hMenu
= (Window
->style
& WS_CHILD
) ? 0 : (HMENU
)Window
->IDMenu
;
165 hSysMenu
= Window
->SystemMenu
;
166 MenuObject
= IntGetMenuObject(Window
->SystemMenu
);
168 /* find menu item and ask application to initialize it */
169 /* 1. in the system menu */
173 nPos
= IntGetMenuItemByFlag( MenuObject
,
180 if (MenuItem
&& (nPos
!= (UINT
)-1))
182 hSubMenu
= MenuItem
->hSubMenu
;
184 if (IntGetCaptureWindow())
186 if (Window
->style
& WS_DISABLED
)
190 co_IntSendMessage(hWnd
, WM_INITMENU
, (WPARAM
)hSysMenu
, 0L);
191 if (hSubMenu
!= hSysMenu
)
193 nPos
= IntFindSubMenu(&hSysMenu
, hSubMenu
);
194 DPRINT("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu
, hSubMenu
, nPos
);
195 co_IntSendMessage(hWnd
, WM_INITMENUPOPUP
, (WPARAM
)hSubMenu
, MAKELPARAM(nPos
, TRUE
));
197 uSysStat
= IntGetMenuState(IntGetSubMenu(hSysMenu
, 0), cmd
, MF_BYCOMMAND
);
200 else /* 2. in the window's menu */
202 MenuObject
= IntGetMenuObject(hMenu
);
206 nPos
= IntGetMenuItemByFlag( MenuObject
,
213 if (MenuItem
&& (nPos
!= (UINT
)-1))
215 if (IntGetCaptureWindow())
217 if (Window
->style
& WS_DISABLED
)
221 co_IntSendMessage(hWnd
, WM_INITMENU
, (WPARAM
)hMenu
, 0L);
222 if (hSubMenu
!= hMenu
)
224 nPos
= IntFindSubMenu(&hMenu
, hSubMenu
);
225 DPRINT("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu
, hSubMenu
, nPos
);
226 co_IntSendMessage(hWnd
, WM_INITMENUPOPUP
, (WPARAM
)hSubMenu
, MAKELPARAM(nPos
, FALSE
));
228 uStat
= IntGetMenuState(hMenu
, cmd
, MF_BYCOMMAND
);
236 if (uSysStat
!= (UINT
)-1)
238 if (uSysStat
& (MF_DISABLED
|MF_GRAYED
))
245 if (uStat
!= (UINT
)-1)
247 if (Window
->style
& WS_MINIMIZE
)
251 if (uStat
& (MF_DISABLED
|MF_GRAYED
))
265 if (mesg
== WM_COMMAND
)
267 DPRINT(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd
);
268 co_IntSendMessage(Window
->head
.h
, mesg
, 0x10000 | cmd
, 0L);
270 else if (mesg
== WM_SYSCOMMAND
)
272 DPRINT(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd
);
273 co_IntSendMessage(Window
->head
.h
, mesg
, cmd
, 0x00010000L
);
277 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
278 * #0: unknown (please report!)
279 * #1: for WM_KEYUP,WM_SYSKEYUP
280 * #2: mouse is captured
281 * #3: window is disabled
282 * #4: it's a disabled system menu option
283 * #5: it's a menu option, but window is iconic
284 * #6: it's a menu option, but disabled
286 DPRINT1(", but won't send WM_{SYS}COMMAND, reason is #%d\n", mesg
);
289 DPRINT1(" unknown reason - please report!");
293 DPRINT("IntTranslateAccelerator(hWnd %x, message %x, wParam %x, lParam %x, fVirt %d, key %x, cmd %x) = TRUE\n",
294 Window
->head
.h
, message
, wParam
, lParam
, fVirt
, key
, cmd
);
300 /* SYSCALLS *****************************************************************/
305 NtUserCopyAcceleratorTable(
310 PACCELERATOR_TABLE Accel
;
315 DPRINT("Enter NtUserCopyAcceleratorTable\n");
318 Accel
= UserGetAccelObject(hAccel
);
320 if ((Entries
&& (EntriesCount
< 1)) || ((hAccel
== NULL
) || (!Accel
)))
325 if (Accel
->Count
< EntriesCount
)
326 EntriesCount
= Accel
->Count
;
334 Entries
[Ret
].fVirt
= Accel
->Table
[Ret
].fVirt
& 0x7f;
335 Entries
[Ret
].key
= Accel
->Table
[Ret
].key
;
336 Entries
[Ret
].cmd
= Accel
->Table
[Ret
].cmd
;
338 if(Ret
+ 1 == EntriesCount
) Done
= TRUE
;
341 if((Accel
->Table
[Ret
].fVirt
& 0x80) != 0) Done
= TRUE
;
349 DPRINT("Leave NtUserCopyAcceleratorTable, ret=%i\n",_ret_
);
356 NtUserCreateAcceleratorTable(
360 PACCELERATOR_TABLE Accel
;
363 DECLARE_RETURN(HACCEL
);
365 DPRINT("Enter NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d)\n",
366 Entries
, EntriesCount
);
367 UserEnterExclusive();
369 if (!Entries
|| EntriesCount
< 1)
371 SetLastNtError(STATUS_INVALID_PARAMETER
);
372 RETURN( (HACCEL
) NULL
);
375 Accel
= UserCreateObject(gHandleTable
, NULL
, (PHANDLE
)&hAccel
, otAccel
, sizeof(ACCELERATOR_TABLE
));
379 SetLastNtError(STATUS_NO_MEMORY
);
380 RETURN( (HACCEL
) NULL
);
383 Accel
->Count
= EntriesCount
;
384 if (Accel
->Count
> 0)
386 Accel
->Table
= ExAllocatePoolWithTag(PagedPool
, EntriesCount
* sizeof(ACCEL
), USERTAG_ACCEL
);
387 if (Accel
->Table
== NULL
)
389 UserDereferenceObject(Accel
);
390 UserDeleteObject(hAccel
, otAccel
);
391 SetLastNtError(STATUS_NO_MEMORY
);
392 RETURN( (HACCEL
) NULL
);
395 for (Index
= 0; Index
< EntriesCount
; Index
++)
397 Accel
->Table
[Index
].fVirt
= Entries
[Index
].fVirt
&0x7f;
398 if(Accel
->Table
[Index
].fVirt
& FVIRTKEY
)
400 Accel
->Table
[Index
].key
= Entries
[Index
].key
;
404 RtlMultiByteToUnicodeN(&Accel
->Table
[Index
].key
,
407 (PCSTR
)&Entries
[Index
].key
,
411 Accel
->Table
[Index
].cmd
= Entries
[Index
].cmd
;
414 /* Set the end-of-table terminator. */
415 Accel
->Table
[EntriesCount
- 1].fVirt
|= 0x80;
418 /* FIXME: Save HandleTable in a list somewhere so we can clean it up again */
423 DPRINT("Leave NtUserCreateAcceleratorTable(Entries %p, EntriesCount %d) = %x\n",
424 Entries
, EntriesCount
,_ret_
);
431 NtUserDestroyAcceleratorTable(
434 PACCELERATOR_TABLE Accel
;
435 DECLARE_RETURN(BOOLEAN
);
437 /* FIXME: If the handle table is from a call to LoadAcceleratorTable, decrement it's
438 usage count (and return TRUE).
439 FIXME: Destroy only tables created using CreateAcceleratorTable.
442 DPRINT("NtUserDestroyAcceleratorTable(Table %x)\n", hAccel
);
443 UserEnterExclusive();
445 if (!(Accel
= UserGetAccelObject(hAccel
)))
450 if (Accel
->Table
!= NULL
)
452 ExFreePoolWithTag(Accel
->Table
, USERTAG_ACCEL
);
456 UserDeleteObject(hAccel
, otAccel
);
461 DPRINT("Leave NtUserDestroyAcceleratorTable(Table %x) = %i\n", hAccel
,_ret_
);
468 NtUserTranslateAccelerator(
474 PACCELERATOR_TABLE Accel
= NULL
;
476 USER_REFERENCE_ENTRY AccelRef
, WindowRef
;
479 DPRINT("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p)\n",
480 hWnd
, hAccel
, Message
);
485 SetLastNtError(STATUS_INVALID_PARAMETER
);
489 if ((Message
->message
!= WM_KEYDOWN
) &&
490 (Message
->message
!= WM_SYSKEYDOWN
) &&
491 (Message
->message
!= WM_SYSCHAR
) &&
492 (Message
->message
!= WM_CHAR
))
497 if (!(Accel
= UserGetAccelObject(hAccel
)))
502 UserRefObjectCo(Accel
, &AccelRef
);
504 if (!(Window
= UserGetWindowObject(hWnd
)))
509 UserRefObjectCo(Window
, &WindowRef
);
512 /* FIXME: Associate AcceleratorTable with the current thread */
514 for (i
= 0; i
< Accel
->Count
; i
++)
516 if (co_IntTranslateAccelerator(Window
, Message
->message
, Message
->wParam
, Message
->lParam
,
517 Accel
->Table
[i
].fVirt
, Accel
->Table
[i
].key
,
518 Accel
->Table
[i
].cmd
))
520 DPRINT("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p) = %i end\n",
521 hWnd
, hAccel
, Message
, 1);
524 if (((Accel
->Table
[i
].fVirt
& 0x80) > 0))
533 if (Window
) UserDerefObjectCo(Window
);
534 if (Accel
) UserDerefObjectCo(Accel
);
536 DPRINT("NtUserTranslateAccelerator(hWnd %x, Table %x, Message %p) = %i end\n",
537 hWnd
, hAccel
, Message
, 0);