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.

683 lines
17 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. spmenu.c
  5. Abstract:
  6. Text setup menu support.
  7. Author:
  8. Ted Miller (tedm) 8-September-1993
  9. Revision History:
  10. --*/
  11. #include "spprecmp.h"
  12. #pragma hdrstop
  13. #define MENUITEM_NORMAL 0x00000000
  14. #define MENUITEM_STATIC 0x00000001
  15. typedef struct _MENU_ITEM {
  16. PWSTR Text;
  17. ULONG Flags;
  18. ULONG LeftX;
  19. ULONG_PTR UserData;
  20. ULONG OriginalLength;
  21. } MENU_ITEM, *PMENU_ITEM;
  22. typedef struct _MENU {
  23. PMENU_ITEM Items;
  24. ULONG ItemCount;
  25. ULONG TopY;
  26. ULONG Height;
  27. ULONG LeftX;
  28. ULONG Width;
  29. ULONG TopDisplayedIndex;
  30. BOOLEAN MoreUp,MoreDown;
  31. } MENU, *PMENU;
  32. VOID
  33. pSpMnDrawMenu(
  34. IN PMENU pMenu,
  35. IN ULONG SelectedIndex,
  36. IN BOOLEAN DrawFrame,
  37. IN BOOLEAN IndicateMore,
  38. IN PWSTR MoreUpText,
  39. IN PWSTR MoreDownText
  40. );
  41. PVOID
  42. SpMnCreate(
  43. IN ULONG LeftX,
  44. IN ULONG TopY,
  45. IN ULONG Width,
  46. IN ULONG Height
  47. )
  48. /*++
  49. Routine Description:
  50. Create a new menu by allocating space for a new menu structure
  51. and initializing its fields.
  52. Arguments:
  53. LeftX - supplies the 0-based X coordinate of the leftmost column
  54. of the menu.
  55. TopY - supplies the 0-based Y coordinate of the topmost line
  56. of the menu.
  57. Width - supplies the maximum displayed width for lines in the menu.
  58. Height - supplies the maximum displayed height of the menu.
  59. The menu will scroll if it is too long to fit in the
  60. allotted space.
  61. Return Value:
  62. Menu handle (expressed as a pvoid) of NULL if memory couldn't
  63. be allocated.
  64. --*/
  65. {
  66. PMENU p;
  67. if(p = SpMemAlloc(sizeof(MENU))) {
  68. RtlZeroMemory(p,sizeof(MENU));
  69. if(p->Items = SpMemAlloc(0)) {
  70. p->LeftX = LeftX;
  71. p->TopY = TopY;
  72. p->Width = Width;
  73. p->Height = Height;
  74. } else {
  75. SpMemFree(p);
  76. p = NULL;
  77. }
  78. }
  79. return(p);
  80. }
  81. VOID
  82. SpMnDestroy(
  83. IN PVOID Menu
  84. )
  85. /*++
  86. Routine Description:
  87. Destroy a menu, releasing all memory associated with it.
  88. Arguments:
  89. Menu - supplies handle to menu to destroy.
  90. Return Value:
  91. None.
  92. --*/
  93. {
  94. PMENU pMenu = Menu;
  95. ULONG u;
  96. for(u=0; u<pMenu->ItemCount; u++) {
  97. if(pMenu->Items[u].Text) {
  98. SpMemFree(pMenu->Items[u].Text);
  99. }
  100. }
  101. SpMemFree(pMenu->Items);
  102. SpMemFree(pMenu);
  103. }
  104. BOOLEAN
  105. SpMnAddItem(
  106. IN PVOID Menu,
  107. IN PWSTR Text,
  108. IN ULONG LeftX,
  109. IN ULONG Width,
  110. IN BOOLEAN Selectable,
  111. IN ULONG_PTR UserData
  112. )
  113. /*++
  114. Routine Description:
  115. Add an item to a menu.
  116. Arguments:
  117. Menu - supplies handle to menu to which the item is to be added.
  118. Text - supplies text that comprises the menu selection. This routine
  119. will make a copy of the text.
  120. LeftX - supplies 0-based x coordinate of leftmost character of the text
  121. when it is displayed.
  122. Width - supplies width in characters of the field for this selection.
  123. If this is larger than the number of characters in the text, then
  124. the text is padded to the right with blanks when highlighted.
  125. Selectable - if FALSE, then this text is static -- ie, not selectable.
  126. UserData - supplies a ulong's worth of caller-specific data to be associated
  127. with this menu item.
  128. Return Value:
  129. TRUE if the menu item was added successfully; FALSE if insufficient memory.
  130. --*/
  131. {
  132. PMENU pMenu = Menu;
  133. PMENU_ITEM p;
  134. ULONG TextLen;
  135. ULONG PaddedLen;
  136. PWSTR String;
  137. ULONG u;
  138. ULONG ColumnLen;
  139. ULONG FillLen;
  140. //
  141. // Build a string that is padded to the right with blanks to make
  142. // it the right width.
  143. //
  144. TextLen = wcslen(Text);
  145. PaddedLen = max(TextLen,Width);
  146. ColumnLen = SplangGetColumnCount(Text);
  147. FillLen = (PaddedLen <= ColumnLen) ? 0 : PaddedLen - ColumnLen;
  148. String = SpMemAlloc((PaddedLen+1)*sizeof(WCHAR));
  149. if(!String) {
  150. return(FALSE);
  151. }
  152. wcsncpy(String,Text,TextLen);
  153. for(u=0; u<FillLen; u++) {
  154. String[TextLen+u] = L' ';
  155. }
  156. String[TextLen+u] = 0;
  157. //
  158. // Make space for the item.
  159. //
  160. if((p = SpMemRealloc(pMenu->Items,(pMenu->ItemCount+1) * sizeof(MENU_ITEM))) == NULL) {
  161. SpMemFree(String);
  162. return(FALSE);
  163. }
  164. pMenu->Items = p;
  165. //
  166. // Calculate the address of the new menu item and
  167. // indicate that there is now an additional item in the menu.
  168. //
  169. p = &pMenu->Items[pMenu->ItemCount++];
  170. //
  171. // Set the fields of the menu.
  172. //
  173. p->LeftX = LeftX;
  174. p->UserData = UserData;
  175. p->Flags = Selectable ? MENUITEM_NORMAL : MENUITEM_STATIC;
  176. p->Text = String;
  177. p->OriginalLength = TextLen;
  178. return(TRUE);
  179. }
  180. PWSTR
  181. SpMnGetText(
  182. IN PVOID Menu,
  183. IN ULONG_PTR UserData
  184. )
  185. {
  186. PMENU pMenu = Menu;
  187. ULONG i;
  188. for(i=0; i<pMenu->ItemCount; i++) {
  189. if(pMenu->Items[i].UserData == UserData) {
  190. return(pMenu->Items[i].Text);
  191. }
  192. }
  193. return(NULL);
  194. }
  195. PWSTR
  196. SpMnGetTextDup(
  197. IN PVOID Menu,
  198. IN ULONG_PTR UserData
  199. )
  200. {
  201. PMENU pMenu = Menu;
  202. ULONG i;
  203. PWSTR p;
  204. for(i=0; i<pMenu->ItemCount; i++) {
  205. if(pMenu->Items[i].UserData == UserData) {
  206. //
  207. // Make a duplicate; leave off trailing pad spaces.
  208. //
  209. p = SpMemAlloc((pMenu->Items[i].OriginalLength+1)*sizeof(WCHAR));
  210. wcsncpy(p,pMenu->Items[i].Text,pMenu->Items[i].OriginalLength);
  211. p[pMenu->Items[i].OriginalLength] = 0;
  212. return(p);
  213. }
  214. }
  215. return(NULL);
  216. }
  217. VOID
  218. SpMnDisplay(
  219. IN PVOID Menu,
  220. IN ULONG_PTR UserDataOfHighlightedItem,
  221. IN BOOLEAN Framed,
  222. IN PULONG ValidKeys,
  223. IN PULONG Mnemonics, OPTIONAL
  224. IN PMENU_CALLBACK_ROUTINE NewHighlightCallback, OPTIONAL
  225. IN PMENU_SELECTION_CALLBACK_ROUTINE SelectionCallbackRoutine,OPTIONAL
  226. OUT PULONG KeyPressed,
  227. OUT PULONG_PTR UserDataOfSelectedItem
  228. )
  229. /*++
  230. Routine Description:
  231. Display a menu and accept keystrokes.
  232. When the user presses a menu keystroke (up/down arrow keys), this
  233. routine automatically updates the highlight and calls a callback function
  234. to inform the caller that a new item has the highlight.
  235. When the user presses a keystroke in a list provided by the caller,
  236. this routine returns, providing information about the key pressed and
  237. the item that was highlighted when the key was pressed.
  238. Arguments:
  239. Menu - supplies handle to menu to be displayed.
  240. UserDataOfHighlightedItem - supplies user data of the menu item which
  241. is to receive the highlight initially.
  242. Framed - if TRUE, then draw a single-line bordera around the menu.
  243. ValidKeys - supplies a list of keystrokes that cause this routine to
  244. return to the caller. The list must be terminated with a 0 entry.
  245. NewHighlightCallback - If specified, supplies a routine to be called
  246. when a new item has received the highlight.
  247. SelectionCallbackRoutine - If specified, supplies a routine to be called
  248. when a item in the menu is selected.
  249. KeyPressed - receives the key press that caused this routine to exit.
  250. This will be a valid from the ValidKeys array.
  251. UserDataOfSelectedItem - receives the UserData of the item that had the
  252. highlight when the user pressed a key in ValidKeys.
  253. Return Value:
  254. None.
  255. --*/
  256. {
  257. ULONG ValidMenuKeys[3] = { KEY_UP, KEY_DOWN, 0 };
  258. ULONG key;
  259. PMENU pMenu = Menu;
  260. ULONG SelectedIndex,OldIndex;
  261. BOOLEAN FoundNewItem;
  262. ULONG NewTopDisplayedIndex;
  263. BOOLEAN MustScroll;
  264. PWSTR MoreUpText,MoreDownText;
  265. //
  266. // Get the text for the text that indicate that there are more
  267. // selections.
  268. //
  269. SpFormatMessage(TemporaryBuffer,sizeof(TemporaryBuffer),SP_TEXT_MORE_UP);
  270. MoreUpText = SpDupStringW(TemporaryBuffer);
  271. SpFormatMessage(TemporaryBuffer,sizeof(TemporaryBuffer),SP_TEXT_MORE_DOWN);
  272. MoreDownText = SpDupStringW(TemporaryBuffer);
  273. //
  274. // Locate the seleccted item.
  275. //
  276. for(SelectedIndex=0; SelectedIndex<pMenu->ItemCount; SelectedIndex++) {
  277. if(!(pMenu->Items[SelectedIndex].Flags & MENUITEM_STATIC)
  278. && (pMenu->Items[SelectedIndex].UserData == UserDataOfHighlightedItem))
  279. {
  280. break;
  281. }
  282. }
  283. ASSERT(SelectedIndex < pMenu->ItemCount);
  284. //
  285. // In free builds we bugcheck later on because of it being equal so
  286. // inserting code to handle this situation even if it is a remote case
  287. // but can occur.
  288. //
  289. if (SelectedIndex >= pMenu->ItemCount){
  290. SelectedIndex = pMenu->ItemCount - 1;
  291. }
  292. //
  293. // Make sure the selected item will be visible when we draw the menu.
  294. //
  295. pMenu->TopDisplayedIndex = 0;
  296. while(SelectedIndex >= pMenu->TopDisplayedIndex + pMenu->Height) {
  297. pMenu->TopDisplayedIndex += pMenu->Height;
  298. }
  299. //
  300. // Draw the menu itself.
  301. //
  302. pSpMnDrawMenu(pMenu,SelectedIndex,Framed,Framed,MoreUpText,MoreDownText);
  303. while(1) {
  304. //
  305. // Wait for a valid keypress.
  306. //
  307. key = SpWaitValidKey(ValidKeys,ValidMenuKeys,Mnemonics);
  308. //
  309. // If the key is a menu keystroke, handle it here.
  310. //
  311. FoundNewItem = FALSE;
  312. MustScroll = FALSE;
  313. OldIndex = SelectedIndex;
  314. switch(key) {
  315. case KEY_UP:
  316. //
  317. // Locate the previous selectable item.
  318. //
  319. if(SelectedIndex) {
  320. for(SelectedIndex=SelectedIndex-1; (LONG)SelectedIndex>=0; SelectedIndex--) {
  321. if(!(pMenu->Items[SelectedIndex].Flags & MENUITEM_STATIC)) {
  322. FoundNewItem = TRUE;
  323. break;
  324. }
  325. }
  326. if(FoundNewItem) {
  327. //
  328. // Figure out whether we have to scroll the menu.
  329. //
  330. if(SelectedIndex < pMenu->TopDisplayedIndex) {
  331. MustScroll = TRUE;
  332. NewTopDisplayedIndex = SelectedIndex;
  333. }
  334. } else {
  335. //
  336. // If the first lines are static text, there might be no
  337. // way to get them back on the screen -- the tests above
  338. // fail in this case. So if the user hits the up arrow
  339. // when he's at the topmost selectable item but there are
  340. // static items above him, we'll simply scroll the menu
  341. // so that item #0 is at the top.
  342. //
  343. FoundNewItem = TRUE;
  344. NewTopDisplayedIndex = 0;
  345. MustScroll = TRUE;
  346. SelectedIndex = OldIndex;
  347. }
  348. }
  349. break;
  350. case KEY_DOWN:
  351. //
  352. // Locate the next selectable item.
  353. //
  354. if(SelectedIndex < pMenu->ItemCount) {
  355. for(SelectedIndex=SelectedIndex+1; SelectedIndex < pMenu->ItemCount; SelectedIndex++) {
  356. if(!(pMenu->Items[SelectedIndex].Flags & MENUITEM_STATIC)) {
  357. FoundNewItem = TRUE;
  358. break;
  359. }
  360. }
  361. if(FoundNewItem) {
  362. //
  363. // Figure out whether we have to scroll the menu.
  364. //
  365. if(SelectedIndex >= pMenu->TopDisplayedIndex + pMenu->Height) {
  366. MustScroll = TRUE;
  367. NewTopDisplayedIndex = pMenu->TopDisplayedIndex + SelectedIndex - OldIndex;
  368. }
  369. }
  370. }
  371. break;
  372. default:
  373. if (SelectionCallbackRoutine){
  374. if (!SelectionCallbackRoutine(pMenu->Items[SelectedIndex].UserData,
  375. key)){
  376. continue;
  377. }
  378. }
  379. //
  380. // User pressed a non-menu key.
  381. //
  382. *KeyPressed = key;
  383. *UserDataOfSelectedItem = pMenu->Items[SelectedIndex].UserData;
  384. SpMemFree(MoreUpText);
  385. SpMemFree(MoreDownText);
  386. return;
  387. }
  388. if(FoundNewItem) {
  389. //
  390. // Unhighlight the currently selected item.
  391. //
  392. SpvidDisplayString(
  393. pMenu->Items[OldIndex].Text,
  394. DEFAULT_ATTRIBUTE,
  395. pMenu->Items[OldIndex].LeftX,
  396. pMenu->TopY + OldIndex - pMenu->TopDisplayedIndex
  397. );
  398. //
  399. // Highlight the newly selected item. This may involve
  400. // scrolling the menu.
  401. //
  402. if(MustScroll) {
  403. //
  404. // Redraw the menu so the newly highlighted line is in view.
  405. //
  406. pMenu->TopDisplayedIndex = NewTopDisplayedIndex;
  407. pSpMnDrawMenu(pMenu,SelectedIndex,FALSE,Framed,MoreUpText,MoreDownText);
  408. }
  409. //
  410. // Highlight the newly selected item.
  411. //
  412. SpvidDisplayString(
  413. pMenu->Items[SelectedIndex].Text,
  414. ATT_BG_WHITE | ATT_FG_BLUE,
  415. pMenu->Items[SelectedIndex].LeftX,
  416. pMenu->TopY + SelectedIndex - pMenu->TopDisplayedIndex
  417. );
  418. //
  419. // Inform the caller.
  420. //
  421. if(NewHighlightCallback){
  422. NewHighlightCallback(pMenu->Items[SelectedIndex].UserData);
  423. }
  424. } else {
  425. SelectedIndex = OldIndex;
  426. }
  427. }
  428. }
  429. VOID
  430. pSpMnDrawMenu(
  431. IN PMENU pMenu,
  432. IN ULONG SelectedIndex,
  433. IN BOOLEAN DrawFrame,
  434. IN BOOLEAN IndicateMore,
  435. IN PWSTR MoreUpText,
  436. IN PWSTR MoreDownText
  437. )
  438. {
  439. ULONG item;
  440. BOOLEAN MoreUp,MoreDown,MoreStatusChanged;
  441. //
  442. // Blank out the on-screen menu display.
  443. //
  444. SpvidClearScreenRegion(
  445. pMenu->LeftX,
  446. pMenu->TopY,
  447. pMenu->Width,
  448. pMenu->Height,
  449. DEFAULT_BACKGROUND
  450. );
  451. MoreUp = (BOOLEAN)(pMenu->TopDisplayedIndex > 0);
  452. MoreDown = (BOOLEAN)(pMenu->TopDisplayedIndex + pMenu->Height < pMenu->ItemCount);
  453. //
  454. // We want to force the frame to be drawn if there is a change in whether
  455. // there are more selections above or below us.
  456. //
  457. MoreStatusChanged = (BOOLEAN)( IndicateMore
  458. && ( (pMenu->MoreUp != MoreUp)
  459. || (pMenu->MoreDown != MoreDown)
  460. )
  461. );
  462. if(DrawFrame || MoreStatusChanged) {
  463. ASSERT(pMenu->LeftX);
  464. ASSERT(pMenu->TopY);
  465. SpDrawFrame(
  466. pMenu->LeftX-1,
  467. pMenu->Width+2,
  468. pMenu->TopY-1,
  469. pMenu->Height+2,
  470. DEFAULT_ATTRIBUTE,
  471. FALSE
  472. );
  473. }
  474. //
  475. // Draw each item that is currently on-screen.
  476. //
  477. ASSERT(pMenu->TopDisplayedIndex < pMenu->ItemCount);
  478. for(item = pMenu->TopDisplayedIndex;
  479. item < min(pMenu->TopDisplayedIndex+pMenu->Height,pMenu->ItemCount);
  480. item++)
  481. {
  482. SpvidDisplayString(
  483. pMenu->Items[item].Text,
  484. (UCHAR)((item == SelectedIndex) ? ATT_BG_WHITE | ATT_FG_BLUE : DEFAULT_ATTRIBUTE),
  485. pMenu->Items[item].LeftX,
  486. pMenu->TopY + item - pMenu->TopDisplayedIndex
  487. );
  488. }
  489. //
  490. // If there are more selections above or below us,
  491. // indicate so by placing a small bit of text on the frame.
  492. // Note that the arrow chars can sometimes be DBCS.
  493. //
  494. if(MoreStatusChanged) {
  495. if(MoreUp) {
  496. SpvidDisplayString(
  497. MoreUpText,
  498. DEFAULT_ATTRIBUTE,
  499. pMenu->LeftX + pMenu->Width - SplangGetColumnCount(MoreUpText) - 1,
  500. pMenu->TopY - 1
  501. );
  502. }
  503. if(MoreDown) {
  504. SpvidDisplayString(
  505. MoreDownText,
  506. DEFAULT_ATTRIBUTE,
  507. pMenu->LeftX + pMenu->Width - SplangGetColumnCount(MoreDownText) - 1,
  508. pMenu->TopY + pMenu->Height
  509. );
  510. }
  511. pMenu->MoreUp = MoreUp;
  512. pMenu->MoreDown = MoreDown;
  513. }
  514. }