3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 PVOID TextVideoBuffer
= NULL
;
35 BOOL
TuiInitialize(VOID
)
37 MachVideoClearScreen(ATTR(COLOR_WHITE
, COLOR_BLACK
));
38 MachVideoHideShowTextCursor(FALSE
);
40 TextVideoBuffer
= VideoAllocateOffScreenBuffer();
41 if (TextVideoBuffer
== NULL
)
49 VOID
TuiUnInitialize(VOID
)
51 if (UiUseSpecialEffects
)
57 MachVideoSetDisplayMode(NULL
, FALSE
);
61 MachVideoHideShowTextCursor(TRUE
);
64 VOID
TuiDrawBackdrop(VOID
)
67 // Fill in the background (excluding title box & status bar)
70 TUI_TITLE_BOX_CHAR_HEIGHT
,
74 ATTR(UiBackdropFgColor
, UiBackdropBgColor
));
82 TUI_TITLE_BOX_CHAR_HEIGHT
- 1,
87 ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
94 GetFreeLoaderVersionString(),
95 ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
103 ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
107 ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
112 TuiDrawText(UiScreenWidth
- 16, 3, /*"F1 for Help"*/"F8 for Options", ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
117 TuiDrawText( (UiScreenWidth
/ 2) - (strlen(UiTitleBoxTitleText
) / 2),
120 ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
125 TuiDrawStatusText("Welcome to FreeLoader!");
128 // Update the date & time
132 VideoCopyOffScreenBufferToVRAM();
137 * This function assumes coordinates are zero-based
139 VOID
TuiFillArea(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
, CHAR FillChar
, UCHAR Attr
/* Color Attributes */)
141 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
144 // Clip the area to the screen
145 // FIXME: This code seems to have problems... Uncomment and view ;-)
146 /*if ((Left >= UiScreenWidth) || (Top >= UiScreenHeight))
150 if ((Left + Right) >= UiScreenWidth)
152 Right = UiScreenWidth - Left;
154 if ((Top + Bottom) >= UiScreenHeight)
156 Bottom = UiScreenHeight - Top;
159 // Loop through each line and fill it in
160 for (i
=Top
; i
<=Bottom
; i
++)
162 // Loop through each character (column) in the line and fill it in
163 for (j
=Left
; j
<=Right
; j
++)
165 ScreenMemory
[((i
*2)*UiScreenWidth
)+(j
*2)] = (UCHAR
)FillChar
;
166 ScreenMemory
[((i
*2)*UiScreenWidth
)+(j
*2)+1] = Attr
;
173 * This function assumes coordinates are zero-based
175 VOID
TuiDrawShadow(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
)
177 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
180 // Shade the bottom of the area
181 if (Bottom
< (UiScreenHeight
- 1))
183 if (UiScreenHeight
< 34)
192 for (; Idx
<=Right
; Idx
++)
194 ScreenMemory
[(((Bottom
+1)*2)*UiScreenWidth
)+(Idx
*2)+1] = ATTR(COLOR_GRAY
, COLOR_BLACK
);
198 // Shade the right of the area
199 if (Right
< (UiScreenWidth
- 1))
201 for (Idx
=Top
+1; Idx
<=Bottom
; Idx
++)
203 ScreenMemory
[((Idx
*2)*UiScreenWidth
)+((Right
+1)*2)+1] = ATTR(COLOR_GRAY
, COLOR_BLACK
);
206 if (UiScreenHeight
< 34)
208 if ((Right
+ 1) < (UiScreenWidth
- 1))
210 for (Idx
=Top
+1; Idx
<=Bottom
; Idx
++)
212 ScreenMemory
[((Idx
*2)*UiScreenWidth
)+((Right
+2)*2)+1] = ATTR(COLOR_GRAY
, COLOR_BLACK
);
217 // Shade the bottom right corner
218 if ((Right
< (UiScreenWidth
- 1)) && (Bottom
< (UiScreenHeight
- 1)))
220 ScreenMemory
[(((Bottom
+1)*2)*UiScreenWidth
)+((Right
+1)*2)+1] = ATTR(COLOR_GRAY
, COLOR_BLACK
);
222 if (UiScreenHeight
< 34)
224 if (((Right
+ 1) < (UiScreenWidth
- 1)) && (Bottom
< (UiScreenHeight
- 1)))
226 ScreenMemory
[(((Bottom
+1)*2)*UiScreenWidth
)+((Right
+2)*2)+1] = ATTR(COLOR_GRAY
, COLOR_BLACK
);
233 * This function assumes coordinates are zero-based
235 VOID
TuiDrawBox(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
, UCHAR VertStyle
, UCHAR HorzStyle
, BOOL Fill
, BOOL Shadow
, UCHAR Attr
)
237 UCHAR ULCorner
, URCorner
, LLCorner
, LRCorner
;
239 // Calculate the corner values
240 if (HorzStyle
== HORZ
)
242 if (VertStyle
== VERT
)
249 else // VertStyle == D_VERT
257 else // HorzStyle == D_HORZ
259 if (VertStyle
== VERT
)
266 else // VertStyle == D_VERT
275 // Fill in box background
278 TuiFillArea(Left
, Top
, Right
, Bottom
, ' ', Attr
);
282 TuiFillArea(Left
, Top
, Left
, Top
, ULCorner
, Attr
);
283 TuiFillArea(Right
, Top
, Right
, Top
, URCorner
, Attr
);
284 TuiFillArea(Left
, Bottom
, Left
, Bottom
, LLCorner
, Attr
);
285 TuiFillArea(Right
, Bottom
, Right
, Bottom
, LRCorner
, Attr
);
288 TuiFillArea(Left
, Top
+1, Left
, Bottom
-1, VertStyle
, Attr
);
290 TuiFillArea(Left
+1, Top
, Right
-1, Top
, HorzStyle
, Attr
);
291 // Fill in right line
292 TuiFillArea(Right
, Top
+1, Right
, Bottom
-1, VertStyle
, Attr
);
293 // Fill in bottom line
294 TuiFillArea(Left
+1, Bottom
, Right
-1, Bottom
, HorzStyle
, Attr
);
299 TuiDrawShadow(Left
, Top
, Right
, Bottom
);
305 * This function assumes coordinates are zero-based
307 VOID
TuiDrawText(ULONG X
, ULONG Y
, PCSTR Text
, UCHAR Attr
)
309 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
313 for (i
=X
, j
=0; Text
[j
] && i
<UiScreenWidth
; i
++,j
++)
315 ScreenMemory
[((Y
*2)*UiScreenWidth
)+(i
*2)] = (UCHAR
)Text
[j
];
316 ScreenMemory
[((Y
*2)*UiScreenWidth
)+(i
*2)+1] = Attr
;
320 VOID
TuiDrawCenteredText(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
, PCSTR TextString
, UCHAR Attr
)
325 ULONG LineBreakCount
;
334 TextLength
= strlen(TextString
);
336 // Count the new lines and the box width
340 for (Index
=0; Index
<TextLength
; Index
++)
342 if (TextString
[Index
] == '\n')
349 if ((Index
- LastIndex
) > BoxWidth
)
351 BoxWidth
= (Index
- LastIndex
);
356 BoxHeight
= LineBreakCount
+ 1;
358 RealLeft
= (((Right
- Left
) - BoxWidth
) / 2) + Left
;
359 RealTop
= (((Bottom
- Top
) - BoxHeight
) / 2) + Top
;
362 for (Index
=0; Index
<TextLength
; Index
++)
364 if (TextString
[Index
] == '\n')
371 X
= RealLeft
+ LastIndex
;
374 Temp
[0] = TextString
[Index
];
376 TuiDrawText(X
, Y
, Temp
, Attr
);
381 VOID
TuiDrawStatusText(PCSTR StatusText
)
385 TuiDrawText(0, UiScreenHeight
-1, " ", ATTR(UiStatusBarFgColor
, UiStatusBarBgColor
));
386 TuiDrawText(1, UiScreenHeight
-1, StatusText
, ATTR(UiStatusBarFgColor
, UiStatusBarBgColor
));
388 for (i
=strlen(StatusText
)+1; i
<UiScreenWidth
; i
++)
390 TuiDrawText(i
, UiScreenHeight
-1, " ", ATTR(UiStatusBarFgColor
, UiStatusBarBgColor
));
393 VideoCopyOffScreenBufferToVRAM();
396 VOID
TuiUpdateDateTime(VOID
)
398 ULONG Year
, Month
, Day
;
399 ULONG Hour
, Minute
, Second
;
405 MachRTCGetCurrentDateTime(&Year
, &Month
, &Day
, &Hour
, &Minute
, &Second
);
406 if (Year
< 1 || 9999 < Year
|| Month
< 1 || 12 < Month
|| Day
< 1 ||
407 31 < Day
|| 23 < Hour
|| 59 < Minute
|| 59 < Second
)
409 /* This happens on QEmu sometimes. We just skip updating */
412 // Get the month name
413 strcpy(DateString
, UiMonthNames
[Month
- 1]);
415 itoa(Day
, TempString
, 10);
416 // Get the day postfix
417 if (1 == Day
|| 21 == Day
|| 31 == Day
)
419 strcat(TempString
, "st");
421 else if (2 == Day
|| 22 == Day
)
423 strcat(TempString
, "nd");
425 else if (3 == Day
|| 23 == Day
)
427 strcat(TempString
, "rd");
431 strcat(TempString
, "th");
434 // Add the day to the date
435 strcat(DateString
, TempString
);
436 strcat(DateString
, " ");
438 // Get the year and add it to the date
439 itoa(Year
, TempString
, 10);
440 strcat(DateString
, TempString
);
443 TuiDrawText(UiScreenWidth
-strlen(DateString
)-2, 1, DateString
, ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
445 // Get the hour and change from 24-hour mode to 12-hour
455 itoa(Hour
, TempString
, 10);
456 strcpy(TimeString
, " ");
457 strcat(TimeString
, TempString
);
458 strcat(TimeString
, ":");
459 itoa(Minute
, TempString
, 10);
462 strcat(TimeString
, "0");
464 strcat(TimeString
, TempString
);
465 strcat(TimeString
, ":");
466 itoa(Second
, TempString
, 10);
469 strcat(TimeString
, "0");
471 strcat(TimeString
, TempString
);
474 strcat(TimeString
, " PM");
478 strcat(TimeString
, " AM");
482 TuiDrawText(UiScreenWidth
-strlen(TimeString
)-2, 2, TimeString
, ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
485 VOID
TuiSaveScreen(PUCHAR Buffer
)
487 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
490 for (i
=0; i
< (UiScreenWidth
* UiScreenHeight
* 2); i
++)
492 Buffer
[i
] = ScreenMemory
[i
];
496 VOID
TuiRestoreScreen(PUCHAR Buffer
)
498 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
501 for (i
=0; i
< (UiScreenWidth
* UiScreenHeight
* 2); i
++)
503 ScreenMemory
[i
] = Buffer
[i
];
507 VOID
TuiMessageBox(PCSTR MessageText
)
511 // Save the screen contents
512 ScreenBuffer
= MmAllocateMemory(UiScreenWidth
* UiScreenHeight
* 2);
513 TuiSaveScreen(ScreenBuffer
);
515 // Display the message box
516 TuiMessageBoxCritical(MessageText
);
518 // Restore the screen contents
519 TuiRestoreScreen(ScreenBuffer
);
520 MmFreeMemory(ScreenBuffer
);
523 VOID
TuiMessageBoxCritical(PCSTR MessageText
)
526 unsigned int height
= 1;
535 for (i
=0; i
<strlen(MessageText
); i
++)
537 if (MessageText
[i
] == '\n')
542 for (i
=0,j
=0,k
=0; i
<height
; i
++)
544 while ((MessageText
[j
] != '\n') && (MessageText
[j
] != 0))
557 // Calculate box area
558 x1
= (UiScreenWidth
- (width
+2))/2;
560 y1
= ((UiScreenHeight
- height
- 2)/2) + 1;
561 y2
= y1
+ height
+ 4;
564 TuiDrawBox(x1
, y1
, x2
, y2
, D_VERT
, D_HORZ
, TRUE
, TRUE
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
567 for (i
=0,j
=0; i
<strlen(MessageText
)+1; i
++)
569 if ((MessageText
[i
] == '\n') || (MessageText
[i
] == 0))
573 UiDrawText(x1
+2, y1
+1+curline
, temp
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
577 temp
[j
++] = MessageText
[i
];
581 strcpy(temp
, " OK ");
582 UiDrawText(x1
+((x2
-x1
)/2)-3, y2
-2, temp
, ATTR(COLOR_BLACK
, COLOR_GRAY
));
585 UiDrawStatusText("Press ENTER to continue");
587 VideoCopyOffScreenBufferToVRAM();
593 key
= MachConsGetCh();
594 if(key
== KEY_EXTENDED
)
595 key
= MachConsGetCh();
599 else if(key
== KEY_SPACE
)
601 else if(key
== KEY_ESC
)
607 VideoCopyOffScreenBufferToVRAM();
613 VOID
TuiDrawProgressBarCenter(ULONG Position
, ULONG Range
, PCHAR ProgressText
)
615 ULONG Left
, Top
, Right
, Bottom
;
616 ULONG Width
= 50; // Allow for 50 "bars"
619 Left
= (UiScreenWidth
- Width
- 4) / 2;
620 Right
= Left
+ Width
+ 3;
621 Top
= (UiScreenHeight
- Height
- 2) / 2;
623 Bottom
= Top
+ Height
+ 1;
625 TuiDrawProgressBar(Left
, Top
, Right
, Bottom
, Position
, Range
, ProgressText
);
628 VOID
TuiDrawProgressBar(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
, ULONG Position
, ULONG Range
, PCHAR ProgressText
)
631 ULONG ProgressBarWidth
= (Right
- Left
) - 3;
633 // First make sure the progress bar text fits
634 UiTruncateStringEllipsis(ProgressText
, ProgressBarWidth
- 4);
636 if (Position
> Range
)
642 TuiDrawBox(Left
, Top
, Right
, Bottom
, VERT
, HORZ
, TRUE
, TRUE
, ATTR(UiMenuFgColor
, UiMenuBgColor
));
644 // Draw the "Loading..." text
645 //TuiDrawText(70/2, Top+1, "Loading...", ATTR(UiTextColor, UiMenuBgColor));
646 TuiDrawCenteredText(Left
+ 2, Top
+ 2, Right
- 2, Top
+ 2, ProgressText
, ATTR(UiTextColor
, UiMenuBgColor
));
648 // Draw the percent complete
649 for (i
=0; i
<(Position
*ProgressBarWidth
)/Range
; i
++)
651 TuiDrawText(Left
+2+i
, Top
+2, "\xDB", ATTR(UiTextColor
, UiMenuBgColor
));
655 for (; i
<ProgressBarWidth
; i
++)
657 TuiDrawText(Left
+2+i
, Top
+2, "\xB2", ATTR(UiTextColor
, UiMenuBgColor
));
662 VideoCopyOffScreenBufferToVRAM();
665 UCHAR
TuiTextToColor(PCSTR ColorText
)
667 if (stricmp(ColorText
, "Black") == 0)
669 else if (stricmp(ColorText
, "Blue") == 0)
671 else if (stricmp(ColorText
, "Green") == 0)
673 else if (stricmp(ColorText
, "Cyan") == 0)
675 else if (stricmp(ColorText
, "Red") == 0)
677 else if (stricmp(ColorText
, "Magenta") == 0)
678 return COLOR_MAGENTA
;
679 else if (stricmp(ColorText
, "Brown") == 0)
681 else if (stricmp(ColorText
, "Gray") == 0)
683 else if (stricmp(ColorText
, "DarkGray") == 0)
684 return COLOR_DARKGRAY
;
685 else if (stricmp(ColorText
, "LightBlue") == 0)
686 return COLOR_LIGHTBLUE
;
687 else if (stricmp(ColorText
, "LightGreen") == 0)
688 return COLOR_LIGHTGREEN
;
689 else if (stricmp(ColorText
, "LightCyan") == 0)
690 return COLOR_LIGHTCYAN
;
691 else if (stricmp(ColorText
, "LightRed") == 0)
692 return COLOR_LIGHTRED
;
693 else if (stricmp(ColorText
, "LightMagenta") == 0)
694 return COLOR_LIGHTMAGENTA
;
695 else if (stricmp(ColorText
, "Yellow") == 0)
697 else if (stricmp(ColorText
, "White") == 0)
703 UCHAR
TuiTextToFillStyle(PCSTR FillStyleText
)
705 if (stricmp(FillStyleText
, "Light") == 0)
709 else if (stricmp(FillStyleText
, "Medium") == 0)
713 else if (stricmp(FillStyleText
, "Dark") == 0)
721 VOID
TuiFadeInBackdrop(VOID
)
723 PPALETTE_ENTRY TuiFadePalette
= NULL
;
725 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed())
727 TuiFadePalette
= (PPALETTE_ENTRY
)MmAllocateMemory(sizeof(PALETTE_ENTRY
) * 64);
729 if (TuiFadePalette
!= NULL
)
731 VideoSavePaletteState(TuiFadePalette
, 64);
732 VideoSetAllColorsToBlack(64);
736 // Draw the backdrop and title box
739 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
741 VideoFadeIn(TuiFadePalette
, 64);
742 MmFreeMemory(TuiFadePalette
);
746 VOID
TuiFadeOut(VOID
)
748 PPALETTE_ENTRY TuiFadePalette
= NULL
;
750 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed())
752 TuiFadePalette
= (PPALETTE_ENTRY
)MmAllocateMemory(sizeof(PALETTE_ENTRY
) * 64);
754 if (TuiFadePalette
!= NULL
)
756 VideoSavePaletteState(TuiFadePalette
, 64);
760 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
765 MachVideoSetDisplayMode(NULL
, FALSE
);
767 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
769 VideoRestorePaletteState(TuiFadePalette
, 64);
770 MmFreeMemory(TuiFadePalette
);
775 BOOL
TuiEditBox(PCSTR MessageText
, PCHAR EditTextBuffer
, ULONG Length
)
778 unsigned int height
= 1;
786 ULONG EditBoxStartX
, EditBoxEndX
;
788 unsigned int EditBoxTextCount
;
789 int EditBoxTextDisplayIndex
;
793 // Save the screen contents
794 ScreenBuffer
= MmAllocateMemory(UiScreenWidth
* UiScreenHeight
* 2);
795 TuiSaveScreen(ScreenBuffer
);
798 for (i
=0; i
<strlen(MessageText
); i
++)
800 if (MessageText
[i
] == '\n')
805 for (i
=0,j
=0,k
=0; i
<height
; i
++)
807 while ((MessageText
[j
] != '\n') && (MessageText
[j
] != 0))
820 // Calculate box area
821 x1
= (UiScreenWidth
- (width
+2))/2;
823 y1
= ((UiScreenHeight
- height
- 2)/2) + 1;
824 y2
= y1
+ height
+ 4;
827 TuiDrawBox(x1
, y1
, x2
, y2
, D_VERT
, D_HORZ
, TRUE
, TRUE
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
830 for (i
=0,j
=0; i
<strlen(MessageText
)+1; i
++)
832 if ((MessageText
[i
] == '\n') || (MessageText
[i
] == 0))
836 UiDrawText(x1
+2, y1
+1+curline
, temp
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
840 temp
[j
++] = MessageText
[i
];
843 EditBoxTextCount
= 0;
844 EditBoxLine
= y2
- 2;
845 EditBoxStartX
= x1
+ 3;
846 EditBoxEndX
= x2
- 3;
847 UiFillArea(EditBoxStartX
, EditBoxLine
, EditBoxEndX
, EditBoxLine
, ' ', ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
850 EditBoxCursorX
= EditBoxStartX
;
851 MachVideoSetTextCursorPosition(EditBoxCursorX
, EditBoxLine
);
852 MachVideoHideShowTextCursor(TRUE
);
855 UiDrawStatusText("Press ENTER to continue, or ESC to cancel");
857 VideoCopyOffScreenBufferToVRAM();
863 key
= MachConsGetCh();
864 if(key
== KEY_EXTENDED
)
866 key
= MachConsGetCh();
874 else if(key
== KEY_ESC
)
879 else if (key
== KEY_BACKSPACE
) // Remove a character
881 if (EditBoxTextCount
)
884 EditTextBuffer
[EditBoxTextCount
] = 0;
891 else // Add this key to the buffer
893 if (EditBoxTextCount
< Length
- 1)
895 EditTextBuffer
[EditBoxTextCount
] = key
;
897 EditTextBuffer
[EditBoxTextCount
] = 0;
906 // Draw the edit box background
907 UiFillArea(EditBoxStartX
, EditBoxLine
, EditBoxEndX
, EditBoxLine
, ' ', ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
910 if (EditBoxTextCount
> (EditBoxEndX
- EditBoxStartX
))
912 EditBoxTextDisplayIndex
= EditBoxTextCount
- (EditBoxEndX
- EditBoxStartX
);
913 EditBoxCursorX
= EditBoxEndX
;
917 EditBoxTextDisplayIndex
= 0;
918 EditBoxCursorX
= EditBoxStartX
+ EditBoxTextCount
;
920 UiDrawText(EditBoxStartX
, EditBoxLine
, &EditTextBuffer
[EditBoxTextDisplayIndex
], ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
923 MachVideoSetTextCursorPosition(EditBoxCursorX
, EditBoxLine
);
927 VideoCopyOffScreenBufferToVRAM();
930 // Hide the cursor again
931 MachVideoHideShowTextCursor(FALSE
);
933 // Restore the screen contents
934 TuiRestoreScreen(ScreenBuffer
);
935 MmFreeMemory(ScreenBuffer
);