1 /** @file
2   implements menubar interface functions.
3 
4   Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include "EditMenuBar.h"
16 #include "UefiShellDebug1CommandsLib.h"
17 #include "EditStatusBar.h"
18 
19 EDITOR_MENU_ITEM   *MenuItems;
20 MENU_ITEM_FUNCTION *ControlBasedMenuFunctions;
21 UINTN                 NumItems;
22 
23 /**
24   Cleanup function for a menu bar.  frees all allocated memory.
25 **/
26 VOID
MenuBarCleanup(VOID)27 MenuBarCleanup (
28   VOID
29   )
30 {
31   SHELL_FREE_NON_NULL(MenuItems);
32 }
33 
34 /**
35   Initialize the menu bar with the specified items.
36 
37   @param[in] Items              The items to display and their functions.
38 
39   @retval EFI_SUCCESS           The initialization was correct.
40   @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
41 **/
42 EFI_STATUS
MenuBarInit(IN CONST EDITOR_MENU_ITEM * Items)43 MenuBarInit (
44   IN CONST EDITOR_MENU_ITEM  *Items
45   )
46 {
47   CONST EDITOR_MENU_ITEM  *ItemsWalker;
48 
49   for (NumItems = 0, ItemsWalker = Items ; ItemsWalker != NULL && ItemsWalker->Function != NULL ; ItemsWalker++,NumItems++);
50 
51   MenuItems = AllocateZeroPool((NumItems+1) * sizeof(EDITOR_MENU_ITEM));
52   if (MenuItems == NULL) {
53     return EFI_OUT_OF_RESOURCES;
54   }
55   CopyMem(MenuItems, Items, (NumItems+1) * sizeof(EDITOR_MENU_ITEM));
56   return EFI_SUCCESS;
57 }
58 
59 /**
60   Initialize the control hot-key with the specified items.
61 
62   @param[in] Items              The hot-key functions.
63 
64   @retval EFI_SUCCESS           The initialization was correct.
65 **/
66 EFI_STATUS
ControlHotKeyInit(IN MENU_ITEM_FUNCTION * Items)67 ControlHotKeyInit (
68   IN MENU_ITEM_FUNCTION  *Items
69   )
70 {
71   ControlBasedMenuFunctions = Items;
72   return EFI_SUCCESS;
73 }
74 /**
75   Refresh function for the menu bar.
76 
77   @param[in] LastRow            The last printable row.
78   @param[in] LastCol            The last printable column.
79 
80   @retval EFI_SUCCESS           The refresh was successful.
81 **/
82 EFI_STATUS
MenuBarRefresh(IN CONST UINTN LastRow,IN CONST UINTN LastCol)83 MenuBarRefresh (
84   IN CONST UINTN LastRow,
85   IN CONST UINTN LastCol
86   )
87 {
88   EDITOR_MENU_ITEM  *Item;
89   UINTN                 Col;
90   UINTN                 Row;
91   UINTN                 Width;
92   CHAR16                *NameString;
93   CHAR16                *FunctionKeyString;
94 
95   //
96   // variable initialization
97   //
98   Col = 1;
99   Row = (LastRow - 2);
100 
101   //
102   // clear menu bar rows
103   //
104   EditorClearLine (LastRow - 2, LastCol, LastRow);
105   EditorClearLine (LastRow - 1, LastCol, LastRow);
106   EditorClearLine (LastRow    , LastCol, LastRow);
107 
108 
109   //
110   // print out the menu items
111   //
112   for (Item = MenuItems; Item != NULL && Item->Function != NULL; Item++) {
113 
114 
115     NameString = HiiGetString(gShellDebug1HiiHandle, Item->NameToken, NULL);
116 
117 
118     Width             = MAX ((StrLen (NameString) + 6), 20);
119     if (((Col + Width) > LastCol)) {
120       Row++;
121       Col = 1;
122     }
123 
124     FunctionKeyString = HiiGetString(gShellDebug1HiiHandle, Item->FunctionKeyToken, NULL);
125 
126     ShellPrintEx ((INT32)(Col) - 1, (INT32)(Row) - 1, L"%E%s%N  %H%s%N  ", FunctionKeyString, NameString);
127 
128     FreePool (NameString);
129     FreePool (FunctionKeyString);
130     Col += Width;
131   }
132 
133   return EFI_SUCCESS;
134 }
135 
136 /**
137   Function to dispatch the correct function based on a function key (F1...)
138 
139   @param[in] Key                The pressed key.
140 
141   @retval EFI_NOT_FOUND         The key was not a valid function key
142                                 (an error was sent to the status bar).
143   @return The return value from the called dispatch function.
144 **/
145 EFI_STATUS
MenuBarDispatchFunctionKey(IN CONST EFI_INPUT_KEY * Key)146 MenuBarDispatchFunctionKey (
147   IN CONST EFI_INPUT_KEY   *Key
148   )
149 {
150   UINTN                 Index;
151 
152   Index     = Key->ScanCode - SCAN_F1;
153 
154   //
155   // check whether in range
156   //
157   if (Index > (NumItems - 1)) {
158     StatusBarSetStatusString (L"Unknown Command");
159     return EFI_SUCCESS;
160   }
161 
162   return (MenuItems[Index].Function ());
163 }
164 
165 /**
166   Function to dispatch the correct function based on a control-based key (ctrl+o...)
167 
168   @param[in] Key                The pressed key.
169 
170   @retval EFI_NOT_FOUND         The key was not a valid control-based key
171                                 (an error was sent to the status bar).
172   @return EFI_SUCCESS.
173 **/
174 EFI_STATUS
MenuBarDispatchControlHotKey(IN CONST EFI_INPUT_KEY * Key)175 MenuBarDispatchControlHotKey (
176   IN CONST EFI_INPUT_KEY   *Key
177   )
178 {
179 
180   if ((SCAN_CONTROL_Z < Key->UnicodeChar)
181     ||(NULL == ControlBasedMenuFunctions[Key->UnicodeChar]))
182   {
183       return EFI_NOT_FOUND;
184   }
185 
186   ControlBasedMenuFunctions[Key->UnicodeChar]();
187   return EFI_SUCCESS;
188 }
189 
190 
191