[FREELDR] Minor code improvements + 1 bug-fix.
[reactos.git] / boot / freeldr / freeldr / ui / tui.c
index c3e4229..d8fa402 100644 (file)
@@ -22,7 +22,7 @@
 #define TAG_TUI_SCREENBUFFER 'SiuT'
 #define TAG_TAG_TUI_PALETTE 'PiuT'
 
-PVOID    TextVideoBuffer = NULL;
+PVOID TextVideoBuffer = NULL;
 
 /*
  * TuiPrintf()
@@ -52,6 +52,7 @@ int TuiPrintf(const char *Format, ...)
 BOOLEAN TuiInitialize(VOID)
 {
     MachVideoHideShowTextCursor(FALSE);
+    MachVideoSetTextCursorPosition(0, 0);
     MachVideoClearScreen(ATTR(COLOR_GRAY, COLOR_BLACK));
 
     TextVideoBuffer = VideoAllocateOffScreenBuffer();
@@ -75,6 +76,7 @@ VOID TuiUnInitialize(VOID)
     }
 
     MachVideoClearScreen(ATTR(COLOR_GRAY, COLOR_BLACK));
+    MachVideoSetTextCursorPosition(0, 0);
     MachVideoHideShowTextCursor(TRUE);
 }
 
@@ -108,7 +110,7 @@ VOID TuiDrawBackdrop(VOID)
     //
     TuiDrawText(2,
             1,
-            GetFreeLoaderVersionString(),
+            FrLdrVersionString,
             ATTR(UiTitleBoxFgColor, UiTitleBoxBgColor));
 
     //
@@ -628,14 +630,10 @@ VOID TuiMessageBoxCritical(PCSTR MessageText)
         if (MachConsKbHit())
         {
             key = MachConsGetCh();
-            if(key == KEY_EXTENDED)
+            if (key == KEY_EXTENDED)
                 key = MachConsGetCh();
 
-            if(key == KEY_ENTER)
-                break;
-            else if(key == KEY_SPACE)
-                break;
-            else if(key == KEY_ESC)
+            if ((key == KEY_ENTER) || (key == KEY_SPACE) || (key == KEY_ESC))
                 break;
         }
 
@@ -645,7 +643,6 @@ VOID TuiMessageBoxCritical(PCSTR MessageText)
 
         MachHwIdle();
     }
-
 }
 
 VOID TuiDrawProgressBarCenter(ULONG Position, ULONG Range, PCHAR ProgressText)
@@ -702,55 +699,58 @@ VOID TuiDrawProgressBar(ULONG Left, ULONG Top, ULONG Right, ULONG Bottom, ULONG
 
 UCHAR TuiTextToColor(PCSTR ColorText)
 {
-    if (_stricmp(ColorText, "Black") == 0)
-        return COLOR_BLACK;
-    else if (_stricmp(ColorText, "Blue") == 0)
-        return COLOR_BLUE;
-    else if (_stricmp(ColorText, "Green") == 0)
-        return COLOR_GREEN;
-    else if (_stricmp(ColorText, "Cyan") == 0)
-        return COLOR_CYAN;
-    else if (_stricmp(ColorText, "Red") == 0)
-        return COLOR_RED;
-    else if (_stricmp(ColorText, "Magenta") == 0)
-        return COLOR_MAGENTA;
-    else if (_stricmp(ColorText, "Brown") == 0)
-        return COLOR_BROWN;
-    else if (_stricmp(ColorText, "Gray") == 0)
-        return COLOR_GRAY;
-    else if (_stricmp(ColorText, "DarkGray") == 0)
-        return COLOR_DARKGRAY;
-    else if (_stricmp(ColorText, "LightBlue") == 0)
-        return COLOR_LIGHTBLUE;
-    else if (_stricmp(ColorText, "LightGreen") == 0)
-        return COLOR_LIGHTGREEN;
-    else if (_stricmp(ColorText, "LightCyan") == 0)
-        return COLOR_LIGHTCYAN;
-    else if (_stricmp(ColorText, "LightRed") == 0)
-        return COLOR_LIGHTRED;
-    else if (_stricmp(ColorText, "LightMagenta") == 0)
-        return COLOR_LIGHTMAGENTA;
-    else if (_stricmp(ColorText, "Yellow") == 0)
-        return COLOR_YELLOW;
-    else if (_stricmp(ColorText, "White") == 0)
-        return COLOR_WHITE;
+    static const struct
+    {
+        PCSTR ColorName;
+        UCHAR ColorValue;
+    } Colors[] =
+    {
+        {"Black"  , COLOR_BLACK  },
+        {"Blue"   , COLOR_BLUE   },
+        {"Green"  , COLOR_GREEN  },
+        {"Cyan"   , COLOR_CYAN   },
+        {"Red"    , COLOR_RED    },
+        {"Magenta", COLOR_MAGENTA},
+        {"Brown"  , COLOR_BROWN  },
+        {"Gray"   , COLOR_GRAY   },
+        {"DarkGray"    , COLOR_DARKGRAY    },
+        {"LightBlue"   , COLOR_LIGHTBLUE   },
+        {"LightGreen"  , COLOR_LIGHTGREEN  },
+        {"LightCyan"   , COLOR_LIGHTCYAN   },
+        {"LightRed"    , COLOR_LIGHTRED    },
+        {"LightMagenta", COLOR_LIGHTMAGENTA},
+        {"Yellow"      , COLOR_YELLOW      },
+        {"White"       , COLOR_WHITE       },
+    };
+    ULONG i;
+
+    for (i = 0; i < sizeof(Colors)/sizeof(Colors[0]); ++i)
+    {
+        if (_stricmp(ColorText, Colors[i].ColorName) == 0)
+            return Colors[i].ColorValue;
+    }
 
     return COLOR_BLACK;
 }
 
 UCHAR TuiTextToFillStyle(PCSTR FillStyleText)
 {
-    if (_stricmp(FillStyleText, "Light") == 0)
+    static const struct
     {
-        return LIGHT_FILL;
-    }
-    else if (_stricmp(FillStyleText, "Medium") == 0)
+        PCSTR FillStyleName;
+        UCHAR FillStyleValue;
+    } FillStyles[] =
     {
-        return MEDIUM_FILL;
-    }
-    else if (_stricmp(FillStyleText, "Dark") == 0)
+        {"Light" , LIGHT_FILL },
+        {"Medium", MEDIUM_FILL},
+        {"Dark"  , DARK_FILL  },
+    };
+    ULONG i;
+
+    for (i = 0; i < sizeof(FillStyles)/sizeof(FillStyles[0]); ++i)
     {
-        return DARK_FILL;
+        if (_stricmp(FillStyleText, FillStyles[i].FillStyleName) == 0)
+            return FillStyles[i].FillStyleValue;
     }
 
     return LIGHT_FILL;
@@ -882,12 +882,16 @@ BOOLEAN TuiEditBox(PCSTR MessageText, PCHAR EditTextBuffer, ULONG Length)
             temp[j++] = MessageText[i];
     }
 
-    EditBoxTextLength = 0;
+    EditBoxTextLength = (ULONG)strlen(EditTextBuffer);
+    EditBoxTextLength = min(EditBoxTextLength, Length - 1);
     EditBoxTextPosition = 0;
     EditBoxLine = y2 - 2;
     EditBoxStartX = x1 + 3;
     EditBoxEndX = x2 - 3;
+
+    // Draw the edit box background and the text
     UiFillArea(EditBoxStartX, EditBoxLine, EditBoxEndX, EditBoxLine, ' ', ATTR(UiEditBoxTextColor, UiEditBoxBgColor));
+    UiDrawText2(EditBoxStartX, EditBoxLine, EditBoxEndX - EditBoxStartX + 1, EditTextBuffer, ATTR(UiEditBoxTextColor, UiEditBoxBgColor));
 
     // Show the cursor
     EditBoxCursorX = EditBoxStartX;
@@ -910,18 +914,18 @@ BOOLEAN TuiEditBox(PCSTR MessageText, PCHAR EditTextBuffer, ULONG Length)
         {
             Extended = FALSE;
             key = MachConsGetCh();
-            if(key == KEY_EXTENDED)
+            if (key == KEY_EXTENDED)
             {
                 Extended = TRUE;
                 key = MachConsGetCh();
             }
 
-            if(key == KEY_ENTER)
+            if (key == KEY_ENTER)
             {
                 ReturnCode = TRUE;
                 break;
             }
-            else if(key == KEY_ESC)
+            else if (key == KEY_ESC)
             {
                 ReturnCode = FALSE;
                 break;