5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Modifications copyright (c) 2012-2015 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), or (at your option) any later version.
44 * This program is free software: you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation, either version 3 of the License, or
47 * (at your option) any later version.
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
54 * You should have received a copy of the GNU General Public License
55 * along with this program. If not, see <http://www.gnu.org/licenses/>.
65 #include "line_edit.h"
66 #include "mystrings.h"
67 #include "../include/refit_call_wrapper.h"
69 #include "../include/egemb_back_selected_small.h"
70 #include "../include/egemb_back_selected_big.h"
71 #include "../include/egemb_arrow_left.h"
72 #include "../include/egemb_arrow_right.h"
74 // other menu definitions
76 #define MENU_FUNCTION_INIT (0)
77 #define MENU_FUNCTION_CLEANUP (1)
78 #define MENU_FUNCTION_PAINT_ALL (2)
79 #define MENU_FUNCTION_PAINT_SELECTION (3)
80 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
81 #define MENU_FUNCTION_PAINT_HINTS (5)
83 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
85 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
86 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
87 static UINTN TileSizes
[2] = { 144, 64 };
89 // Text and icon spacing constants....
90 #define TEXT_YMARGIN (2)
91 #define TITLEICON_SPACING (16)
93 #define TILE_XSPACING (8)
94 #define TILE_YSPACING (16)
96 // Alignment values for PaintIcon()
100 static EG_IMAGE
*SelectionImages
[2] = { NULL
, NULL
};
101 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
104 // Graphics helper functions
107 static VOID
InitSelection(VOID
)
109 EG_IMAGE
*TempSmallImage
= NULL
, *TempBigImage
= NULL
;
110 BOOLEAN LoadedSmallImage
= FALSE
;
112 if (!AllowGraphicsMode
)
114 if (SelectionImages
[0] != NULL
)
117 // load small selection image
118 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
119 TempSmallImage
= egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, TRUE
);
121 if (TempSmallImage
== NULL
)
122 TempSmallImage
= egPrepareEmbeddedImage(&egemb_back_selected_small
, TRUE
);
124 LoadedSmallImage
= TRUE
;
125 SelectionImages
[1] = egScaleImage(TempSmallImage
, TileSizes
[1], TileSizes
[1]);
127 // load big selection image
128 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
129 TempBigImage
= egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, TRUE
);
131 if (TempBigImage
== NULL
) {
132 if (LoadedSmallImage
) {
133 // calculate big selection image from small one
134 TempBigImage
= egCopyImage(TempSmallImage
);
136 TempBigImage
= egPrepareEmbeddedImage(&egemb_back_selected_big
, TRUE
);
139 SelectionImages
[0] = egScaleImage(TempBigImage
, TileSizes
[0], TileSizes
[0]);
142 egFreeImage(TempSmallImage
);
144 egFreeImage(TempBigImage
);
145 } // VOID InitSelection()
148 // Scrolling functions
151 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
153 State
->PreviousSelection
= State
->CurrentSelection
= 0;
154 State
->MaxIndex
= (INTN
)ItemCount
- 1;
155 State
->FirstVisible
= 0;
156 if (AllowGraphicsMode
) {
157 State
->MaxVisible
= UGAWidth
/ (TileSizes
[0] + TILE_XSPACING
) - 1;
159 State
->MaxVisible
= ConHeight
- 4;
160 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
161 State
->MaxVisible
= (INTN
)VisibleSpace
;
162 State
->PaintAll
= TRUE
;
163 State
->PaintSelection
= FALSE
;
165 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
168 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
169 // visible given the current scrolling condition....
170 static VOID
AdjustScrollState(IN SCROLL_STATE
*State
) {
171 if (State
->CurrentSelection
> State
->LastVisible
) {
172 State
->LastVisible
= State
->CurrentSelection
;
173 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
174 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
175 State
->FirstVisible
= 0;
176 State
->PaintAll
= TRUE
;
178 if (State
->CurrentSelection
< State
->FirstVisible
) {
179 State
->FirstVisible
= State
->CurrentSelection
;
180 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
181 State
->PaintAll
= TRUE
;
183 } // static VOID AdjustScrollState
185 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
187 State
->PreviousSelection
= State
->CurrentSelection
;
190 case SCROLL_LINE_LEFT
:
191 if (State
->CurrentSelection
> 0) {
192 State
->CurrentSelection
--;
196 case SCROLL_LINE_RIGHT
:
197 if (State
->CurrentSelection
< State
->MaxIndex
) {
198 State
->CurrentSelection
++;
203 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
204 if (State
->CurrentSelection
>= State
->InitialRow1
) {
205 if (State
->MaxIndex
> State
->InitialRow1
) { // avoid division by 0!
206 State
->CurrentSelection
= State
->FirstVisible
+ (State
->LastVisible
- State
->FirstVisible
) *
207 (State
->CurrentSelection
- State
->InitialRow1
) /
208 (State
->MaxIndex
- State
->InitialRow1
);
210 State
->CurrentSelection
= State
->FirstVisible
;
212 } // if in second row
214 if (State
->CurrentSelection
> 0)
215 State
->CurrentSelection
--;
219 case SCROLL_LINE_DOWN
:
220 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
221 if (State
->CurrentSelection
<= State
->FinalRow0
) {
222 if (State
->LastVisible
> State
->FirstVisible
) { // avoid division by 0!
223 State
->CurrentSelection
= State
->InitialRow1
+ (State
->MaxIndex
- State
->InitialRow1
) *
224 (State
->CurrentSelection
- State
->FirstVisible
) /
225 (State
->LastVisible
- State
->FirstVisible
);
227 State
->CurrentSelection
= State
->InitialRow1
;
231 if (State
->CurrentSelection
< State
->MaxIndex
)
232 State
->CurrentSelection
++;
237 if (State
->CurrentSelection
<= State
->FinalRow0
)
238 State
->CurrentSelection
-= State
->MaxVisible
;
239 else if (State
->CurrentSelection
== State
->InitialRow1
)
240 State
->CurrentSelection
= State
->FinalRow0
;
242 State
->CurrentSelection
= State
->InitialRow1
;
243 if (State
->CurrentSelection
< 0)
244 State
->CurrentSelection
= 0;
248 if (State
->CurrentSelection
> 0) {
249 State
->PaintAll
= TRUE
;
250 State
->CurrentSelection
= 0;
254 case SCROLL_PAGE_DOWN
:
255 if (State
->CurrentSelection
< State
->FinalRow0
) {
256 State
->CurrentSelection
+= State
->MaxVisible
;
257 if (State
->CurrentSelection
> State
->FinalRow0
)
258 State
->CurrentSelection
= State
->FinalRow0
;
259 } else if (State
->CurrentSelection
== State
->FinalRow0
) {
260 State
->CurrentSelection
++;
262 State
->CurrentSelection
= State
->MaxIndex
;
264 if (State
->CurrentSelection
> State
->MaxIndex
)
265 State
->CurrentSelection
= State
->MaxIndex
;
269 if (State
->CurrentSelection
< State
->MaxIndex
) {
270 State
->PaintAll
= TRUE
;
271 State
->CurrentSelection
= State
->MaxIndex
;
279 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
280 AdjustScrollState(State
);
282 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
283 State
->PaintSelection
= TRUE
;
284 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
285 } // static VOID UpdateScroll()
288 // menu helper functions
291 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
293 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
296 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
298 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
302 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Defaults
)
307 while ((Shortcut
= FindCommaDelimited(Defaults
, j
)) != NULL
) {
308 if (StrLen(Shortcut
) == 1) {
309 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
310 Shortcut
[0] -= ('a' - 'A');
312 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
313 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
314 MyFreePool(Shortcut
);
319 } else if (StrLen(Shortcut
) > 1) {
320 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
321 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
)) {
322 MyFreePool(Shortcut
);
327 MyFreePool(Shortcut
);
333 // Identify the end of row 0 and the beginning of row 1; store the results in the
334 // appropriate fields in State. Also reduce MaxVisible if that value is greater
335 // than the total number of row-0 tags and if we're in an icon-based screen
336 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
339 State
->FinalRow0
= 0;
340 State
->InitialRow1
= State
->MaxIndex
;
341 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
342 if (Screen
->Entries
[i
]->Row
== 0) {
343 State
->FinalRow0
= i
;
344 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
345 State
->InitialRow1
= i
;
348 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
349 State
->MaxVisible
= State
->FinalRow0
+ 1;
350 } // static VOID IdentifyRows()
352 // Blank the screen, wait for a keypress, and restore banner/background.
353 // Screen may still require redrawing of text and icons on return.
354 // TODO: Support more sophisticated screen savers, such as power-saving
355 // mode and dynamic images.
356 static VOID
SaveScreen(VOID
) {
358 EG_PIXEL Black
= { 0x0, 0x0, 0x0, 0 };
360 egClearScreen(&Black
);
361 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
362 if (AllowGraphicsMode
)
363 SwitchToGraphicsAndClear();
365 } // VOID SaveScreen()
368 // generic menu function
370 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN OUT INTN
*DefaultEntryIndex
,
371 OUT REFIT_MENU_ENTRY
**ChosenEntry
)
378 BOOLEAN HaveTimeout
= FALSE
;
379 BOOLEAN WaitForRelease
= FALSE
;
380 UINTN TimeoutCountdown
= 0;
381 INTN PreviousTime
= -1, CurrentTime
, TimeSinceKeystroke
= 0;
382 CHAR16 TimeoutMessage
[256];
383 CHAR16 KeyAsString
[2];
386 if (Screen
->TimeoutSeconds
> 0) {
388 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
392 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
393 IdentifyRows(&State
, Screen
);
394 // override the starting selection with the default index, if any
395 if (*DefaultEntryIndex
>= 0 && *DefaultEntryIndex
<= State
.MaxIndex
) {
396 State
.CurrentSelection
= *DefaultEntryIndex
;
397 if (GlobalConfig
.ScreensaverTime
!= -1)
398 UpdateScroll(&State
, SCROLL_NONE
);
401 if (Screen
->TimeoutSeconds
== -1) {
402 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
403 if (Status
== EFI_NOT_READY
) {
404 MenuExit
= MENU_EXIT_TIMEOUT
;
406 KeyAsString
[0] = key
.UnicodeChar
;
408 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
409 if (ShortcutEntry
>= 0) {
410 State
.CurrentSelection
= ShortcutEntry
;
411 MenuExit
= MENU_EXIT_ENTER
;
413 WaitForRelease
= TRUE
;
419 if (GlobalConfig
.ScreensaverTime
!= -1)
420 State
.PaintAll
= TRUE
;
424 if (State
.PaintAll
&& (GlobalConfig
.ScreensaverTime
!= -1)) {
425 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
426 State
.PaintAll
= FALSE
;
427 } else if (State
.PaintSelection
) {
428 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
429 State
.PaintSelection
= FALSE
;
432 if (WaitForRelease
) {
433 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
434 if (Status
== EFI_SUCCESS
) {
435 // reset, because otherwise the buffer gets queued with keystrokes
436 refit_call2_wrapper(ST
->ConIn
->Reset
, ST
->ConIn
, FALSE
);
437 refit_call1_wrapper(BS
->Stall
, 100000);
439 WaitForRelease
= FALSE
;
440 refit_call2_wrapper(ST
->ConIn
->Reset
, ST
->ConIn
, TRUE
);
446 CurrentTime
= (TimeoutCountdown
+ 5) / 10;
447 if (CurrentTime
!= PreviousTime
) {
448 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, CurrentTime
);
449 if (GlobalConfig
.ScreensaverTime
!= -1)
450 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
451 PreviousTime
= CurrentTime
;
455 // read key press (and wait for it if applicable)
456 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
457 if (Status
!= EFI_SUCCESS
) {
458 if (HaveTimeout
&& TimeoutCountdown
== 0) {
460 MenuExit
= MENU_EXIT_TIMEOUT
;
462 } else if (HaveTimeout
|| GlobalConfig
.ScreensaverTime
> 0) {
463 EFI_EVENT TimerEvent
;
464 UINTN ElapsCount
= 1;
466 Status
= refit_call5_wrapper(BS
->CreateEvent
, EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
467 if (EFI_ERROR(Status
)) {
468 refit_call1_wrapper(BS
->Stall
, 100000); // Pause for 100 ms
470 EFI_EVENT WaitList
[2];
473 refit_call3_wrapper(BS
->SetTimer
, TimerEvent
, TimerRelative
, 10000000); // 1s Timeout
474 WaitList
[0] = ST
->ConIn
->WaitForKey
;
475 WaitList
[1] = TimerEvent
;
476 Status
= refit_call3_wrapper(BS
->WaitForEvent
, 2, WaitList
, &Index
);
477 refit_call1_wrapper(BS
->CloseEvent
, TimerEvent
);
478 if (EFI_ERROR(Status
))
479 refit_call1_wrapper(BS
->Stall
, 100000); // Pause for 100 ms
483 ElapsCount
= 10; // always counted as 1s to end of the timeout
485 TimeSinceKeystroke
+= ElapsCount
;
487 TimeoutCountdown
= TimeoutCountdown
<= ElapsCount
? 0 : TimeoutCountdown
- ElapsCount
;
488 } else if (GlobalConfig
.ScreensaverTime
> 0 &&
489 TimeSinceKeystroke
> (GlobalConfig
.ScreensaverTime
* 10))
492 State
.PaintAll
= TRUE
;
493 TimeSinceKeystroke
= 0;
496 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
500 TimeSinceKeystroke
= 0;
501 } // if/else !read keystroke
504 // the user pressed a key, cancel the timeout
505 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
507 if (GlobalConfig
.ScreensaverTime
== -1) { // cancel start-with-blank-screen coding
508 GlobalConfig
.ScreensaverTime
= 0;
509 if (!GlobalConfig
.TextOnly
)
510 BltClearScreen(TRUE
);
514 // react to key press
515 switch (key
.ScanCode
) {
517 UpdateScroll(&State
, SCROLL_LINE_UP
);
520 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
523 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
526 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
529 UpdateScroll(&State
, SCROLL_FIRST
);
532 UpdateScroll(&State
, SCROLL_LAST
);
535 UpdateScroll(&State
, SCROLL_PAGE_UP
);
538 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
541 MenuExit
= MENU_EXIT_ESCAPE
;
545 MenuExit
= MENU_EXIT_DETAILS
;
552 MenuExit
= MENU_EXIT_ESCAPE
;
555 switch (key
.UnicodeChar
) {
557 case CHAR_CARRIAGE_RETURN
:
559 MenuExit
= MENU_EXIT_ENTER
;
562 MenuExit
= MENU_EXIT_DETAILS
;
565 KeyAsString
[0] = key
.UnicodeChar
;
567 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
568 if (ShortcutEntry
>= 0) {
569 State
.CurrentSelection
= ShortcutEntry
;
570 MenuExit
= MENU_EXIT_ENTER
;
576 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
579 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
580 *DefaultEntryIndex
= State
.CurrentSelection
;
582 } /* static UINTN RunGenericMenu() */
585 // text-mode generic style
588 // Show information lines in text mode.
589 static VOID
ShowTextInfoLines(IN REFIT_MENU_SCREEN
*Screen
) {
592 BeginTextScreen(Screen
->Title
);
593 if (Screen
->InfoLineCount
> 0) {
594 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
595 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
596 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
597 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
600 } // VOID ShowTextInfoLines()
602 // Do most of the work for text-based menus....
603 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
606 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
607 static UINTN MenuPosY
;
608 static CHAR16
**DisplayStrings
;
609 CHAR16 TimeoutMessage
[256];
611 State
->ScrollMode
= SCROLL_MODE_TEXT
;
614 case MENU_FUNCTION_INIT
:
617 if (Screen
->InfoLineCount
> 0)
618 MenuPosY
+= Screen
->InfoLineCount
+ 1;
619 MenuHeight
= ConHeight
- MenuPosY
- 3;
620 if (Screen
->TimeoutSeconds
> 0)
622 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
624 // determine width of the menu
625 MenuWidth
= 20; // minimum
626 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
627 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
628 if (MenuWidth
< ItemWidth
)
629 MenuWidth
= ItemWidth
;
632 if (MenuWidth
> ConWidth
- 3)
633 MenuWidth
= ConWidth
- 3;
635 // prepare strings for display
636 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
637 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
638 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
639 // description of the StrSize parameter to SPrint implies it's measured
640 // in characters, but in practice both TianoCore and GNU-EFI seem to
641 // use bytes instead, resulting in truncated displays. I could just
642 // double the size of the StrSize parameter, but that seems unsafe in
643 // case a future library change starts treating this as characters, so
644 // I'm doing it the hard way in this instance.
645 // TODO: Review the above and possibly change other uses of SPrint()
646 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
647 DisplayStrings
[i
][0] = L
' ';
648 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
649 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
650 DisplayStrings
[i
][MenuWidth
- 1] = 0;
651 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
652 // TODO: account for double-width characters
657 case MENU_FUNCTION_CLEANUP
:
658 // release temporary memory
659 for (i
= 0; i
<= State
->MaxIndex
; i
++)
660 MyFreePool(DisplayStrings
[i
]);
661 MyFreePool(DisplayStrings
);
664 case MENU_FUNCTION_PAINT_ALL
:
665 // paint the whole screen (initially and after scrolling)
667 ShowTextInfoLines(Screen
);
668 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
669 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
670 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
671 if (i
== State
->CurrentSelection
)
672 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
674 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
675 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
678 // scrolling indicators
679 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
680 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
681 if (State
->FirstVisible
> 0)
682 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
684 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
685 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
686 if (State
->LastVisible
< State
->MaxIndex
)
687 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
689 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
690 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
691 if (Screen
->Hint1
!= NULL
) {
692 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 2);
693 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint1
);
695 if (Screen
->Hint2
!= NULL
) {
696 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
697 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint2
);
702 case MENU_FUNCTION_PAINT_SELECTION
:
703 // redraw selection cursor
704 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
705 MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
706 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
707 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
708 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
709 MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
710 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
711 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
714 case MENU_FUNCTION_PAINT_TIMEOUT
:
715 if (ParamText
[0] == 0) {
717 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
718 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 3);
719 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
721 // paint or update message
722 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
723 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 3);
724 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
725 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
733 // graphical generic style
736 inline static UINTN
TextLineHeight(VOID
) {
737 return egGetFontHeight() + TEXT_YMARGIN
* 2;
738 } // UINTN TextLineHeight()
744 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
745 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
746 // specified XPos and YPos locations.
747 static VOID
DrawText(IN CHAR16
*Text
, IN BOOLEAN Selected
, IN UINTN FieldWidth
, IN UINTN XPos
, IN UINTN YPos
)
749 EG_IMAGE
*TextBuffer
;
752 TextBuffer
= egCreateImage(FieldWidth
, TextLineHeight(), FALSE
);
754 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
755 Bg
= MenuBackgroundPixel
;
757 // draw selection bar background
758 egFillImageArea(TextBuffer
, 0, 0, FieldWidth
, TextBuffer
->Height
, &SelectionBackgroundPixel
);
759 Bg
= SelectionBackgroundPixel
;
763 egRenderText(Text
, TextBuffer
, egGetFontCellWidth(), TEXT_YMARGIN
, (Bg
.r
+ Bg
.g
+ Bg
.b
) / 3);
764 egDrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
765 // BltImage(TextBuffer, XPos, YPos);
768 // Finds the average brightness of the input Image.
769 // NOTE: Passing an Image that covers the whole screen can strain the
770 // capacity of a UINTN on a 32-bit system with a very large display.
771 // Using UINT64 instead is unworkable, since the code won't compile
772 // on a 32-bit system. As the intended use for this function is to handle
773 // a single text string's background, this shouldn't be a problem, but it
774 // may need addressing if it's applied more broadly....
775 static UINT8
AverageBrightness(EG_IMAGE
*Image
) {
779 if ((Image
!= NULL
) && ((Image
->Width
* Image
->Height
) != 0)) {
780 for (i
= 0; i
< (Image
->Width
* Image
->Height
); i
++) {
781 Sum
+= (Image
->PixelData
[i
].r
+ Image
->PixelData
[i
].g
+ Image
->PixelData
[i
].b
);
783 Sum
/= (Image
->Width
* Image
->Height
* 3);
786 } // UINT8 AverageBrightness()
788 // Display text against the screen's background image. Special case: If Text is NULL
789 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
790 // to the specified XPos and YPos values.
791 static VOID
DrawTextWithTransparency(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
794 EG_IMAGE
*TextBuffer
= NULL
;
799 egMeasureText(Text
, &TextWidth
, NULL
);
800 if (TextWidth
== 0) {
801 TextWidth
= UGAWidth
;
805 TextBuffer
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
, TextWidth
, TextLineHeight());
806 if (TextBuffer
== NULL
)
810 egRenderText(Text
, TextBuffer
, 0, 0, AverageBrightness(TextBuffer
));
811 egDrawImageWithTransparency(TextBuffer
, NULL
, XPos
, YPos
, TextBuffer
->Width
, TextBuffer
->Height
);
812 egFreeImage(TextBuffer
);
815 // Compute the size & position of the window that will hold a subscreen's information.
816 static VOID
ComputeSubScreenWindowSize(REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*XPos
, UINTN
*YPos
,
817 UINTN
*Width
, UINTN
*Height
, UINTN
*LineWidth
) {
818 UINTN i
, ItemWidth
, HintTop
, BannerBottomEdge
, TitleWidth
;
819 UINTN FontCellWidth
= egGetFontCellWidth();
820 UINTN FontCellHeight
= egGetFontHeight();
824 TitleWidth
= egComputeTextWidth(Screen
->Title
);
826 for (i
= 0; i
< Screen
->InfoLineCount
; i
++) {
827 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
828 if (*Width
< ItemWidth
) {
833 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
834 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
835 if (*Width
< ItemWidth
) {
840 *Width
= (*Width
+ 2) * FontCellWidth
;
842 if (Screen
->TitleImage
)
843 *Width
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2 + FontCellWidth
);
845 *Width
+= FontCellWidth
;
847 if (*Width
< TitleWidth
)
848 *Width
= TitleWidth
+ 2 * FontCellWidth
;
850 // Keep it within the bounds of the screen, or 2/3 of the screen's width
851 // for screens over 800 pixels wide
852 if (*Width
> UGAWidth
)
855 *XPos
= (UGAWidth
- *Width
) / 2;
857 HintTop
= UGAHeight
- (FontCellHeight
* 3); // top of hint text
858 *Height
*= TextLineHeight();
859 if (Screen
->TitleImage
&& (*Height
< (Screen
->TitleImage
->Height
+ TextLineHeight() * 4)))
860 *Height
= Screen
->TitleImage
->Height
+ TextLineHeight() * 4;
862 if (GlobalConfig
.BannerBottomEdge
>= HintTop
) { // probably a full-screen image; treat it as an empty banner
863 BannerBottomEdge
= 0;
865 BannerBottomEdge
= GlobalConfig
.BannerBottomEdge
;
867 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
868 BannerBottomEdge
= 0;
870 if (*Height
> (HintTop
- BannerBottomEdge
- FontCellHeight
* 2)) {
871 // TODO: Implement scrolling in text screen.
872 *Height
= (HintTop
- BannerBottomEdge
- FontCellHeight
* 2);
875 *YPos
= ((UGAHeight
- *Height
) / 2);
876 if (*YPos
< BannerBottomEdge
)
877 *YPos
= BannerBottomEdge
+ FontCellHeight
+ (HintTop
- BannerBottomEdge
- *Height
) / 2;
878 } // VOID ComputeSubScreenWindowSize()
880 // Displays sub-menus
881 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
885 static UINTN LineWidth
, MenuWidth
, MenuHeight
, EntriesPosX
, TitlePosX
, EntriesPosY
, TimeoutPosY
, CharWidth
;
887 EG_PIXEL
*BackgroundPixel
= &(GlobalConfig
.ScreenBackground
->PixelData
[0]);
889 CharWidth
= egGetFontCellWidth();
890 State
->ScrollMode
= SCROLL_MODE_TEXT
;
893 case MENU_FUNCTION_INIT
:
894 InitScroll(State
, Screen
->EntryCount
, 0);
895 ComputeSubScreenWindowSize(Screen
, State
, &EntriesPosX
, &EntriesPosY
, &MenuWidth
, &MenuHeight
, &LineWidth
);
896 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TextLineHeight();
899 SwitchToGraphicsAndClear();
900 Window
= egCreateFilledImage(MenuWidth
, MenuHeight
, FALSE
, BackgroundPixel
);
901 egDrawImage(Window
, EntriesPosX
, EntriesPosY
);
902 ItemWidth
= egComputeTextWidth(Screen
->Title
);
903 if (MenuWidth
> ItemWidth
) {
904 TitlePosX
= EntriesPosX
+ (MenuWidth
- ItemWidth
) / 2 - CharWidth
;
906 TitlePosX
= EntriesPosX
;
908 i
= MenuWidth
/ CharWidth
- 2;
910 Screen
->Title
[i
] = 0;
915 case MENU_FUNCTION_CLEANUP
:
919 case MENU_FUNCTION_PAINT_ALL
:
920 ComputeSubScreenWindowSize(Screen
, State
, &EntriesPosX
, &EntriesPosY
, &MenuWidth
, &MenuHeight
, &LineWidth
);
921 DrawText(Screen
->Title
, FALSE
, (StrLen(Screen
->Title
) + 2) * CharWidth
, TitlePosX
, EntriesPosY
+= TextLineHeight());
922 if (Screen
->TitleImage
) {
923 BltImageAlpha(Screen
->TitleImage
, EntriesPosX
+ TITLEICON_SPACING
, EntriesPosY
+ TextLineHeight() * 2,
925 EntriesPosX
+= (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
* 2);
927 EntriesPosY
+= (TextLineHeight() * 2);
928 if (Screen
->InfoLineCount
> 0) {
929 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
930 DrawText(Screen
->InfoLines
[i
], FALSE
, LineWidth
, EntriesPosX
, EntriesPosY
);
931 EntriesPosY
+= TextLineHeight();
933 EntriesPosY
+= TextLineHeight(); // also add a blank line
936 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
937 DrawText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
), LineWidth
, EntriesPosX
,
938 EntriesPosY
+ i
* TextLineHeight());
940 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
941 if ((Screen
->Hint1
!= NULL
) && (StrLen(Screen
->Hint1
) > 0))
942 DrawTextWithTransparency(Screen
->Hint1
, (UGAWidth
- egComputeTextWidth(Screen
->Hint1
)) / 2,
943 UGAHeight
- (egGetFontHeight() * 3));
944 if ((Screen
->Hint2
!= NULL
) && (StrLen(Screen
->Hint2
) > 0))
945 DrawTextWithTransparency(Screen
->Hint2
, (UGAWidth
- egComputeTextWidth(Screen
->Hint2
)) / 2,
946 UGAHeight
- (egGetFontHeight() * 2));
950 case MENU_FUNCTION_PAINT_SELECTION
:
951 // redraw selection cursor
952 DrawText(Screen
->Entries
[State
->PreviousSelection
]->Title
, FALSE
, LineWidth
,
953 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TextLineHeight());
954 DrawText(Screen
->Entries
[State
->CurrentSelection
]->Title
, TRUE
, LineWidth
,
955 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TextLineHeight());
958 case MENU_FUNCTION_PAINT_TIMEOUT
:
959 DrawText(ParamText
, FALSE
, LineWidth
, EntriesPosX
, TimeoutPosY
);
963 } // static VOID GraphicsMenuStyle()
966 // graphical main menu style
969 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
971 EG_IMAGE
*Background
;
973 if (SelectionImages
!= NULL
) {
975 Background
= egCropImage(GlobalConfig
.ScreenBackground
, XPos
, YPos
,
976 SelectionImages
[Entry
->Row
]->Width
, SelectionImages
[Entry
->Row
]->Height
);
977 egComposeImage(Background
, SelectionImages
[Entry
->Row
], 0, 0);
978 BltImageCompositeBadge(Background
, Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
979 } else { // Image not selected; copy background
980 egDrawImageWithTransparency(Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
,
981 SelectionImages
[Entry
->Row
]->Width
, SelectionImages
[Entry
->Row
]->Height
);
984 } // VOID DrawMainMenuEntry()
986 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
987 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
990 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
991 AdjustScrollState(State
);
992 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
993 if (Screen
->Entries
[i
]->Row
== 0) {
994 if (i
<= State
->LastVisible
) {
995 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
996 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
999 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
1002 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1003 DrawTextWithTransparency(L
"", 0, textPosY
);
1004 DrawTextWithTransparency(Screen
->Entries
[State
->CurrentSelection
]->Title
,
1005 (UGAWidth
- egComputeTextWidth(Screen
->Entries
[State
->CurrentSelection
]->Title
)) >> 1,
1009 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
1010 DrawTextWithTransparency(Screen
->Hint1
, (UGAWidth
- egComputeTextWidth(Screen
->Hint1
)) / 2,
1011 UGAHeight
- (egGetFontHeight() * 3));
1012 DrawTextWithTransparency(Screen
->Hint2
, (UGAWidth
- egComputeTextWidth(Screen
->Hint2
)) / 2,
1013 UGAHeight
- (egGetFontHeight() * 2));
1015 } // static VOID PaintAll()
1017 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
1018 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
1019 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
1020 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
1022 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
1023 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
1024 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
1025 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
1026 YPosPrev
= row0PosY
;
1028 XSelectPrev
= State
->PreviousSelection
;
1029 YPosPrev
= row1PosY
;
1031 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
1032 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
1035 XSelectCur
= State
->CurrentSelection
;
1038 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
1039 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
1040 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1041 DrawTextWithTransparency(L
"", 0, textPosY
);
1042 DrawTextWithTransparency(Screen
->Entries
[State
->CurrentSelection
]->Title
,
1043 (UGAWidth
- egComputeTextWidth(Screen
->Entries
[State
->CurrentSelection
]->Title
)) >> 1,
1046 } else { // Current selection not visible; must redraw the menu....
1047 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
1049 } // static VOID MoveSelection(VOID)
1051 // Display a 48x48 icon at the specified location. Uses the image specified by
1052 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1053 // Y position is specified as the center value, and so is adjusted by half
1054 // the icon's height. The X position is set along the icon's left
1055 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1056 // Alignment == ALIGN_RIGHT
1057 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
1058 EG_IMAGE
*Icon
= NULL
;
1060 Icon
= egFindIcon(ExternalFilename
, GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
]);
1062 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
1064 if (Alignment
== ALIGN_RIGHT
)
1065 PosX
-= Icon
->Width
;
1066 egDrawImageWithTransparency(Icon
, NULL
, PosX
, PosY
- (Icon
->Height
/ 2), Icon
->Width
, Icon
->Height
);
1070 UINTN
ComputeRow0PosY(VOID
) {
1071 return ((UGAHeight
/ 2) - TileSizes
[0] / 2);
1072 } // UINTN ComputeRow0PosY()
1074 // Display (or erase) the arrow icons to the left and right of an icon's row,
1076 static VOID
PaintArrows(SCROLL_STATE
*State
, UINTN PosX
, UINTN PosY
, UINTN row0Loaders
) {
1077 EG_IMAGE
*TempImage
;
1078 UINTN Width
, Height
, RightX
, AdjPosY
;
1080 // NOTE: Assume that left and right arrows are of the same size....
1081 Width
= egemb_arrow_left
.Width
;
1082 Height
= egemb_arrow_left
.Height
;
1083 RightX
= (UGAWidth
+ (TileSizes
[0] + TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
;
1084 AdjPosY
= PosY
- (Height
/ 2);
1086 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1087 // of the surrounding row; PaintIcon() adjusts this back up by half the
1088 // icon's height to properly center it.
1089 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
1090 PaintIcon(&egemb_arrow_left
, L
"arrow_left", PosX
, PosY
, ALIGN_RIGHT
);
1092 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, PosX
- Width
, AdjPosY
, Width
, Height
);
1093 BltImage(TempImage
, PosX
- Width
, AdjPosY
);
1094 egFreeImage(TempImage
);
1097 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
1098 PaintIcon(&egemb_arrow_right
, L
"arrow_right", RightX
, PosY
, ALIGN_LEFT
);
1100 TempImage
= egCropImage(GlobalConfig
.ScreenBackground
, RightX
, AdjPosY
, Width
, Height
);
1101 BltImage(TempImage
, RightX
, AdjPosY
);
1102 egFreeImage(TempImage
);
1104 } // VOID PaintArrows()
1106 // Display main menu in graphics mode
1107 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
1110 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
1111 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
1112 static UINTN
*itemPosX
;
1113 static UINTN row0PosY
, textPosY
;
1115 State
->ScrollMode
= SCROLL_MODE_ICONS
;
1118 case MENU_FUNCTION_INIT
:
1119 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
1125 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1126 if (Screen
->Entries
[i
]->Row
== 1) {
1130 if (row0Count
< State
->MaxVisible
)
1134 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[0] + TILE_XSPACING
) * row0Count
) >> 1;
1135 row0PosY
= ComputeRow0PosY();
1136 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (TileSizes
[1] + TILE_XSPACING
) * row1Count
) >> 1;
1137 row1PosY
= row0PosY
+ TileSizes
[0] + TILE_YSPACING
;
1139 textPosY
= row1PosY
+ TileSizes
[1] + TILE_YSPACING
;
1141 textPosY
= row1PosY
;
1143 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
1144 row0PosXRunning
= row0PosX
;
1145 row1PosXRunning
= row1PosX
;
1146 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
1147 if (Screen
->Entries
[i
]->Row
== 0) {
1148 itemPosX
[i
] = row0PosXRunning
;
1149 row0PosXRunning
+= TileSizes
[0] + TILE_XSPACING
;
1151 itemPosX
[i
] = row1PosXRunning
;
1152 row1PosXRunning
+= TileSizes
[1] + TILE_XSPACING
;
1157 SwitchToGraphicsAndClear();
1160 case MENU_FUNCTION_CLEANUP
:
1161 MyFreePool(itemPosX
);
1164 case MENU_FUNCTION_PAINT_ALL
:
1165 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
1166 // For PaintArrows(), the starting Y position is moved to the midpoint
1167 // of the surrounding row; PaintIcon() adjusts this back up by half the
1168 // icon's height to properly center it.
1169 PaintArrows(State
, row0PosX
- TILE_XSPACING
, row0PosY
+ (TileSizes
[0] / 2), row0Loaders
);
1172 case MENU_FUNCTION_PAINT_SELECTION
:
1173 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
1176 case MENU_FUNCTION_PAINT_TIMEOUT
:
1177 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
)) {
1178 DrawTextWithTransparency(L
"", 0, textPosY
+ TextLineHeight());
1179 DrawTextWithTransparency(ParamText
, (UGAWidth
- egComputeTextWidth(ParamText
)) >> 1, textPosY
+ TextLineHeight());
1184 } // VOID MainMenuStyle()
1186 // Enable the user to edit boot loader options.
1187 // Returns TRUE if the user exited with edited options; FALSE if the user
1188 // pressed Esc to terminate the edit.
1189 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
1191 CHAR16
*EditedOptions
;
1192 BOOLEAN retval
= FALSE
;
1194 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
1198 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
1200 if (!GlobalConfig
.TextOnly
)
1203 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
1204 MyFreePool(MenuEntry
->LoadOptions
);
1205 MenuEntry
->LoadOptions
= EditedOptions
;
1208 if (!GlobalConfig
.TextOnly
)
1211 } // VOID EditOptions()
1214 // user-callable dispatcher functions
1217 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
1219 INTN DefaultEntry
= -1;
1220 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1222 if (AllowGraphicsMode
)
1223 Style
= GraphicsMenuStyle
;
1225 return RunGenericMenu(Screen
, Style
, &DefaultEntry
, ChosenEntry
);
1228 UINTN
RunMainMenu(REFIT_MENU_SCREEN
*Screen
, CHAR16
** DefaultSelection
, REFIT_MENU_ENTRY
**ChosenEntry
)
1230 MENU_STYLE_FUNC Style
= TextMenuStyle
;
1231 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
1232 REFIT_MENU_ENTRY
*TempChosenEntry
;
1235 INTN DefaultEntryIndex
= -1;
1236 INTN DefaultSubmenuIndex
= -1;
1238 TileSizes
[0] = (GlobalConfig
.IconSizes
[ICON_SIZE_BIG
] * 9) / 8;
1239 TileSizes
[1] = (GlobalConfig
.IconSizes
[ICON_SIZE_SMALL
] * 4) / 3;
1241 if ((DefaultSelection
!= NULL
) && (*DefaultSelection
!= NULL
)) {
1242 // Find a menu entry that includes *DefaultSelection as a substring
1243 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, *DefaultSelection
);
1246 if (AllowGraphicsMode
) {
1247 Style
= GraphicsMenuStyle
;
1248 MainStyle
= MainMenuStyle
;
1252 MenuExit
= RunGenericMenu(Screen
, MainStyle
, &DefaultEntryIndex
, &TempChosenEntry
);
1253 Screen
->TimeoutSeconds
= 0;
1255 MenuTitle
= StrDuplicate(TempChosenEntry
->Title
);
1256 if (MenuExit
== MENU_EXIT_DETAILS
) {
1257 if (TempChosenEntry
->SubScreen
!= NULL
) {
1258 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, &DefaultSubmenuIndex
, &TempChosenEntry
);
1259 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
1261 if (MenuExit
== MENU_EXIT_DETAILS
) {
1262 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1265 } else { // no sub-screen; ignore keypress
1268 } // Enter sub-screen
1272 *ChosenEntry
= TempChosenEntry
;
1273 if (DefaultSelection
) {
1274 MyFreePool(*DefaultSelection
);
1275 *DefaultSelection
= MenuTitle
;
1278 } /* UINTN RunMainMenu() */