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 if (VisibleSpace
== 0)
170 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
172 State
->MaxVisible
= (INTN
)VisibleSpace
;
173 State
->PaintAll
= TRUE
;
174 State
->PaintSelection
= FALSE
;
176 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
179 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
181 State
->LastSelection
= State
->CurrentSelection
;
185 if (State
->CurrentSelection
> 0) {
186 State
->CurrentSelection
--;
190 case SCROLL_LINE_DOWN
:
191 if (State
->CurrentSelection
< State
->MaxIndex
) {
192 State
->CurrentSelection
++;
196 // TODO: Better handling of SCROLL_PAGE_UP & SCROLL_PAGE_DOWN
199 if (State
->CurrentSelection
> 0) {
200 State
->PaintAll
= TRUE
;
201 State
->CurrentSelection
= 0;
205 case SCROLL_PAGE_DOWN
:
207 if (State
->CurrentSelection
< State
->MaxIndex
) {
208 State
->PaintAll
= TRUE
;
209 State
->CurrentSelection
= State
->MaxIndex
;
218 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->LastSelection
)
219 State
->PaintSelection
= TRUE
;
220 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
224 // menu helper functions
227 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
229 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
232 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
234 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
237 VOID
FreeMenu(IN REFIT_MENU_SCREEN
*Screen
)
240 FreePool(Screen
->Entries
);
243 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
247 if (Shortcut
== NULL
)
250 if (StrLen(Shortcut
) == 1) {
251 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
252 Shortcut
[0] -= ('a' - 'A');
254 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
255 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] ||
256 Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
261 } else if (StrLen(Shortcut
) > 1) {
262 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
263 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
271 // generic menu function
274 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
281 BOOLEAN HaveTimeout
= FALSE
;
282 UINTN TimeoutCountdown
= 0;
283 CHAR16
*TimeoutMessage
;
284 CHAR16 KeyAsString
[2];
287 if (Screen
->TimeoutSeconds
> 0) {
289 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
293 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
294 // override the starting selection with the default index, if any
295 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
296 State
.CurrentSelection
= DefaultEntryIndex
;
297 UpdateScroll(&State
, SCROLL_NONE
);
302 if (State
.PaintAll
) {
303 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
304 State
.PaintAll
= FALSE
;
305 } else if (State
.PaintSelection
) {
306 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
307 State
.PaintSelection
= FALSE
;
311 TimeoutMessage
= PoolPrint(L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
312 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
313 FreePool(TimeoutMessage
);
316 // read key press (and wait for it if applicable)
317 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
318 if (Status
== EFI_NOT_READY
) {
319 if (HaveTimeout
&& TimeoutCountdown
== 0) {
321 MenuExit
= MENU_EXIT_TIMEOUT
;
323 } else if (HaveTimeout
) {
324 refit_call1_wrapper(BS
->Stall
, 100000);
327 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
331 // the user pressed a key, cancel the timeout
332 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
336 // react to key press
337 switch (key
.ScanCode
) {
340 UpdateScroll(&State
, SCROLL_LINE_UP
);
344 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
347 UpdateScroll(&State
, SCROLL_FIRST
);
350 UpdateScroll(&State
, SCROLL_LAST
);
353 UpdateScroll(&State
, SCROLL_PAGE_UP
);
356 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
359 MenuExit
= MENU_EXIT_ESCAPE
;
363 MenuExit
= MENU_EXIT_DETAILS
;
369 switch (key
.UnicodeChar
) {
371 case CHAR_CARRIAGE_RETURN
:
373 MenuExit
= MENU_EXIT_ENTER
;
376 MenuExit
= MENU_EXIT_DETAILS
;
379 KeyAsString
[0] = key
.UnicodeChar
;
381 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
382 if (ShortcutEntry
>= 0) {
383 State
.CurrentSelection
= ShortcutEntry
;
384 MenuExit
= MENU_EXIT_ENTER
;
390 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
393 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
395 } /* static UINTN RunGenericMenu( */
398 // text-mode generic style
401 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
404 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
405 static UINTN MenuPosY
;
406 static CHAR16
**DisplayStrings
;
407 CHAR16
*TimeoutMessage
;
411 case MENU_FUNCTION_INIT
:
414 if (Screen
->InfoLineCount
> 0)
415 MenuPosY
+= Screen
->InfoLineCount
+ 1;
416 MenuHeight
= ConHeight
- MenuPosY
;
417 if (Screen
->TimeoutSeconds
> 0)
419 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
421 // determine width of the menu
422 MenuWidth
= 20; // minimum
423 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
424 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
425 if (MenuWidth
< ItemWidth
)
426 MenuWidth
= ItemWidth
;
428 if (MenuWidth
> ConWidth
- 6)
429 MenuWidth
= ConWidth
- 6;
431 // prepare strings for display
432 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
433 for (i
= 0; i
<= State
->MaxIndex
; i
++)
434 DisplayStrings
[i
] = PoolPrint(L
" %-.*s ", MenuWidth
, Screen
->Entries
[i
]->Title
);
435 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
436 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
437 // TODO: account for double-width characters
440 BeginTextScreen(Screen
->Title
);
441 if (Screen
->InfoLineCount
> 0) {
442 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
443 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
444 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
445 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
451 case MENU_FUNCTION_CLEANUP
:
452 // release temporary memory
453 for (i
= 0; i
<= State
->MaxIndex
; i
++)
454 FreePool(DisplayStrings
[i
]);
455 FreePool(DisplayStrings
);
458 case MENU_FUNCTION_PAINT_ALL
:
459 // paint the whole screen (initially and after scrolling)
460 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
461 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
462 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
463 if (i
== State
->CurrentSelection
)
464 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
466 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
467 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
470 // scrolling indicators
471 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
472 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
473 if (State
->FirstVisible
> 0)
474 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
476 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
477 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
478 if (State
->LastVisible
< State
->MaxIndex
)
479 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
481 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
484 case MENU_FUNCTION_PAINT_SELECTION
:
485 // redraw selection cursor
486 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->LastSelection
- State
->FirstVisible
));
487 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
488 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->LastSelection
]);
489 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
490 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
491 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
494 case MENU_FUNCTION_PAINT_TIMEOUT
:
495 if (ParamText
[0] == 0) {
497 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
498 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
499 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
501 // paint or update message
502 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
503 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
504 TimeoutMessage
= PoolPrint(L
"%s ", ParamText
);
505 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
506 FreePool(TimeoutMessage
);
514 // graphical generic style
518 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
520 // Print(L"Entering DrawMenuText(); Text is '%s', SelectedWidth is %d, XPos is %d, YPos is %d\n",
521 // Text, SelectedWidth, XPos, YPos);
522 if (TextBuffer
== NULL
)
523 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
525 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
526 if (SelectedWidth
> 0) {
527 // draw selection bar background
528 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
529 &SelectionBackgroundPixel
);
533 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
534 BltImage(TextBuffer
, XPos
, YPos
);
537 // Displays sub-menus
538 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
542 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
546 case MENU_FUNCTION_INIT
:
547 InitScroll(State
, Screen
->EntryCount
, 0);
549 // determine width of the menu
550 MenuWidth
= 20; // minimum
551 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
552 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
553 if (MenuWidth
< ItemWidth
)
554 MenuWidth
= ItemWidth
;
556 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
557 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
558 if (MenuWidth
< ItemWidth
)
559 MenuWidth
= ItemWidth
;
561 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
562 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
563 MenuWidth
= LAYOUT_TEXT_WIDTH
;
565 if (Screen
->TitleImage
)
566 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
568 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
569 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
570 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
573 SwitchToGraphicsAndClear();
574 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
575 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
576 if (Screen
->TitleImage
)
577 BltImageAlpha(Screen
->TitleImage
,
578 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
579 &MenuBackgroundPixel
);
580 if (Screen
->InfoLineCount
> 0) {
581 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
582 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
583 EntriesPosY
+= TEXT_LINE_HEIGHT
;
585 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
590 case MENU_FUNCTION_CLEANUP
:
594 case MENU_FUNCTION_PAINT_ALL
:
595 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
596 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
597 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
601 case MENU_FUNCTION_PAINT_SELECTION
:
602 // redraw selection cursor
603 DrawMenuText(Screen
->Entries
[State
->LastSelection
]->Title
, 0,
604 EntriesPosX
, EntriesPosY
+ State
->LastSelection
* TEXT_LINE_HEIGHT
);
605 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
606 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
609 case MENU_FUNCTION_PAINT_TIMEOUT
:
610 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
617 // graphical main menu style
620 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
624 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
625 if (SelectionImages
!= NULL
)
626 BltImageCompositeBadge(SelectionImages
[ImageNum
],
627 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
630 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
632 UINTN TextWidth
, TextPosX
;
634 if (TextBuffer
== NULL
)
635 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
637 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
640 egMeasureText(Text
, &TextWidth
, NULL
);
641 if (TextWidth
> TextBuffer
->Width
)
644 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
645 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
646 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
647 BltImage(TextBuffer
, XPos
, YPos
);
650 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
651 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
654 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
655 if (State
->CurrentSelection
> State
->LastVisible
) {
656 State
->LastVisible
= State
->CurrentSelection
;
657 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
658 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
659 State
->FirstVisible
= 0;
661 if (State
->CurrentSelection
< State
->FirstVisible
) {
662 State
->FirstVisible
= State
->CurrentSelection
;
663 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
666 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
667 if (Screen
->Entries
[i
]->Row
== 0) {
668 if (i
<= State
->LastVisible
) {
669 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
670 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
673 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
674 itemPosX
[i
], row1PosY
);
677 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
678 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
679 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
680 } // static VOID PaintAll()
682 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
683 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
684 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
685 if ((State
->CurrentSelection
< State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) {
686 DrawMainMenuEntry(Screen
->Entries
[State
->LastSelection
], FALSE
,
687 itemPosX
[State
->LastSelection
- State
->FirstVisible
],
688 (Screen
->Entries
[State
->LastSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
689 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
,
690 itemPosX
[State
->CurrentSelection
- State
->FirstVisible
],
691 (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
692 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
693 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
694 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
696 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
698 } // static VOID MoveSelection(VOID)
700 // Display an icon at the specified location. Uses the image specified by
701 // ExternalFilename if it's available, or BuiltInImage if it's not. The
702 // Y position is specified as the center value, and so is adjusted by half
703 // the icon's height. The X position is set along the icon's left
704 // edge if Alignment == ALIGN_LEFT, and along the right edge if
705 // Alignment == ALIGN_RIGHT
706 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
707 EG_IMAGE
*Icon
= NULL
;
709 if (FileExists(SelfDir
, ExternalFilename
))
710 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
712 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
714 if (Alignment
== ALIGN_RIGHT
)
716 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
718 } // static VOID PaintArrow()
720 // Display main menu in graphics mode
721 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
724 extern UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
725 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
726 static UINTN
*itemPosX
;
727 static UINTN row0PosY
, textPosY
;
731 case MENU_FUNCTION_INIT
:
732 InitScroll(State
, Screen
->EntryCount
, 0);
738 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
739 if (Screen
->Entries
[i
]->Row
== 1) {
743 if (row0Count
< State
->MaxVisible
)
747 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
748 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
749 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
750 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
752 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
756 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
757 row0PosXRunning
= row0PosX
;
758 row1PosXRunning
= row1PosX
;
759 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
760 if (Screen
->Entries
[i
]->Row
== 0) {
761 itemPosX
[i
] = row0PosXRunning
;
762 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
764 itemPosX
[i
] = row1PosXRunning
;
765 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
770 SwitchToGraphicsAndClear();
773 case MENU_FUNCTION_CLEANUP
:
777 case MENU_FUNCTION_PAINT_ALL
:
778 BltClearScreen(TRUE
);
779 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
780 // For PaintIcon() calls, the starting Y position is moved to the midpoint
781 // of the surrounding row; PaintIcon() adjusts this back up by half the
782 // icon's height to properly center it.
783 if (State
->FirstVisible
> 0)
784 PaintIcon(&egemb_arrow_left
, L
"icons\\arrow_left.icns", row0PosX
- TILE_XSPACING
,
785 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
786 if (State
->LastVisible
< (row0Loaders
- 1))
787 PaintIcon(&egemb_arrow_right
, L
"icons\\arrow_right.icns",
788 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
789 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
792 case MENU_FUNCTION_PAINT_SELECTION
:
793 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
796 case MENU_FUNCTION_PAINT_TIMEOUT
:
797 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
798 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
805 // user-callable dispatcher functions
808 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
810 MENU_STYLE_FUNC Style
= TextMenuStyle
;
812 if (AllowGraphicsMode
)
813 Style
= GraphicsMenuStyle
;
815 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
818 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
820 MENU_STYLE_FUNC Style
= TextMenuStyle
;
821 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
822 REFIT_MENU_ENTRY
*TempChosenEntry
;
824 UINTN DefaultEntryIndex
= -1;
826 if (DefaultSelection
!= NULL
) {
827 // Find a menu entry whose shortcut is the first character of DefaultSelection, or
829 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
830 // If that didn't work, should we scan more characters? For now, no.
833 if (AllowGraphicsMode
) {
834 Style
= GraphicsMenuStyle
;
835 MainStyle
= MainMenuStyle
;
839 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
840 Screen
->TimeoutSeconds
= 0;
842 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
843 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
844 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
850 *ChosenEntry
= TempChosenEntry
;
852 } /* UINTN RunMainMenu() */