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 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
51 #include "../include/refit_call_wrapper.h"
53 #include "../include/egemb_back_selected_small.h"
54 #include "../include/egemb_arrow_left.h"
55 #include "../include/egemb_arrow_right.h"
57 // other menu definitions
59 #define MENU_FUNCTION_INIT (0)
60 #define MENU_FUNCTION_CLEANUP (1)
61 #define MENU_FUNCTION_PAINT_ALL (2)
62 #define MENU_FUNCTION_PAINT_SELECTION (3)
63 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
64 #define MENU_FUNCTION_PAINT_HINTS (5)
66 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
68 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
69 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
71 #define TEXT_YMARGIN (2)
72 #define TEXT_XMARGIN (8)
73 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
74 #define TITLEICON_SPACING (16)
76 #define ROW0_TILESIZE (144)
77 #define ROW1_TILESIZE (64)
78 #define TILE_XSPACING (8)
79 #define TILE_YSPACING (16)
81 // Alignment values for PaintIcon()
85 static EG_IMAGE
*SelectionImages
[4] = { NULL
, NULL
, NULL
, NULL
};
86 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
87 static EG_IMAGE
*TextBuffer
= NULL
;
90 // Graphics helper functions
93 static VOID
InitSelection(VOID
)
95 UINTN x
, y
, src_x
, src_y
;
96 EG_PIXEL
*DestPtr
, *SrcPtr
;
98 if (!AllowGraphicsMode
)
100 if (SelectionImages
[0] != NULL
)
103 // load small selection image
104 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
105 SelectionImages
[2] = egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, FALSE
);
107 if (SelectionImages
[2] == NULL
)
108 SelectionImages
[2] = egPrepareEmbeddedImage(&egemb_back_selected_small
, FALSE
);
109 SelectionImages
[2] = egEnsureImageSize(SelectionImages
[2],
110 ROW1_TILESIZE
, ROW1_TILESIZE
, &MenuBackgroundPixel
);
111 if (SelectionImages
[2] == NULL
)
114 // load big selection image
115 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
116 SelectionImages
[0] = egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, FALSE
);
117 SelectionImages
[0] = egEnsureImageSize(SelectionImages
[0],
118 ROW0_TILESIZE
, ROW0_TILESIZE
, &MenuBackgroundPixel
);
120 if (SelectionImages
[0] == NULL
) {
121 // calculate big selection image from small one
123 SelectionImages
[0] = egCreateImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
);
124 if (SelectionImages
[0] == NULL
) {
125 egFreeImage(SelectionImages
[2]);
126 SelectionImages
[2] = NULL
;
130 DestPtr
= SelectionImages
[0]->PixelData
;
131 SrcPtr
= SelectionImages
[2]->PixelData
;
132 for (y
= 0; y
< ROW0_TILESIZE
; y
++) {
133 if (y
< (ROW1_TILESIZE
>> 1))
135 else if (y
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
136 src_y
= (ROW1_TILESIZE
>> 1);
138 src_y
= y
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
140 for (x
= 0; x
< ROW0_TILESIZE
; x
++) {
141 if (x
< (ROW1_TILESIZE
>> 1))
143 else if (x
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
144 src_x
= (ROW1_TILESIZE
>> 1);
146 src_x
= x
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
148 *DestPtr
++ = SrcPtr
[src_y
* ROW1_TILESIZE
+ src_x
];
153 // non-selected background images
154 SelectionImages
[1] = egCreateFilledImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
155 SelectionImages
[3] = egCreateFilledImage(ROW1_TILESIZE
, ROW1_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
159 // Scrolling functions
162 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
164 State
->PreviousSelection
= State
->CurrentSelection
= 0;
165 State
->MaxIndex
= (INTN
)ItemCount
- 1;
166 State
->FirstVisible
= 0;
167 if (AllowGraphicsMode
) {
168 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
170 State
->MaxVisible
= ConHeight
- 4;
171 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
172 State
->MaxVisible
= (INTN
)VisibleSpace
;
173 State
->PaintAll
= TRUE
;
174 State
->PaintSelection
= FALSE
;
176 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
179 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
180 // visible given the current scrolling condition....
181 static VOID
AdjustScrollState(IN SCROLL_STATE
*State
) {
182 if (State
->CurrentSelection
> State
->LastVisible
) {
183 State
->LastVisible
= State
->CurrentSelection
;
184 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
185 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
186 State
->FirstVisible
= 0;
187 State
->PaintAll
= TRUE
;
189 if (State
->CurrentSelection
< State
->FirstVisible
) {
190 State
->FirstVisible
= State
->CurrentSelection
;
191 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
192 State
->PaintAll
= TRUE
;
194 } // static VOID AdjustScrollState
196 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
198 State
->PreviousSelection
= State
->CurrentSelection
;
201 case SCROLL_LINE_LEFT
:
202 if (State
->CurrentSelection
> 0) {
203 State
->CurrentSelection
--;
207 case SCROLL_LINE_RIGHT
:
208 if (State
->CurrentSelection
< State
->MaxIndex
) {
209 State
->CurrentSelection
++;
214 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
215 if (State
->CurrentSelection
>= State
->InitialRow1
) {
216 if (State
->MaxIndex
> State
->InitialRow1
) { // avoid division by 0!
217 State
->CurrentSelection
= State
->FirstVisible
+ (State
->LastVisible
- State
->FirstVisible
) *
218 (State
->CurrentSelection
- State
->InitialRow1
) /
219 (State
->MaxIndex
- State
->InitialRow1
);
221 State
->CurrentSelection
= State
->FirstVisible
;
223 } // if in second row
225 if (State
->CurrentSelection
> 0)
226 State
->CurrentSelection
--;
230 case SCROLL_LINE_DOWN
:
231 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
232 if (State
->CurrentSelection
<= State
->FinalRow0
) {
233 if (State
->LastVisible
> State
->FirstVisible
) { // avoid division by 0!
234 State
->CurrentSelection
= State
->InitialRow1
+ (State
->MaxIndex
- State
->InitialRow1
) *
235 (State
->CurrentSelection
- State
->FirstVisible
) /
236 (State
->LastVisible
- State
->FirstVisible
);
238 State
->CurrentSelection
= State
->InitialRow1
;
242 if (State
->CurrentSelection
< State
->MaxIndex
)
243 State
->CurrentSelection
++;
248 if (State
->CurrentSelection
<= State
->FinalRow0
)
249 State
->CurrentSelection
-= State
->MaxVisible
;
250 else if (State
->CurrentSelection
== State
->InitialRow1
)
251 State
->CurrentSelection
= State
->FinalRow0
;
253 State
->CurrentSelection
= State
->InitialRow1
;
254 if (State
->CurrentSelection
< 0)
255 State
->CurrentSelection
= 0;
259 if (State
->CurrentSelection
> 0) {
260 State
->PaintAll
= TRUE
;
261 State
->CurrentSelection
= 0;
265 case SCROLL_PAGE_DOWN
:
266 if (State
->CurrentSelection
< State
->FinalRow0
) {
267 State
->CurrentSelection
+= State
->MaxVisible
;
268 if (State
->CurrentSelection
> State
->FinalRow0
)
269 State
->CurrentSelection
= State
->FinalRow0
;
270 } else if (State
->CurrentSelection
== State
->FinalRow0
) {
271 State
->CurrentSelection
++;
273 State
->CurrentSelection
= State
->MaxIndex
;
275 if (State
->CurrentSelection
> State
->MaxIndex
)
276 State
->CurrentSelection
= State
->MaxIndex
;
280 if (State
->CurrentSelection
< State
->MaxIndex
) {
281 State
->PaintAll
= TRUE
;
282 State
->CurrentSelection
= State
->MaxIndex
;
290 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
291 AdjustScrollState(State
);
293 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
294 State
->PaintSelection
= TRUE
;
295 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
296 } // static VOID UpdateScroll()
299 // menu helper functions
302 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
304 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
307 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
309 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
313 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
317 if (Shortcut
== NULL
)
320 if (StrLen(Shortcut
) == 1) {
321 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
322 Shortcut
[0] -= ('a' - 'A');
324 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
325 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
330 } else if (StrLen(Shortcut
) > 1) {
331 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
332 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
339 // Identify the end of row 0 and the beginning of row 1; store the results in the
340 // appropriate fields in State. Also reduce MaxVisible if that value is greater
341 // than the total number of row-0 tags and if we're in an icon-based screen
342 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
345 State
->FinalRow0
= 0;
346 State
->InitialRow1
= State
->MaxIndex
;
347 for (i
= 0; i
< State
->MaxIndex
; i
++) {
348 if (Screen
->Entries
[i
]->Row
== 0) {
349 State
->FinalRow0
= i
;
350 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
351 State
->InitialRow1
= i
;
354 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
355 State
->MaxVisible
= State
->FinalRow0
+ 1;
356 } // static VOID IdentifyRows()
359 // generic menu function
361 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN OUT INTN
*DefaultEntryIndex
,
362 OUT REFIT_MENU_ENTRY
**ChosenEntry
)
369 BOOLEAN HaveTimeout
= FALSE
;
370 UINTN TimeoutCountdown
= 0;
371 CHAR16 TimeoutMessage
[256];
372 CHAR16 KeyAsString
[2];
375 if (Screen
->TimeoutSeconds
> 0) {
377 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
381 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
382 IdentifyRows(&State
, Screen
);
383 // override the starting selection with the default index, if any
384 if (*DefaultEntryIndex
>= 0 && *DefaultEntryIndex
<= State
.MaxIndex
) {
385 State
.CurrentSelection
= *DefaultEntryIndex
;
386 UpdateScroll(&State
, SCROLL_NONE
);
388 State
.PaintAll
= TRUE
;
392 if (State
.PaintAll
) {
393 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
394 State
.PaintAll
= FALSE
;
395 } else if (State
.PaintSelection
) {
396 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
397 State
.PaintSelection
= FALSE
;
401 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
402 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
405 // read key press (and wait for it if applicable)
406 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
407 if (Status
== EFI_NOT_READY
) {
408 if (HaveTimeout
&& TimeoutCountdown
== 0) {
410 MenuExit
= MENU_EXIT_TIMEOUT
;
412 } else if (HaveTimeout
) {
413 refit_call1_wrapper(BS
->Stall
, 100000);
416 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
420 // the user pressed a key, cancel the timeout
421 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
425 // react to key press
426 switch (key
.ScanCode
) {
428 UpdateScroll(&State
, SCROLL_LINE_UP
);
431 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
434 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
437 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
440 UpdateScroll(&State
, SCROLL_FIRST
);
443 UpdateScroll(&State
, SCROLL_LAST
);
446 UpdateScroll(&State
, SCROLL_PAGE_UP
);
449 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
452 MenuExit
= MENU_EXIT_ESCAPE
;
456 MenuExit
= MENU_EXIT_DETAILS
;
463 MenuExit
= MENU_EXIT_ESCAPE
;
466 switch (key
.UnicodeChar
) {
468 case CHAR_CARRIAGE_RETURN
:
470 MenuExit
= MENU_EXIT_ENTER
;
473 MenuExit
= MENU_EXIT_DETAILS
;
476 KeyAsString
[0] = key
.UnicodeChar
;
478 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
479 if (ShortcutEntry
>= 0) {
480 State
.CurrentSelection
= ShortcutEntry
;
481 MenuExit
= MENU_EXIT_ENTER
;
487 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
490 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
491 *DefaultEntryIndex
= State
.CurrentSelection
;
493 } /* static UINTN RunGenericMenu() */
496 // text-mode generic style
499 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
502 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
503 static UINTN MenuPosY
;
504 static CHAR16
**DisplayStrings
;
505 CHAR16 TimeoutMessage
[256];
507 State
->ScrollMode
= SCROLL_MODE_TEXT
;
510 case MENU_FUNCTION_INIT
:
513 if (Screen
->InfoLineCount
> 0)
514 MenuPosY
+= Screen
->InfoLineCount
+ 1;
515 MenuHeight
= ConHeight
- MenuPosY
- 3;
516 if (Screen
->TimeoutSeconds
> 0)
518 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
520 // determine width of the menu
521 MenuWidth
= 20; // minimum
522 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
523 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
524 if (MenuWidth
< ItemWidth
)
525 MenuWidth
= ItemWidth
;
528 if (MenuWidth
> ConWidth
- 3)
529 MenuWidth
= ConWidth
- 3;
531 // prepare strings for display
532 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
533 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
534 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
535 // description of the StrSize parameter to SPrint implies it's measured
536 // in characters, but in practice both TianoCore and GNU-EFI seem to
537 // use bytes instead, resulting in truncated displays. I could just
538 // double the size of the StrSize parameter, but that seems unsafe in
539 // case a future library change starts treating this as characters, so
540 // I'm doing it the hard way in this instance.
541 // TODO: Review the above and possibly change other uses of SPrint()
542 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
543 DisplayStrings
[i
][0] = L
' ';
544 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
545 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
546 DisplayStrings
[i
][MenuWidth
- 1] = 0;
547 // DisplayStrings[i] = AllocateZeroPool(256 * sizeof(CHAR16));
548 // SPrint(DisplayStrings[i], ((MenuWidth < 255) ? MenuWidth : 255) * sizeof(CHAR16),
549 // L" %s ", Screen->Entries[i]->Title);
550 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
551 // TODO: account for double-width characters
555 BeginTextScreen(Screen
->Title
);
556 if (Screen
->InfoLineCount
> 0) {
557 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
558 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
559 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
560 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
566 case MENU_FUNCTION_CLEANUP
:
567 // release temporary memory
568 for (i
= 0; i
<= State
->MaxIndex
; i
++)
569 MyFreePool(DisplayStrings
[i
]);
570 MyFreePool(DisplayStrings
);
573 case MENU_FUNCTION_PAINT_ALL
:
574 // paint the whole screen (initially and after scrolling)
575 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
576 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
577 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
578 if (i
== State
->CurrentSelection
)
579 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
581 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
582 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
585 // scrolling indicators
586 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
587 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
588 if (State
->FirstVisible
> 0)
589 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
591 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
592 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
593 if (State
->LastVisible
< State
->MaxIndex
)
594 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
596 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
597 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
598 if (Screen
->Hint1
!= NULL
) {
599 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 2);
600 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint1
);
602 if (Screen
->Hint2
!= NULL
) {
603 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
604 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->Hint2
);
609 case MENU_FUNCTION_PAINT_SELECTION
:
610 // redraw selection cursor
611 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
612 MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
613 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
614 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
615 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2,
616 MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
617 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
618 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
621 case MENU_FUNCTION_PAINT_TIMEOUT
:
622 if (ParamText
[0] == 0) {
624 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
625 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 3);
626 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
628 // paint or update message
629 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
630 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 3);
631 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
632 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
640 // graphical generic style
644 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
646 if (TextBuffer
== NULL
)
647 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
649 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
650 if (SelectedWidth
> 0) {
651 // draw selection bar background
652 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
653 &SelectionBackgroundPixel
);
657 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
658 BltImage(TextBuffer
, XPos
, YPos
);
661 // Displays sub-menus
662 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
666 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
668 State
->ScrollMode
= SCROLL_MODE_TEXT
;
671 case MENU_FUNCTION_INIT
:
672 InitScroll(State
, Screen
->EntryCount
, 0);
674 // determine width of the menu
675 MenuWidth
= 20; // minimum
676 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
677 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
678 if (MenuWidth
< ItemWidth
)
679 MenuWidth
= ItemWidth
;
681 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
682 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
683 if (MenuWidth
< ItemWidth
)
684 MenuWidth
= ItemWidth
;
686 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
687 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
688 MenuWidth
= LAYOUT_TEXT_WIDTH
;
690 if (Screen
->TitleImage
)
691 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
693 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
694 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
695 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
698 SwitchToGraphicsAndClear();
699 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
700 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
701 if (Screen
->TitleImage
)
702 BltImageAlpha(Screen
->TitleImage
,
703 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
704 &MenuBackgroundPixel
);
705 if (Screen
->InfoLineCount
> 0) {
706 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
707 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
708 EntriesPosY
+= TEXT_LINE_HEIGHT
;
710 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
715 case MENU_FUNCTION_CLEANUP
:
719 case MENU_FUNCTION_PAINT_ALL
:
720 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
721 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
722 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
724 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
725 if ((Screen
->Hint1
!= NULL
) && (StrLen(Screen
->Hint1
) > 0)) {
726 DrawMenuText(Screen
->Hint1
, 0, (UGAWidth
- (StrLen(Screen
->Hint1
) * FONT_CELL_WIDTH
)) / 2,
727 UGAHeight
- (FONT_CELL_HEIGHT
* 3));
729 if ((Screen
->Hint2
!= NULL
) && (StrLen(Screen
->Hint2
) > 0)) {
730 DrawMenuText(Screen
->Hint2
, 0, (UGAWidth
- (StrLen(Screen
->Hint2
) * FONT_CELL_WIDTH
)) / 2,
731 UGAHeight
- (FONT_CELL_HEIGHT
* 2));
736 case MENU_FUNCTION_PAINT_SELECTION
:
737 // redraw selection cursor
738 DrawMenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
739 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
740 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
741 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
744 case MENU_FUNCTION_PAINT_TIMEOUT
:
745 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
749 } // static VOID GraphicsMenuStyle()
752 // graphical main menu style
755 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
759 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
760 if (SelectionImages
!= NULL
)
761 BltImageCompositeBadge(SelectionImages
[ImageNum
],
762 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
765 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
767 UINTN TextWidth
, TextPosX
;
769 if (TextBuffer
== NULL
)
770 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
772 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
775 egMeasureText(Text
, &TextWidth
, NULL
);
776 if (TextWidth
> TextBuffer
->Width
)
779 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
780 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
781 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
782 BltImage(TextBuffer
, XPos
, YPos
);
785 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
786 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
789 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
790 AdjustScrollState(State
);
791 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
792 if (Screen
->Entries
[i
]->Row
== 0) {
793 if (i
<= State
->LastVisible
) {
794 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
795 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
798 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
801 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
802 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
803 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
805 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
806 DrawMainMenuText(Screen
->Hint1
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 3));
807 DrawMainMenuText(Screen
->Hint2
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 2));
809 } // static VOID PaintAll()
811 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
812 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
813 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
814 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
816 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
817 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
818 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
819 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
822 XSelectPrev
= State
->PreviousSelection
;
825 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
826 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
829 XSelectCur
= State
->CurrentSelection
;
832 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
833 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
834 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
835 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
836 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
837 } else { // Current selection not visible; must redraw the menu....
838 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
840 } // static VOID MoveSelection(VOID)
842 // Display an icon at the specified location. Uses the image specified by
843 // ExternalFilename if it's available, or BuiltInImage if it's not. The
844 // Y position is specified as the center value, and so is adjusted by half
845 // the icon's height. The X position is set along the icon's left
846 // edge if Alignment == ALIGN_LEFT, and along the right edge if
847 // Alignment == ALIGN_RIGHT
848 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
849 EG_IMAGE
*Icon
= NULL
;
851 if (FileExists(SelfDir
, ExternalFilename
))
852 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
854 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
856 if (Alignment
== ALIGN_RIGHT
)
858 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
860 } // static VOID PaintIcon()
862 // Display main menu in graphics mode
863 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
866 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
867 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
868 static UINTN
*itemPosX
;
869 static UINTN row0PosY
, textPosY
;
870 CHAR16 FileName
[256];
872 State
->ScrollMode
= SCROLL_MODE_ICONS
;
875 case MENU_FUNCTION_INIT
:
876 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
882 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
883 if (Screen
->Entries
[i
]->Row
== 1) {
887 if (row0Count
< State
->MaxVisible
)
891 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
892 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
893 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
894 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
896 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
900 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
901 row0PosXRunning
= row0PosX
;
902 row1PosXRunning
= row1PosX
;
903 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
904 if (Screen
->Entries
[i
]->Row
== 0) {
905 itemPosX
[i
] = row0PosXRunning
;
906 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
908 itemPosX
[i
] = row1PosXRunning
;
909 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
914 SwitchToGraphicsAndClear();
917 case MENU_FUNCTION_CLEANUP
:
918 MyFreePool(itemPosX
);
921 case MENU_FUNCTION_PAINT_ALL
:
922 BltClearScreen(TRUE
);
923 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
924 // For PaintIcon() calls, the starting Y position is moved to the midpoint
925 // of the surrounding row; PaintIcon() adjusts this back up by half the
926 // icon's height to properly center it.
927 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
928 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
929 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
930 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
932 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
933 SPrint(FileName
, 255, L
"%s\\arrow_right.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
934 PaintIcon(&egemb_arrow_right
, FileName
,
935 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
936 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
940 case MENU_FUNCTION_PAINT_SELECTION
:
941 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
944 case MENU_FUNCTION_PAINT_TIMEOUT
:
945 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
946 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
950 } // VOID MainMenuStyle()
952 // Enable the user to edit boot loader options.
953 // Returns TRUE if the user exited with edited options; FALSE if the user
954 // pressed Esc to terminate the edit.
955 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
957 CHAR16
*EditedOptions
;
958 BOOLEAN retval
= FALSE
;
960 if (GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_EDITOR
) {
964 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
966 if (!GlobalConfig
.TextOnly
)
969 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
970 MyFreePool(MenuEntry
->LoadOptions
);
971 MenuEntry
->LoadOptions
= EditedOptions
;
974 if (!GlobalConfig
.TextOnly
)
977 } // VOID EditOptions()
980 // user-callable dispatcher functions
983 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
985 INTN DefaultEntry
= -1;
986 MENU_STYLE_FUNC Style
= TextMenuStyle
;
988 if (AllowGraphicsMode
)
989 Style
= GraphicsMenuStyle
;
991 return RunGenericMenu(Screen
, Style
, &DefaultEntry
, ChosenEntry
);
994 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
996 MENU_STYLE_FUNC Style
= TextMenuStyle
;
997 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
998 REFIT_MENU_ENTRY
*TempChosenEntry
;
1000 INTN DefaultEntryIndex
= -1;
1001 INTN DefaultSubmenuIndex
= -1;
1003 if (DefaultSelection
!= NULL
) {
1004 // Find a menu entry that includes *DefaultSelection as a substring
1005 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
1008 if (AllowGraphicsMode
) {
1009 Style
= GraphicsMenuStyle
;
1010 MainStyle
= MainMenuStyle
;
1014 MenuExit
= RunGenericMenu(Screen
, MainStyle
, &DefaultEntryIndex
, &TempChosenEntry
);
1015 Screen
->TimeoutSeconds
= 0;
1017 if (MenuExit
== MENU_EXIT_DETAILS
) {
1018 if (TempChosenEntry
->SubScreen
!= NULL
) {
1019 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, &DefaultSubmenuIndex
, &TempChosenEntry
);
1020 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
1022 if (MenuExit
== MENU_EXIT_DETAILS
) {
1023 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1026 } else { // no sub-screen; ignore keypress
1029 } // Enter sub-screen
1033 *ChosenEntry
= TempChosenEntry
;
1035 } /* UINTN RunMainMenu() */