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
, PCHAR 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
, PCHAR 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(PCHAR 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 // Get the month name
407 strcpy(DateString
, UiMonthNames
[Month
- 1]);
409 itoa(Day
, TempString
, 10);
410 // Get the day postfix
411 if (1 == Day
|| 21 == Day
|| 31 == Day
)
413 strcat(TempString
, "st");
415 else if (2 == Day
|| 22 == Day
)
417 strcat(TempString
, "nd");
419 else if (3 == Day
|| 23 == Day
)
421 strcat(TempString
, "rd");
425 strcat(TempString
, "th");
428 // Add the day to the date
429 strcat(DateString
, TempString
);
430 strcat(DateString
, " ");
432 // Get the year and add it to the date
433 itoa(Year
, TempString
, 10);
434 strcat(DateString
, TempString
);
437 TuiDrawText(UiScreenWidth
-strlen(DateString
)-2, 1, DateString
, ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
439 // Get the hour and change from 24-hour mode to 12-hour
449 itoa(Hour
, TempString
, 10);
450 strcpy(TimeString
, " ");
451 strcat(TimeString
, TempString
);
452 strcat(TimeString
, ":");
453 itoa(Minute
, TempString
, 10);
456 strcat(TimeString
, "0");
458 strcat(TimeString
, TempString
);
459 strcat(TimeString
, ":");
460 itoa(Second
, TempString
, 10);
463 strcat(TimeString
, "0");
465 strcat(TimeString
, TempString
);
468 strcat(TimeString
, " PM");
472 strcat(TimeString
, " AM");
476 TuiDrawText(UiScreenWidth
-strlen(TimeString
)-2, 2, TimeString
, ATTR(UiTitleBoxFgColor
, UiTitleBoxBgColor
));
479 VOID
TuiSaveScreen(PUCHAR Buffer
)
481 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
484 for (i
=0; i
< (UiScreenWidth
* UiScreenHeight
* 2); i
++)
486 Buffer
[i
] = ScreenMemory
[i
];
490 VOID
TuiRestoreScreen(PUCHAR Buffer
)
492 PUCHAR ScreenMemory
= (PUCHAR
)TextVideoBuffer
;
495 for (i
=0; i
< (UiScreenWidth
* UiScreenHeight
* 2); i
++)
497 ScreenMemory
[i
] = Buffer
[i
];
501 VOID
TuiMessageBox(PCHAR MessageText
)
505 // Save the screen contents
506 ScreenBuffer
= MmAllocateMemory(UiScreenWidth
* UiScreenHeight
* 2);
507 TuiSaveScreen(ScreenBuffer
);
509 // Display the message box
510 TuiMessageBoxCritical(MessageText
);
512 // Restore the screen contents
513 TuiRestoreScreen(ScreenBuffer
);
514 MmFreeMemory(ScreenBuffer
);
517 VOID
TuiMessageBoxCritical(PCHAR MessageText
)
528 for (i
=0; i
<strlen(MessageText
); i
++)
530 if (MessageText
[i
] == '\n')
535 for (i
=0,j
=0,k
=0; i
<height
; i
++)
537 while ((MessageText
[j
] != '\n') && (MessageText
[j
] != 0))
550 // Calculate box area
551 x1
= (UiScreenWidth
- (width
+2))/2;
553 y1
= ((UiScreenHeight
- height
- 2)/2) + 1;
554 y2
= y1
+ height
+ 4;
557 TuiDrawBox(x1
, y1
, x2
, y2
, D_VERT
, D_HORZ
, TRUE
, TRUE
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
560 for (i
=0,j
=0; i
<strlen(MessageText
)+1; i
++)
562 if ((MessageText
[i
] == '\n') || (MessageText
[i
] == 0))
566 UiDrawText(x1
+2, y1
+1+curline
, temp
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
570 temp
[j
++] = MessageText
[i
];
574 strcpy(temp
, " OK ");
575 UiDrawText(x1
+((x2
-x1
)/2)-3, y2
-2, temp
, ATTR(COLOR_BLACK
, COLOR_GRAY
));
578 UiDrawStatusText("Press ENTER to continue");
580 VideoCopyOffScreenBufferToVRAM();
586 key
= MachConsGetCh();
587 if(key
== KEY_EXTENDED
)
588 key
= MachConsGetCh();
592 else if(key
== KEY_SPACE
)
594 else if(key
== KEY_ESC
)
600 VideoCopyOffScreenBufferToVRAM();
606 VOID
TuiDrawProgressBarCenter(ULONG Position
, ULONG Range
, PCHAR ProgressText
)
608 ULONG Left
, Top
, Right
, Bottom
;
609 ULONG Width
= 50; // Allow for 50 "bars"
612 Left
= (UiScreenWidth
- Width
- 4) / 2;
613 Right
= Left
+ Width
+ 3;
614 Top
= (UiScreenHeight
- Height
- 2) / 2;
616 Bottom
= Top
+ Height
+ 1;
618 TuiDrawProgressBar(Left
, Top
, Right
, Bottom
, Position
, Range
, ProgressText
);
621 VOID
TuiDrawProgressBar(ULONG Left
, ULONG Top
, ULONG Right
, ULONG Bottom
, ULONG Position
, ULONG Range
, PCHAR ProgressText
)
624 ULONG ProgressBarWidth
= (Right
- Left
) - 3;
626 // First make sure the progress bar text fits
627 UiTruncateStringEllipsis(ProgressText
, ProgressBarWidth
- 4);
629 if (Position
> Range
)
635 TuiDrawBox(Left
, Top
, Right
, Bottom
, VERT
, HORZ
, TRUE
, TRUE
, ATTR(UiMenuFgColor
, UiMenuBgColor
));
637 // Draw the "Loading..." text
638 //TuiDrawText(70/2, Top+1, "Loading...", ATTR(UiTextColor, UiMenuBgColor));
639 TuiDrawCenteredText(Left
+ 2, Top
+ 2, Right
- 2, Top
+ 2, ProgressText
, ATTR(UiTextColor
, UiMenuBgColor
));
641 // Draw the percent complete
642 for (i
=0; i
<(Position
*ProgressBarWidth
)/Range
; i
++)
644 TuiDrawText(Left
+2+i
, Top
+2, "\xDB", ATTR(UiTextColor
, UiMenuBgColor
));
648 for (; i
<ProgressBarWidth
; i
++)
650 TuiDrawText(Left
+2+i
, Top
+2, "\xB2", ATTR(UiTextColor
, UiMenuBgColor
));
655 VideoCopyOffScreenBufferToVRAM();
658 UCHAR
TuiTextToColor(PCHAR ColorText
)
660 if (stricmp(ColorText
, "Black") == 0)
662 else if (stricmp(ColorText
, "Blue") == 0)
664 else if (stricmp(ColorText
, "Green") == 0)
666 else if (stricmp(ColorText
, "Cyan") == 0)
668 else if (stricmp(ColorText
, "Red") == 0)
670 else if (stricmp(ColorText
, "Magenta") == 0)
671 return COLOR_MAGENTA
;
672 else if (stricmp(ColorText
, "Brown") == 0)
674 else if (stricmp(ColorText
, "Gray") == 0)
676 else if (stricmp(ColorText
, "DarkGray") == 0)
677 return COLOR_DARKGRAY
;
678 else if (stricmp(ColorText
, "LightBlue") == 0)
679 return COLOR_LIGHTBLUE
;
680 else if (stricmp(ColorText
, "LightGreen") == 0)
681 return COLOR_LIGHTGREEN
;
682 else if (stricmp(ColorText
, "LightCyan") == 0)
683 return COLOR_LIGHTCYAN
;
684 else if (stricmp(ColorText
, "LightRed") == 0)
685 return COLOR_LIGHTRED
;
686 else if (stricmp(ColorText
, "LightMagenta") == 0)
687 return COLOR_LIGHTMAGENTA
;
688 else if (stricmp(ColorText
, "Yellow") == 0)
690 else if (stricmp(ColorText
, "White") == 0)
696 UCHAR
TuiTextToFillStyle(PCHAR FillStyleText
)
698 if (stricmp(FillStyleText
, "Light") == 0)
702 else if (stricmp(FillStyleText
, "Medium") == 0)
706 else if (stricmp(FillStyleText
, "Dark") == 0)
714 VOID
TuiFadeInBackdrop(VOID
)
716 PPALETTE_ENTRY TuiFadePalette
= NULL
;
718 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed())
720 TuiFadePalette
= (PPALETTE_ENTRY
)MmAllocateMemory(sizeof(PALETTE_ENTRY
) * 64);
722 if (TuiFadePalette
!= NULL
)
724 VideoSavePaletteState(TuiFadePalette
, 64);
725 VideoSetAllColorsToBlack(64);
729 // Draw the backdrop and title box
732 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
734 VideoFadeIn(TuiFadePalette
, 64);
735 MmFreeMemory(TuiFadePalette
);
739 VOID
TuiFadeOut(VOID
)
741 PPALETTE_ENTRY TuiFadePalette
= NULL
;
743 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed())
745 TuiFadePalette
= (PPALETTE_ENTRY
)MmAllocateMemory(sizeof(PALETTE_ENTRY
) * 64);
747 if (TuiFadePalette
!= NULL
)
749 VideoSavePaletteState(TuiFadePalette
, 64);
753 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
758 MachVideoSetDisplayMode(NULL
, FALSE
);
760 if (UiUseSpecialEffects
&& ! MachVideoIsPaletteFixed() && TuiFadePalette
!= NULL
)
762 VideoRestorePaletteState(TuiFadePalette
, 64);
763 MmFreeMemory(TuiFadePalette
);
768 BOOL
TuiEditBox(PCHAR MessageText
, PCHAR EditTextBuffer
, ULONG Length
)
778 int EditBoxStartX
, EditBoxEndX
;
780 int EditBoxTextCount
;
781 int EditBoxTextDisplayIndex
;
785 // Save the screen contents
786 ScreenBuffer
= MmAllocateMemory(UiScreenWidth
* UiScreenHeight
* 2);
787 TuiSaveScreen(ScreenBuffer
);
790 for (i
=0; i
<strlen(MessageText
); i
++)
792 if (MessageText
[i
] == '\n')
797 for (i
=0,j
=0,k
=0; i
<height
; i
++)
799 while ((MessageText
[j
] != '\n') && (MessageText
[j
] != 0))
812 // Calculate box area
813 x1
= (UiScreenWidth
- (width
+2))/2;
815 y1
= ((UiScreenHeight
- height
- 2)/2) + 1;
816 y2
= y1
+ height
+ 4;
819 TuiDrawBox(x1
, y1
, x2
, y2
, D_VERT
, D_HORZ
, TRUE
, TRUE
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
822 for (i
=0,j
=0; i
<strlen(MessageText
)+1; i
++)
824 if ((MessageText
[i
] == '\n') || (MessageText
[i
] == 0))
828 UiDrawText(x1
+2, y1
+1+curline
, temp
, ATTR(UiMessageBoxFgColor
, UiMessageBoxBgColor
));
832 temp
[j
++] = MessageText
[i
];
835 EditBoxTextCount
= 0;
836 EditBoxLine
= y2
- 2;
837 EditBoxStartX
= x1
+ 3;
838 EditBoxEndX
= x2
- 3;
839 UiFillArea(EditBoxStartX
, EditBoxLine
, EditBoxEndX
, EditBoxLine
, ' ', ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
842 EditBoxCursorX
= EditBoxStartX
;
843 MachVideoSetTextCursorPosition(EditBoxCursorX
, EditBoxLine
);
844 MachVideoHideShowTextCursor(TRUE
);
847 UiDrawStatusText("Press ENTER to continue, or ESC to cancel");
849 VideoCopyOffScreenBufferToVRAM();
855 key
= MachConsGetCh();
856 if(key
== KEY_EXTENDED
)
858 key
= MachConsGetCh();
866 else if(key
== KEY_ESC
)
871 else if (key
== KEY_BACKSPACE
) // Remove a character
873 if (EditBoxTextCount
)
876 EditTextBuffer
[EditBoxTextCount
] = 0;
883 else // Add this key to the buffer
885 if (EditBoxTextCount
< Length
- 1)
887 EditTextBuffer
[EditBoxTextCount
] = key
;
889 EditTextBuffer
[EditBoxTextCount
] = 0;
898 // Draw the edit box background
899 UiFillArea(EditBoxStartX
, EditBoxLine
, EditBoxEndX
, EditBoxLine
, ' ', ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
902 if (EditBoxTextCount
> (EditBoxEndX
- EditBoxStartX
))
904 EditBoxTextDisplayIndex
= EditBoxTextCount
- (EditBoxEndX
- EditBoxStartX
);
905 EditBoxCursorX
= EditBoxEndX
;
909 EditBoxTextDisplayIndex
= 0;
910 EditBoxCursorX
= EditBoxStartX
+ EditBoxTextCount
;
912 UiDrawText(EditBoxStartX
, EditBoxLine
, &EditTextBuffer
[EditBoxTextDisplayIndex
], ATTR(UiEditBoxTextColor
, UiEditBoxBgColor
));
915 MachVideoSetTextCursorPosition(EditBoxCursorX
, EditBoxLine
);
919 VideoCopyOffScreenBufferToVRAM();
922 // Hide the cursor again
923 MachVideoHideShowTextCursor(FALSE
);
925 // Restore the screen contents
926 TuiRestoreScreen(ScreenBuffer
);
927 MmFreeMemory(ScreenBuffer
);