Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
6.1 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: whotkeys.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * This module contains the core functions of 3.1 window hotkey processing.
  7. *
  8. * History:
  9. * 16-Apr-1992 JimA Created.
  10. \***************************************************************************/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. /***************************************************************************\
  14. * HotKeyToWindow
  15. *
  16. * Scans the hotkey table and returns the pwnd corresponding to the
  17. * given hot key. Returns NULL if no such hot key in the list. Looks at the
  18. * current key state array.
  19. *
  20. * History:
  21. * 04-15-92 JimA Ported from Win3.1 sources.
  22. \***************************************************************************/
  23. PWND HotKeyToWindow(
  24. DWORD key)
  25. {
  26. PHOTKEYSTRUCT phk;
  27. int ckeys;
  28. ckeys = gcHotKey;
  29. if (ckeys == 0)
  30. return 0;
  31. phk = gpHotKeyList;
  32. while (ckeys) {
  33. if (phk->key == key)
  34. return TestWF(phk->spwnd, WFVISIBLE) ? phk->spwnd : NULL;
  35. phk++;
  36. ckeys--;
  37. }
  38. return 0;
  39. }
  40. /***************************************************************************\
  41. * HotKeyHelper
  42. *
  43. * Scans the hot key list and returns a pointer to the entry for the
  44. * window.
  45. *
  46. * History:
  47. * 04-15-92 JimA Ported from Win3.1 sources.
  48. \***************************************************************************/
  49. PHOTKEYSTRUCT HotKeyHelper(
  50. PWND pwnd)
  51. {
  52. PHOTKEYSTRUCT phk;
  53. int count;
  54. count = gcHotKey;
  55. if (gpHotKeyList == NULL)
  56. return 0;
  57. phk = gpHotKeyList;
  58. while (count) {
  59. if (phk->spwnd == pwnd)
  60. return phk;
  61. phk++;
  62. count--;
  63. }
  64. return 0;
  65. }
  66. /***************************************************************************\
  67. * DWP_SetHotKey
  68. *
  69. * Set the hot key for this window. Replace existing hot key, or if new
  70. * key is NULL, delete the entry. Return 2 if key already existed and
  71. * was replaced, 1 if key did not exist and was set, 0 for
  72. * failure, and -1 for invalid hot key.
  73. *
  74. * History:
  75. * 15-Apr-1992 JimA Ported from Win3.1 sources.
  76. \***************************************************************************/
  77. UINT DWP_SetHotKey(
  78. PWND pwnd,
  79. DWORD dwKey)
  80. {
  81. PHOTKEYSTRUCT phk;
  82. BOOL fKeyExists = FALSE;
  83. PWND pwndTemp;
  84. /*
  85. * Filter out invalid hotkeys
  86. */
  87. if (LOBYTE(dwKey) == VK_ESCAPE ||
  88. LOBYTE(dwKey) == VK_SPACE ||
  89. LOBYTE(dwKey) == VK_TAB ||
  90. LOBYTE(dwKey) == VK_PACKET) {
  91. return (UINT)-1;
  92. }
  93. /*
  94. * Don't allow hotkeys for children
  95. */
  96. if (TestWF(pwnd, WFCHILD))
  97. return 0;
  98. /*
  99. * Check if the hot key exists and is assigned to a different pwnd
  100. */
  101. if (dwKey != 0) {
  102. pwndTemp = HotKeyToWindow(dwKey);
  103. if ((pwndTemp != NULL) && (pwndTemp != pwnd))
  104. fKeyExists = TRUE;
  105. }
  106. /*
  107. * Get the hotkey assigned to the window, if any
  108. */
  109. if ((phk = HotKeyHelper(pwnd)) == NULL) {
  110. /*
  111. * Window doesn't exist in the hotkey list and key is being set
  112. * to zero, so just return.
  113. */
  114. if (dwKey == 0)
  115. return 1;
  116. /*
  117. * Allocate and point to a spot for the new hotkey
  118. */
  119. if (gcHotKey >= gcHotKeyAlloc) {
  120. if (gcHotKeyAlloc) {
  121. phk = (PHOTKEYSTRUCT)UserReAllocPool(
  122. (HANDLE)gpHotKeyList,
  123. gcHotKeyAlloc * sizeof(HOTKEYSTRUCT),
  124. (gcHotKey + 1) * sizeof(HOTKEYSTRUCT), TAG_HOTKEY);
  125. if (phk != NULL) {
  126. gpHotKeyList = phk;
  127. phk = &gpHotKeyList[gcHotKey++];
  128. gcHotKeyAlloc = gcHotKey;
  129. } else {
  130. return 0;
  131. }
  132. } else {
  133. UserAssert(gpHotKeyList == NULL);
  134. UserAssert(gcHotKey == 0);
  135. phk = (PHOTKEYSTRUCT)UserAllocPool(sizeof(HOTKEYSTRUCT),
  136. TAG_HOTKEY);
  137. if (phk != NULL) {
  138. gpHotKeyList = phk;
  139. gcHotKey = 1;
  140. gcHotKeyAlloc = 1;
  141. } else {
  142. return 0;
  143. }
  144. }
  145. } else {
  146. phk = &gpHotKeyList[gcHotKey++];
  147. }
  148. }
  149. if (dwKey == 0) {
  150. /*
  151. * The hotkey for this window is being deleted. Copy the last item
  152. * on the list on top of the one being deleted.
  153. */
  154. if (--gcHotKey) {
  155. Lock(&phk->spwnd, gpHotKeyList[gcHotKey].spwnd);
  156. Unlock(&gpHotKeyList[gcHotKey].spwnd);
  157. phk->key = gpHotKeyList[gcHotKey].key;
  158. phk = (PHOTKEYSTRUCT)UserReAllocPool((HANDLE)gpHotKeyList,
  159. gcHotKeyAlloc * sizeof(HOTKEYSTRUCT),
  160. gcHotKey * sizeof(HOTKEYSTRUCT), TAG_HOTKEY);
  161. if (phk != NULL) {
  162. gpHotKeyList = phk;
  163. gcHotKeyAlloc = gcHotKey;
  164. }
  165. } else {
  166. Unlock(&gpHotKeyList[gcHotKey].spwnd);
  167. UserFreePool((HANDLE)gpHotKeyList);
  168. gpHotKeyList = NULL;
  169. gcHotKeyAlloc = 0;
  170. }
  171. } else {
  172. /*
  173. * Add the window and key to the list
  174. */
  175. phk->spwnd = NULL;
  176. Lock(&phk->spwnd, pwnd);
  177. phk->key = dwKey;
  178. }
  179. return fKeyExists ? 2 : 1;
  180. }
  181. /***************************************************************************\
  182. * DWP_GetHotKey
  183. *
  184. *
  185. * History:
  186. * 15-Apr-1992 JimA Created.
  187. \***************************************************************************/
  188. UINT DWP_GetHotKey(
  189. PWND pwnd)
  190. {
  191. PHOTKEYSTRUCT phk;
  192. if ((phk = HotKeyHelper(pwnd)) == NULL)
  193. return 0;
  194. return phk->key;
  195. }