[FREELDR] Some enhancements for the UI code. (#1763)
[reactos.git] / boot / freeldr / freeldr / ui / tuimenu.c
index 86690af..9b173ac 100644 (file)
 /* FUNCTIONS *****************************************************************/
 
 BOOLEAN
-TuiDisplayMenu(PCSTR MenuHeader,
-               PCSTR MenuFooter,
-               BOOLEAN ShowBootOptions,
-               PCSTR MenuItemList[],
-               ULONG MenuItemCount,
-               ULONG DefaultMenuItem,
-               LONG MenuTimeOut,
-               ULONG* SelectedMenuItem,
-               BOOLEAN CanEscape,
-               UiMenuKeyPressFilterCallback KeyPressFilter)
+TuiDisplayMenu(
+    IN PCSTR MenuHeader,
+    IN PCSTR MenuFooter OPTIONAL,
+    IN BOOLEAN ShowBootOptions,
+    IN PCSTR MenuItemList[],
+    IN ULONG MenuItemCount,
+    IN ULONG DefaultMenuItem,
+    IN LONG MenuTimeOut,
+    OUT PULONG SelectedMenuItem,
+    IN BOOLEAN CanEscape,
+    IN UiMenuKeyPressFilterCallback KeyPressFilter OPTIONAL,
+    IN PVOID Context OPTIONAL)
 {
     UI_MENU_INFO MenuInformation;
     ULONG LastClockSecond;
@@ -39,20 +41,17 @@ TuiDisplayMenu(PCSTR MenuHeader,
     if (!MenuTimeOut && KeyPressFilter && MachConsKbHit())
     {
         //
-        // Get the key
+        // Get the key (get the extended key if needed)
         //
         KeyPress = MachConsGetCh();
-
-        //
-        // Is it extended? Then get the extended key
-        //
-        if (!KeyPress) KeyPress = MachConsGetCh();
+        if (KeyPress == KEY_EXTENDED)
+            KeyPress = MachConsGetCh();
 
         //
         // Call the supplied key filter callback function to see
         // if it is going to handle this keypress.
         //
-        if (KeyPressFilter(KeyPress))
+        if (KeyPressFilter(KeyPress, DefaultMenuItem, Context))
         {
             //
             // It processed the key character, cancel the timeout
@@ -83,6 +82,7 @@ TuiDisplayMenu(PCSTR MenuHeader,
     MenuInformation.MenuItemCount = MenuItemCount;
     MenuInformation.MenuTimeRemaining = MenuTimeOut;
     MenuInformation.SelectedMenuItem = DefaultMenuItem;
+    MenuInformation.Context = Context;
 
     //
     // Calculate the size of the menu box
@@ -107,8 +107,7 @@ TuiDisplayMenu(PCSTR MenuHeader,
         //
         // Process key presses
         //
-        KeyPress = TuiProcessMenuKeyboardEvent(&MenuInformation,
-                                               KeyPressFilter);
+        KeyPress = TuiProcessMenuKeyboardEvent(&MenuInformation, KeyPressFilter);
 
         //
         // Check for ENTER or ESC
@@ -169,7 +168,6 @@ TuiDisplayMenu(PCSTR MenuHeader,
 }
 
 VOID
-NTAPI
 TuiCalcMenuBoxSize(PUI_MENU_INFO MenuInfo)
 {
     ULONG i;
@@ -269,7 +267,6 @@ TuiDrawMenu(PUI_MENU_INFO MenuInfo)
 }
 
 VOID
-NTAPI
 TuiDrawMenuBox(PUI_MENU_INFO MenuInfo)
 {
     CHAR MenuLineText[80], TempString[80];
@@ -395,7 +392,6 @@ TuiDrawMenuBox(PUI_MENU_INFO MenuInfo)
 }
 
 VOID
-NTAPI
 TuiDrawMenuItem(PUI_MENU_INFO MenuInfo,
                 ULONG MenuItemNumber)
 {
@@ -477,7 +473,6 @@ TuiDrawMenuItem(PUI_MENU_INFO MenuInfo,
 }
 
 ULONG
-NTAPI
 TuiProcessMenuKeyboardEvent(PUI_MENU_INFO MenuInfo,
                             UiMenuKeyPressFilterCallback KeyPressFilter)
 {
@@ -487,115 +482,113 @@ TuiProcessMenuKeyboardEvent(PUI_MENU_INFO MenuInfo,
     //
     // Check for a keypress
     //
-    if (MachConsKbHit())
+    if (!MachConsKbHit())
+        return 0; // None, bail out
+
+    //
+    // Check if the timeout is not already complete
+    //
+    if (MenuInfo->MenuTimeRemaining != -1)
     {
         //
-        // Check if the timeout is not already complete
+        // Cancel it and remove it
         //
-        if (MenuInfo->MenuTimeRemaining != -1)
-        {
-            //
-            // Cancel it and remove it
-            //
-            MenuInfo->MenuTimeRemaining = -1;
-            TuiDrawMenuBox(MenuInfo); // FIXME: Remove for minimal UI too
-        }
+        MenuInfo->MenuTimeRemaining = -1;
+        TuiDrawMenuBox(MenuInfo); // FIXME: Remove for minimal UI too
+    }
 
-        //
-        // Get the key
-        //
+    //
+    // Get the key (get the extended key if needed)
+    //
+    KeyEvent = MachConsGetCh();
+    if (KeyEvent == KEY_EXTENDED)
         KeyEvent = MachConsGetCh();
 
+    //
+    // Call the supplied key filter callback function to see
+    // if it is going to handle this keypress.
+    //
+    if (KeyPressFilter &&
+        KeyPressFilter(KeyEvent, MenuInfo->SelectedMenuItem, MenuInfo->Context))
+    {
         //
-        // Is it extended? Then get the extended key
+        // It processed the key character, so redraw and exit
         //
-        if (!KeyEvent) KeyEvent = MachConsGetCh();
+        UiVtbl.DrawMenu(MenuInfo);
+        return 0;
+    }
 
+    //
+    // Process the key
+    //
+    if ((KeyEvent == KEY_UP  ) || (KeyEvent == KEY_DOWN) ||
+        (KeyEvent == KEY_HOME) || (KeyEvent == KEY_END ))
+    {
         //
-        // Call the supplied key filter callback function to see
-        // if it is going to handle this keypress.
+        // Get the current selected item and count
         //
-        if ((KeyPressFilter) && (KeyPressFilter(KeyEvent)))
-        {
-            //
-            // It processed the key character, so redraw and exit
-            //
-            UiVtbl.DrawMenu(MenuInfo);
-            return 0;
-        }
+        Selected = MenuInfo->SelectedMenuItem;
+        Count = MenuInfo->MenuItemCount - 1;
 
         //
-        // Process the key
+        // Check the key and change the selected menu item
         //
-        if ((KeyEvent == KEY_UP  ) || (KeyEvent == KEY_DOWN) ||
-            (KeyEvent == KEY_HOME) || (KeyEvent == KEY_END ))
+        if ((KeyEvent == KEY_UP) && (Selected > 0))
         {
             //
-            // Get the current selected item and count
+            // Deselect previous item and go up
             //
-            Selected = MenuInfo->SelectedMenuItem;
-            Count = MenuInfo->MenuItemCount - 1;
+            MenuInfo->SelectedMenuItem--;
+            TuiDrawMenuItem(MenuInfo, Selected);
+            Selected--;
 
-            //
-            // Check the key and change the selected menu item
-            //
-            if ((KeyEvent == KEY_UP) && (Selected > 0))
+            // Skip past any separators
+            if ((Selected > 0) &&
+                (MenuInfo->MenuItemList[Selected] == NULL))
             {
-                //
-                // Deselect previous item and go up
-                //
                 MenuInfo->SelectedMenuItem--;
-                TuiDrawMenuItem(MenuInfo, Selected);
-                Selected--;
-
-                // Skip past any separators
-                if ((Selected > 0) &&
-                    (MenuInfo->MenuItemList[Selected] == NULL))
-                {
-                    MenuInfo->SelectedMenuItem--;
-                }
-            }
-            else if ( ((KeyEvent == KEY_UP) && (Selected == 0)) ||
-                       (KeyEvent == KEY_END) )
-            {
-                //
-                // Go to the end
-                //
-                MenuInfo->SelectedMenuItem = Count;
-                TuiDrawMenuItem(MenuInfo, Selected);
             }
-            else if ((KeyEvent == KEY_DOWN) && (Selected < Count))
+        }
+        else if ( ((KeyEvent == KEY_UP) && (Selected == 0)) ||
+                   (KeyEvent == KEY_END) )
+        {
+            //
+            // Go to the end
+            //
+            MenuInfo->SelectedMenuItem = Count;
+            TuiDrawMenuItem(MenuInfo, Selected);
+        }
+        else if ((KeyEvent == KEY_DOWN) && (Selected < Count))
+        {
+            //
+            // Deselect previous item and go down
+            //
+            MenuInfo->SelectedMenuItem++;
+            TuiDrawMenuItem(MenuInfo, Selected);
+            Selected++;
+
+            // Skip past any separators
+            if ((Selected < Count) &&
+                (MenuInfo->MenuItemList[Selected] == NULL))
             {
-                //
-                // Deselect previous item and go down
-                //
                 MenuInfo->SelectedMenuItem++;
-                TuiDrawMenuItem(MenuInfo, Selected);
-                Selected++;
-
-                // Skip past any separators
-                if ((Selected < Count) &&
-                    (MenuInfo->MenuItemList[Selected] == NULL))
-                {
-                    MenuInfo->SelectedMenuItem++;
-                }
-            }
-            else if ( ((KeyEvent == KEY_DOWN) && (Selected == Count)) ||
-                       (KeyEvent == KEY_HOME) )
-            {
-                //
-                // Go to the beginning
-                //
-                MenuInfo->SelectedMenuItem = 0;
-                TuiDrawMenuItem(MenuInfo, Selected);
             }
-
+        }
+        else if ( ((KeyEvent == KEY_DOWN) && (Selected == Count)) ||
+                   (KeyEvent == KEY_HOME) )
+        {
             //
-            // Select new item and update video buffer
+            // Go to the beginning
             //
-            TuiDrawMenuItem(MenuInfo, MenuInfo->SelectedMenuItem);
-            VideoCopyOffScreenBufferToVRAM();
+            MenuInfo->SelectedMenuItem = 0;
+            TuiDrawMenuItem(MenuInfo, Selected);
         }
+
+        //
+        // Select new item and update video buffer
+        //
+        TuiDrawMenuItem(MenuInfo, MenuInfo->SelectedMenuItem);
+        VideoCopyOffScreenBufferToVRAM();
     }
 
     //