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 "refit_call_wrapper.h"
53 #include "egemb_back_selected_small.h"
54 #include "egemb_arrow_left.h"
55 #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)
65 typedef VOID (*MENU_STYLE_FUNC
)(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
);
67 static CHAR16 ArrowUp
[2] = { ARROW_UP
, 0 };
68 static CHAR16 ArrowDown
[2] = { ARROW_DOWN
, 0 };
70 #define TEXT_YMARGIN (2)
71 #define TEXT_XMARGIN (8)
72 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
73 #define TITLEICON_SPACING (16)
75 #define ROW0_TILESIZE (144)
76 #define ROW1_TILESIZE (64)
77 #define TILE_XSPACING (8)
78 #define TILE_YSPACING (16)
80 // Alignment values for PaintIcon()
84 static EG_IMAGE
*SelectionImages
[4] = { NULL
, NULL
, NULL
, NULL
};
85 static EG_PIXEL SelectionBackgroundPixel
= { 0xff, 0xff, 0xff, 0 };
86 static EG_IMAGE
*TextBuffer
= NULL
;
88 // Used in MainMenuStyle(), but must be persistent....
89 UINTN row0PosX
= 0, row0PosXRunning
= 0, row1PosY
= 0, row0Loaders
= 0;
92 // Graphics helper functions
95 static VOID
InitSelection(VOID
)
97 UINTN x
, y
, src_x
, src_y
;
98 EG_PIXEL
*DestPtr
, *SrcPtr
;
100 if (!AllowGraphicsMode
)
102 if (SelectionImages
[0] != NULL
)
105 // load small selection image
106 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
107 SelectionImages
[2] = egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, FALSE
);
109 if (SelectionImages
[2] == NULL
)
110 SelectionImages
[2] = egPrepareEmbeddedImage(&egemb_back_selected_small
, FALSE
);
111 SelectionImages
[2] = egEnsureImageSize(SelectionImages
[2],
112 ROW1_TILESIZE
, ROW1_TILESIZE
, &MenuBackgroundPixel
);
113 if (SelectionImages
[2] == NULL
)
116 // load big selection image
117 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
118 SelectionImages
[0] = egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, FALSE
);
119 SelectionImages
[0] = egEnsureImageSize(SelectionImages
[0],
120 ROW0_TILESIZE
, ROW0_TILESIZE
, &MenuBackgroundPixel
);
122 if (SelectionImages
[0] == NULL
) {
123 // calculate big selection image from small one
125 SelectionImages
[0] = egCreateImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
);
126 if (SelectionImages
[0] == NULL
) {
127 egFreeImage(SelectionImages
[2]);
128 SelectionImages
[2] = NULL
;
132 DestPtr
= SelectionImages
[0]->PixelData
;
133 SrcPtr
= SelectionImages
[2]->PixelData
;
134 for (y
= 0; y
< ROW0_TILESIZE
; y
++) {
135 if (y
< (ROW1_TILESIZE
>> 1))
137 else if (y
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
138 src_y
= (ROW1_TILESIZE
>> 1);
140 src_y
= y
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
142 for (x
= 0; x
< ROW0_TILESIZE
; x
++) {
143 if (x
< (ROW1_TILESIZE
>> 1))
145 else if (x
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
146 src_x
= (ROW1_TILESIZE
>> 1);
148 src_x
= x
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
150 *DestPtr
++ = SrcPtr
[src_y
* ROW1_TILESIZE
+ src_x
];
155 // non-selected background images
156 SelectionImages
[1] = egCreateFilledImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
157 SelectionImages
[3] = egCreateFilledImage(ROW1_TILESIZE
, ROW1_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
161 // Scrolling functions
164 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
166 State
->LastSelection
= State
->CurrentSelection
= 0;
167 State
->MaxIndex
= (INTN
)ItemCount
- 1;
168 State
->FirstVisible
= 0;
169 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
170 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
171 State
->MaxVisible
= (INTN
)VisibleSpace
;
172 State
->PaintAll
= TRUE
;
173 State
->PaintSelection
= FALSE
;
175 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
178 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
180 State
->LastSelection
= State
->CurrentSelection
;
184 if (State
->CurrentSelection
> 0) {
185 State
->CurrentSelection
--;
189 case SCROLL_LINE_DOWN
:
190 if (State
->CurrentSelection
< State
->MaxIndex
) {
191 State
->CurrentSelection
++;
195 // TODO: Better handling of SCROLL_PAGE_UP & SCROLL_PAGE_DOWN
198 if (State
->CurrentSelection
> 0) {
199 State
->PaintAll
= TRUE
;
200 State
->CurrentSelection
= 0;
204 case SCROLL_PAGE_DOWN
:
206 if (State
->CurrentSelection
< State
->MaxIndex
) {
207 State
->PaintAll
= TRUE
;
208 State
->CurrentSelection
= State
->MaxIndex
;
217 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->LastSelection
)
218 State
->PaintSelection
= TRUE
;
219 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
223 // menu helper functions
226 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
228 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
231 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
233 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
236 VOID
FreeMenu(IN REFIT_MENU_SCREEN
*Screen
)
239 FreePool(Screen
->Entries
);
242 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
246 if (Shortcut
== NULL
)
249 if (StrLen(Shortcut
) == 1) {
250 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
251 Shortcut
[0] -= ('a' - 'A');
253 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
254 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
259 } else if (StrLen(Shortcut
) > 1) {
260 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
261 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
269 // generic menu function
272 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
279 BOOLEAN HaveTimeout
= FALSE
;
280 UINTN TimeoutCountdown
= 0;
281 CHAR16
*TimeoutMessage
;
282 CHAR16 KeyAsString
[2];
285 if (Screen
->TimeoutSeconds
> 0) {
287 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
291 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
292 // override the starting selection with the default index, if any
293 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
294 State
.CurrentSelection
= DefaultEntryIndex
;
295 UpdateScroll(&State
, SCROLL_NONE
);
300 if (State
.PaintAll
) {
301 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
302 State
.PaintAll
= FALSE
;
303 } else if (State
.PaintSelection
) {
304 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
305 State
.PaintSelection
= FALSE
;
309 TimeoutMessage
= PoolPrint(L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
310 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
311 FreePool(TimeoutMessage
);
314 // read key press (and wait for it if applicable)
315 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
316 if (Status
== EFI_NOT_READY
) {
317 if (HaveTimeout
&& TimeoutCountdown
== 0) {
319 MenuExit
= MENU_EXIT_TIMEOUT
;
321 } else if (HaveTimeout
) {
322 refit_call1_wrapper(BS
->Stall
, 100000);
325 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
329 // the user pressed a key, cancel the timeout
330 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
334 // react to key press
335 switch (key
.ScanCode
) {
338 UpdateScroll(&State
, SCROLL_LINE_UP
);
342 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
345 UpdateScroll(&State
, SCROLL_FIRST
);
348 UpdateScroll(&State
, SCROLL_LAST
);
351 UpdateScroll(&State
, SCROLL_PAGE_UP
);
354 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
357 MenuExit
= MENU_EXIT_ESCAPE
;
361 MenuExit
= MENU_EXIT_DETAILS
;
367 switch (key
.UnicodeChar
) {
369 case CHAR_CARRIAGE_RETURN
:
371 MenuExit
= MENU_EXIT_ENTER
;
374 MenuExit
= MENU_EXIT_DETAILS
;
377 KeyAsString
[0] = key
.UnicodeChar
;
379 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
380 if (ShortcutEntry
>= 0) {
381 State
.CurrentSelection
= ShortcutEntry
;
382 MenuExit
= MENU_EXIT_ENTER
;
388 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
391 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
393 } /* static UINTN RunGenericMenu( */
396 // text-mode generic style
399 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
402 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
403 static UINTN MenuPosY
;
404 static CHAR16
**DisplayStrings
;
405 CHAR16
*TimeoutMessage
;
409 case MENU_FUNCTION_INIT
:
412 if (Screen
->InfoLineCount
> 0)
413 MenuPosY
+= Screen
->InfoLineCount
+ 1;
414 MenuHeight
= ConHeight
- MenuPosY
;
415 if (Screen
->TimeoutSeconds
> 0)
417 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
419 // determine width of the menu
420 MenuWidth
= 20; // minimum
421 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
422 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
423 if (MenuWidth
< ItemWidth
)
424 MenuWidth
= ItemWidth
;
426 if (MenuWidth
> ConWidth
- 6)
427 MenuWidth
= ConWidth
- 6;
429 // prepare strings for display
430 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
431 for (i
= 0; i
<= State
->MaxIndex
; i
++)
432 DisplayStrings
[i
] = PoolPrint(L
" %-.*s ", MenuWidth
, Screen
->Entries
[i
]->Title
);
433 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
434 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
435 // TODO: account for double-width characters
438 BeginTextScreen(Screen
->Title
);
439 if (Screen
->InfoLineCount
> 0) {
440 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
441 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
442 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
443 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
449 case MENU_FUNCTION_CLEANUP
:
450 // release temporary memory
451 for (i
= 0; i
<= State
->MaxIndex
; i
++)
452 FreePool(DisplayStrings
[i
]);
453 FreePool(DisplayStrings
);
456 case MENU_FUNCTION_PAINT_ALL
:
457 // paint the whole screen (initially and after scrolling)
458 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
459 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
460 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
461 if (i
== State
->CurrentSelection
)
462 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
464 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
465 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
468 // scrolling indicators
469 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
470 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
471 if (State
->FirstVisible
> 0)
472 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
474 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
475 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
476 if (State
->LastVisible
< State
->MaxIndex
)
477 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
479 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
482 case MENU_FUNCTION_PAINT_SELECTION
:
483 // redraw selection cursor
484 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->LastSelection
- State
->FirstVisible
));
485 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
486 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->LastSelection
]);
487 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
488 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
489 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
492 case MENU_FUNCTION_PAINT_TIMEOUT
:
493 if (ParamText
[0] == 0) {
495 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
496 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
497 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
499 // paint or update message
500 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
501 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
502 TimeoutMessage
= PoolPrint(L
"%s ", ParamText
);
503 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
504 FreePool(TimeoutMessage
);
512 // graphical generic style
516 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
518 // Print(L"Entering DrawMenuText(); Text is '%s', SelectedWidth is %d, XPos is %d, YPos is %d\n",
519 // Text, SelectedWidth, XPos, YPos);
520 if (TextBuffer
== NULL
)
521 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
523 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
524 if (SelectedWidth
> 0) {
525 // draw selection bar background
526 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
527 &SelectionBackgroundPixel
);
531 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
532 BltImage(TextBuffer
, XPos
, YPos
);
535 // Displays sub-menus
536 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
540 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
544 case MENU_FUNCTION_INIT
:
545 InitScroll(State
, Screen
->EntryCount
, 0);
547 // determine width of the menu
548 MenuWidth
= 20; // minimum
549 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
550 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
551 if (MenuWidth
< ItemWidth
)
552 MenuWidth
= ItemWidth
;
554 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
555 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
556 if (MenuWidth
< ItemWidth
)
557 MenuWidth
= ItemWidth
;
559 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
560 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
561 MenuWidth
= LAYOUT_TEXT_WIDTH
;
563 if (Screen
->TitleImage
)
564 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
566 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
567 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
568 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
571 SwitchToGraphicsAndClear();
572 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
573 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
574 if (Screen
->TitleImage
)
575 BltImageAlpha(Screen
->TitleImage
,
576 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
577 &MenuBackgroundPixel
);
578 if (Screen
->InfoLineCount
> 0) {
579 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
580 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
581 EntriesPosY
+= TEXT_LINE_HEIGHT
;
583 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
588 case MENU_FUNCTION_CLEANUP
:
592 case MENU_FUNCTION_PAINT_ALL
:
593 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
594 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
595 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
599 case MENU_FUNCTION_PAINT_SELECTION
:
600 // redraw selection cursor
601 DrawMenuText(Screen
->Entries
[State
->LastSelection
]->Title
, 0,
602 EntriesPosX
, EntriesPosY
+ State
->LastSelection
* TEXT_LINE_HEIGHT
);
603 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
604 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
607 case MENU_FUNCTION_PAINT_TIMEOUT
:
608 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
615 // graphical main menu style
618 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
622 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
623 if (SelectionImages
!= NULL
)
624 BltImageCompositeBadge(SelectionImages
[ImageNum
],
625 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
628 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
630 UINTN TextWidth
, TextPosX
;
632 if (TextBuffer
== NULL
)
633 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
635 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
638 egMeasureText(Text
, &TextWidth
, NULL
);
639 if (TextWidth
> TextBuffer
->Width
)
642 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
643 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
644 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
645 BltImage(TextBuffer
, XPos
, YPos
);
648 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
649 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
652 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
653 if (State
->CurrentSelection
> State
->LastVisible
) {
654 State
->LastVisible
= State
->CurrentSelection
;
655 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
656 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
657 State
->FirstVisible
= 0;
659 if (State
->CurrentSelection
< State
->FirstVisible
) {
660 State
->FirstVisible
= State
->CurrentSelection
;
661 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
664 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
665 if (Screen
->Entries
[i
]->Row
== 0) {
666 if (i
<= State
->LastVisible
) {
667 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
668 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
671 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
672 itemPosX
[i
], row1PosY
);
675 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
676 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
677 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
678 } // static VOID PaintAll()
680 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
681 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
682 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
683 if ((State
->CurrentSelection
< State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) {
684 DrawMainMenuEntry(Screen
->Entries
[State
->LastSelection
], FALSE
,
685 itemPosX
[State
->LastSelection
- State
->FirstVisible
],
686 (Screen
->Entries
[State
->LastSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
687 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
,
688 itemPosX
[State
->CurrentSelection
- State
->FirstVisible
],
689 (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
690 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
691 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
692 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
694 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
696 } // static VOID MoveSelection(VOID)
698 // Display an icon at the specified location. Uses the image specified by
699 // ExternalFilename if it's available, or BuiltInImage if it's not. The
700 // Y position is specified as the center value, and so is adjusted by half
701 // the icon's height. The X position is set along the icon's left
702 // edge if Alignment == ALIGN_LEFT, and along the right edge if
703 // Alignment == ALIGN_RIGHT
704 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
705 EG_IMAGE
*Icon
= NULL
;
707 if (FileExists(SelfDir
, ExternalFilename
))
708 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
710 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
712 if (Alignment
== ALIGN_RIGHT
)
714 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
716 } // static VOID PaintArrow()
718 // Display main menu in graphics mode
719 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
722 extern UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
723 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
724 static UINTN
*itemPosX
;
725 static UINTN row0PosY
, textPosY
;
729 case MENU_FUNCTION_INIT
:
730 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
736 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
737 if (Screen
->Entries
[i
]->Row
== 1) {
741 if (row0Count
< State
->MaxVisible
)
745 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
746 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
747 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
748 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
750 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
754 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
755 row0PosXRunning
= row0PosX
;
756 row1PosXRunning
= row1PosX
;
757 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
758 if (Screen
->Entries
[i
]->Row
== 0) {
759 itemPosX
[i
] = row0PosXRunning
;
760 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
762 itemPosX
[i
] = row1PosXRunning
;
763 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
768 SwitchToGraphicsAndClear();
771 case MENU_FUNCTION_CLEANUP
:
775 case MENU_FUNCTION_PAINT_ALL
:
776 BltClearScreen(TRUE
);
777 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
778 // For PaintIcon() calls, the starting Y position is moved to the midpoint
779 // of the surrounding row; PaintIcon() adjusts this back up by half the
780 // icon's height to properly center it.
781 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
)))
782 PaintIcon(&egemb_arrow_left
, L
"icons\\arrow_left.icns", row0PosX
- TILE_XSPACING
,
783 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
784 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
)))
785 PaintIcon(&egemb_arrow_right
, L
"icons\\arrow_right.icns",
786 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
787 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
790 case MENU_FUNCTION_PAINT_SELECTION
:
791 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
794 case MENU_FUNCTION_PAINT_TIMEOUT
:
795 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
796 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
803 // user-callable dispatcher functions
806 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
808 MENU_STYLE_FUNC Style
= TextMenuStyle
;
810 if (AllowGraphicsMode
)
811 Style
= GraphicsMenuStyle
;
813 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
816 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
818 MENU_STYLE_FUNC Style
= TextMenuStyle
;
819 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
820 REFIT_MENU_ENTRY
*TempChosenEntry
;
822 UINTN DefaultEntryIndex
= -1;
824 if (DefaultSelection
!= NULL
) {
825 // Find a menu entry that includes *DefaultSelection as a substring
826 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
827 // If that didn't work, should we scan more characters? For now, no.
830 if (AllowGraphicsMode
) {
831 Style
= GraphicsMenuStyle
;
832 MainStyle
= MainMenuStyle
;
836 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
837 Screen
->TimeoutSeconds
= 0;
839 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
840 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
841 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
847 *ChosenEntry
= TempChosenEntry
;
849 } /* UINTN RunMainMenu() */