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 INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
368 BOOLEAN HaveTimeout
= FALSE
;
369 UINTN TimeoutCountdown
= 0;
370 CHAR16 TimeoutMessage
[256];
371 CHAR16 KeyAsString
[2];
374 if (Screen
->TimeoutSeconds
> 0) {
376 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
380 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
381 IdentifyRows(&State
, Screen
);
382 // override the starting selection with the default index, if any
383 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
384 State
.CurrentSelection
= DefaultEntryIndex
;
385 UpdateScroll(&State
, SCROLL_NONE
);
387 State
.PaintAll
= TRUE
;
391 if (State
.PaintAll
) {
392 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
393 State
.PaintAll
= FALSE
;
394 } else if (State
.PaintSelection
) {
395 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
396 State
.PaintSelection
= FALSE
;
400 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
401 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
404 // read key press (and wait for it if applicable)
405 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
406 if (Status
== EFI_NOT_READY
) {
407 if (HaveTimeout
&& TimeoutCountdown
== 0) {
409 MenuExit
= MENU_EXIT_TIMEOUT
;
411 } else if (HaveTimeout
) {
412 refit_call1_wrapper(BS
->Stall
, 100000);
415 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
419 // the user pressed a key, cancel the timeout
420 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
424 // react to key press
425 switch (key
.ScanCode
) {
427 UpdateScroll(&State
, SCROLL_LINE_UP
);
430 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
433 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
436 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
439 UpdateScroll(&State
, SCROLL_FIRST
);
442 UpdateScroll(&State
, SCROLL_LAST
);
445 UpdateScroll(&State
, SCROLL_PAGE_UP
);
448 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
451 MenuExit
= MENU_EXIT_ESCAPE
;
455 MenuExit
= MENU_EXIT_DETAILS
;
462 MenuExit
= MENU_EXIT_ESCAPE
;
465 switch (key
.UnicodeChar
) {
467 case CHAR_CARRIAGE_RETURN
:
469 MenuExit
= MENU_EXIT_ENTER
;
472 MenuExit
= MENU_EXIT_DETAILS
;
475 KeyAsString
[0] = key
.UnicodeChar
;
477 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
478 if (ShortcutEntry
>= 0) {
479 State
.CurrentSelection
= ShortcutEntry
;
480 MenuExit
= MENU_EXIT_ENTER
;
486 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
489 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
491 } /* static UINTN RunGenericMenu() */
494 // text-mode generic style
497 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
500 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
501 static UINTN MenuPosY
;
502 static CHAR16
**DisplayStrings
;
503 CHAR16 TimeoutMessage
[256];
505 State
->ScrollMode
= SCROLL_MODE_TEXT
;
508 case MENU_FUNCTION_INIT
:
511 if (Screen
->InfoLineCount
> 0)
512 MenuPosY
+= Screen
->InfoLineCount
+ 1;
513 MenuHeight
= ConHeight
- MenuPosY
- 2;
514 if (Screen
->TimeoutSeconds
> 0)
516 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
518 // determine width of the menu
519 MenuWidth
= 20; // minimum
520 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
521 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
522 if (MenuWidth
< ItemWidth
)
523 MenuWidth
= ItemWidth
;
526 if (MenuWidth
> ConWidth
- 3)
527 MenuWidth
= ConWidth
- 3;
529 // prepare strings for display
530 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
531 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
532 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
533 // description of the StrSize parameter to SPrint implies it's measured
534 // in characters, but in practice both TianoCore and GNU-EFI seem to
535 // use bytes instead, resulting in truncated displays. I could just
536 // double the size of the StrSize parameter, but that seems unsafe in
537 // case a future library change starts treating this as characters, so
538 // I'm doing it the hard way in this instance.
539 // TODO: Review the above and possibly change other uses of SPrint()
540 DisplayStrings
[i
] = AllocateZeroPool(2 * sizeof(CHAR16
));
541 DisplayStrings
[i
][0] = L
' ';
542 MergeStrings(&DisplayStrings
[i
], Screen
->Entries
[i
]->Title
, 0);
543 if (StrLen(DisplayStrings
[i
]) > MenuWidth
)
544 DisplayStrings
[i
][MenuWidth
- 1] = 0;
545 // DisplayStrings[i] = AllocateZeroPool(256 * sizeof(CHAR16));
546 // SPrint(DisplayStrings[i], ((MenuWidth < 255) ? MenuWidth : 255) * sizeof(CHAR16),
547 // L" %s ", Screen->Entries[i]->Title);
548 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
549 // TODO: account for double-width characters
553 BeginTextScreen(Screen
->Title
);
554 if (Screen
->InfoLineCount
> 0) {
555 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
556 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
557 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
558 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
564 case MENU_FUNCTION_CLEANUP
:
565 // release temporary memory
566 for (i
= 0; i
<= State
->MaxIndex
; i
++)
567 MyFreePool(DisplayStrings
[i
]);
568 MyFreePool(DisplayStrings
);
571 case MENU_FUNCTION_PAINT_ALL
:
572 // paint the whole screen (initially and after scrolling)
573 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
574 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
575 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
576 if (i
== State
->CurrentSelection
)
577 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
579 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
580 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
583 // scrolling indicators
584 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
585 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
586 if (State
->FirstVisible
> 0)
587 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
589 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
590 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
591 if (State
->LastVisible
< State
->MaxIndex
)
592 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
594 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
595 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
596 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
597 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
,
598 L
"arrow keys to move cursor; Enter to boot; Insert or F2 for more options");
602 case MENU_FUNCTION_PAINT_SELECTION
:
603 // redraw selection cursor
604 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
605 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
606 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
607 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
608 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
609 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
612 case MENU_FUNCTION_PAINT_TIMEOUT
:
613 if (ParamText
[0] == 0) {
615 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
616 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 2);
617 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
619 // paint or update message
620 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
621 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 2);
622 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
623 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
631 // graphical generic style
635 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
637 if (TextBuffer
== NULL
)
638 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
640 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
641 if (SelectedWidth
> 0) {
642 // draw selection bar background
643 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
644 &SelectionBackgroundPixel
);
648 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
649 BltImage(TextBuffer
, XPos
, YPos
);
652 // Displays sub-menus
653 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
657 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
659 State
->ScrollMode
= SCROLL_MODE_TEXT
;
662 case MENU_FUNCTION_INIT
:
663 InitScroll(State
, Screen
->EntryCount
, 0);
665 // determine width of the menu
666 MenuWidth
= 20; // minimum
667 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
668 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
669 if (MenuWidth
< ItemWidth
)
670 MenuWidth
= ItemWidth
;
672 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
673 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
674 if (MenuWidth
< ItemWidth
)
675 MenuWidth
= ItemWidth
;
677 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
678 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
679 MenuWidth
= LAYOUT_TEXT_WIDTH
;
681 if (Screen
->TitleImage
)
682 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
684 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
685 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
686 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
689 SwitchToGraphicsAndClear();
690 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
691 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
692 if (Screen
->TitleImage
)
693 BltImageAlpha(Screen
->TitleImage
,
694 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
695 &MenuBackgroundPixel
);
696 if (Screen
->InfoLineCount
> 0) {
697 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
698 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
699 EntriesPosY
+= TEXT_LINE_HEIGHT
;
701 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
706 case MENU_FUNCTION_CLEANUP
:
710 case MENU_FUNCTION_PAINT_ALL
:
711 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
712 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
713 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
715 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
716 DrawMenuText(L
"arrow keys to move cursor; Enter to boot;", 0,
717 (UGAWidth
- (41 * FONT_CELL_WIDTH
)) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 3));
718 DrawMenuText(L
"Insert or F2 to edit options", 0,
719 (UGAWidth
- (28 * FONT_CELL_WIDTH
)) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 2));
723 case MENU_FUNCTION_PAINT_SELECTION
:
724 // redraw selection cursor
725 DrawMenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
726 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
727 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
728 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
731 case MENU_FUNCTION_PAINT_TIMEOUT
:
732 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
736 } // static VOID GraphicsMenuStyle()
739 // graphical main menu style
742 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
746 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
747 if (SelectionImages
!= NULL
)
748 BltImageCompositeBadge(SelectionImages
[ImageNum
],
749 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
752 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
754 UINTN TextWidth
, TextPosX
;
756 if (TextBuffer
== NULL
)
757 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
759 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
762 egMeasureText(Text
, &TextWidth
, NULL
);
763 if (TextWidth
> TextBuffer
->Width
)
766 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
767 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
768 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
769 BltImage(TextBuffer
, XPos
, YPos
);
772 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
773 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
776 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
777 AdjustScrollState(State
);
778 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
779 if (Screen
->Entries
[i
]->Row
== 0) {
780 if (i
<= State
->LastVisible
) {
781 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
782 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
785 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
788 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
789 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
790 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
792 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_HINTS
)) {
793 DrawMainMenuText(L
"arrow keys to move cursor; Enter to boot;",
794 (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 3));
795 DrawMainMenuText(L
"Insert or F2 for more options",
796 (UGAWidth
- LAYOUT_TEXT_WIDTH
) / 2, UGAHeight
- (FONT_CELL_HEIGHT
* 2));
798 } // static VOID PaintAll()
800 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
801 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
802 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
803 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
805 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
806 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
807 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
808 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
811 XSelectPrev
= State
->PreviousSelection
;
814 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
815 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
818 XSelectCur
= State
->CurrentSelection
;
821 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
822 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
823 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
824 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
825 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
826 } else { // Current selection not visible; must redraw the menu....
827 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
829 } // static VOID MoveSelection(VOID)
831 // Display an icon at the specified location. Uses the image specified by
832 // ExternalFilename if it's available, or BuiltInImage if it's not. The
833 // Y position is specified as the center value, and so is adjusted by half
834 // the icon's height. The X position is set along the icon's left
835 // edge if Alignment == ALIGN_LEFT, and along the right edge if
836 // Alignment == ALIGN_RIGHT
837 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
838 EG_IMAGE
*Icon
= NULL
;
840 if (FileExists(SelfDir
, ExternalFilename
))
841 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
843 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
845 if (Alignment
== ALIGN_RIGHT
)
847 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
849 } // static VOID PaintIcon()
851 // Display main menu in graphics mode
852 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
855 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
856 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
857 static UINTN
*itemPosX
;
858 static UINTN row0PosY
, textPosY
;
859 CHAR16 FileName
[256];
861 State
->ScrollMode
= SCROLL_MODE_ICONS
;
864 case MENU_FUNCTION_INIT
:
865 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
871 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
872 if (Screen
->Entries
[i
]->Row
== 1) {
876 if (row0Count
< State
->MaxVisible
)
880 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
881 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
882 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
883 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
885 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
889 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
890 row0PosXRunning
= row0PosX
;
891 row1PosXRunning
= row1PosX
;
892 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
893 if (Screen
->Entries
[i
]->Row
== 0) {
894 itemPosX
[i
] = row0PosXRunning
;
895 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
897 itemPosX
[i
] = row1PosXRunning
;
898 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
903 SwitchToGraphicsAndClear();
906 case MENU_FUNCTION_CLEANUP
:
907 MyFreePool(itemPosX
);
910 case MENU_FUNCTION_PAINT_ALL
:
911 BltClearScreen(TRUE
);
912 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
913 // For PaintIcon() calls, the starting Y position is moved to the midpoint
914 // of the surrounding row; PaintIcon() adjusts this back up by half the
915 // icon's height to properly center it.
916 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
917 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
918 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
919 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
921 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
922 SPrint(FileName
, 255, L
"%s\\arrow_right.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
923 PaintIcon(&egemb_arrow_right
, FileName
,
924 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
925 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
929 case MENU_FUNCTION_PAINT_SELECTION
:
930 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
933 case MENU_FUNCTION_PAINT_TIMEOUT
:
934 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
935 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
939 } // VOID MainMenuStyle()
941 // Enable the user to edit boot loader options.
942 // Returns TRUE if the user exited with edited options; FALSE if the user
943 // pressed Esc to terminate the edit.
944 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
946 CHAR16
*EditedOptions
;
947 BOOLEAN retval
= FALSE
;
949 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
951 if (!GlobalConfig
.TextOnly
)
954 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
)) {
955 MyFreePool(MenuEntry
->LoadOptions
);
956 MenuEntry
->LoadOptions
= EditedOptions
;
959 if (!GlobalConfig
.TextOnly
)
962 } // VOID EditOptions()
965 // user-callable dispatcher functions
968 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
970 MENU_STYLE_FUNC Style
= TextMenuStyle
;
972 if (AllowGraphicsMode
)
973 Style
= GraphicsMenuStyle
;
975 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
978 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
980 MENU_STYLE_FUNC Style
= TextMenuStyle
;
981 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
982 REFIT_MENU_ENTRY
*TempChosenEntry
;
984 UINTN DefaultEntryIndex
= -1;
986 if (DefaultSelection
!= NULL
) {
987 // Find a menu entry that includes *DefaultSelection as a substring
988 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
989 // If that didn't work, should we scan more characters? For now, no.
992 if (AllowGraphicsMode
) {
993 Style
= GraphicsMenuStyle
;
994 MainStyle
= MainMenuStyle
;
998 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
999 Screen
->TimeoutSeconds
= 0;
1001 if (MenuExit
== MENU_EXIT_DETAILS
) {
1002 if (TempChosenEntry
->SubScreen
!= NULL
) {
1003 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
1004 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
1006 if (MenuExit
== MENU_EXIT_DETAILS
) {
1007 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
1010 } else { // no sub-screen; ignore keypress
1013 } // Enter sub-screen
1017 *ChosenEntry
= TempChosenEntry
;
1019 } /* UINTN RunMainMenu() */