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 (AllowGraphicsMode
)
170 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
172 State
->MaxVisible
= ConHeight
- 4;
173 if ((VisibleSpace
> 0) && (VisibleSpace
< State
->MaxVisible
))
174 State
->MaxVisible
= (INTN
)VisibleSpace
;
175 State
->PaintAll
= TRUE
;
176 State
->PaintSelection
= FALSE
;
178 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
181 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
182 // visible given the current scrolling condition....
183 static VOID
AdjustScrollState(/* IN REFIT_MENU_SCREEN *Screen, */ IN SCROLL_STATE
*State
) {
184 if (State
->CurrentSelection
> State
->LastVisible
) {
185 State
->LastVisible
= State
->CurrentSelection
;
186 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
187 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
188 State
->FirstVisible
= 0;
189 State
->PaintAll
= TRUE
;
191 if (State
->CurrentSelection
< State
->FirstVisible
) {
192 State
->FirstVisible
= State
->CurrentSelection
;
193 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
194 State
->PaintAll
= TRUE
;
196 } // static VOID AdjustScrollState
198 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
200 State
->LastSelection
= State
->CurrentSelection
;
204 if (State
->CurrentSelection
> 0) {
205 State
->CurrentSelection
--;
209 case SCROLL_LINE_DOWN
:
210 if (State
->CurrentSelection
< State
->MaxIndex
) {
211 State
->CurrentSelection
++;
215 // TODO: Better handling of SCROLL_PAGE_UP & SCROLL_PAGE_DOWN
218 if (State
->CurrentSelection
> 0) {
219 State
->PaintAll
= TRUE
;
220 State
->CurrentSelection
= 0;
224 case SCROLL_PAGE_DOWN
:
226 if (State
->CurrentSelection
< State
->MaxIndex
) {
227 State
->PaintAll
= TRUE
;
228 State
->CurrentSelection
= State
->MaxIndex
;
236 if (!AllowGraphicsMode
)
237 AdjustScrollState(State
);
239 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->LastSelection
)
240 State
->PaintSelection
= TRUE
;
241 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
245 // menu helper functions
248 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
250 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
253 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
255 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
258 VOID
FreeMenu(IN REFIT_MENU_SCREEN
*Screen
)
261 FreePool(Screen
->Entries
);
264 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
268 if (Shortcut
== NULL
)
271 if (StrLen(Shortcut
) == 1) {
272 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
273 Shortcut
[0] -= ('a' - 'A');
275 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
276 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
281 } else if (StrLen(Shortcut
) > 1) {
282 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
283 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
291 // generic menu function
294 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
301 BOOLEAN HaveTimeout
= FALSE
;
302 UINTN TimeoutCountdown
= 0;
303 CHAR16
*TimeoutMessage
;
304 CHAR16 KeyAsString
[2];
307 if (Screen
->TimeoutSeconds
> 0) {
309 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
313 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
314 // override the starting selection with the default index, if any
315 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
316 State
.CurrentSelection
= DefaultEntryIndex
;
317 UpdateScroll(&State
, SCROLL_NONE
);
322 if (State
.PaintAll
) {
323 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
324 State
.PaintAll
= FALSE
;
325 } else if (State
.PaintSelection
) {
326 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
327 State
.PaintSelection
= FALSE
;
331 TimeoutMessage
= PoolPrint(L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
332 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
333 FreePool(TimeoutMessage
);
336 // read key press (and wait for it if applicable)
337 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
338 if (Status
== EFI_NOT_READY
) {
339 if (HaveTimeout
&& TimeoutCountdown
== 0) {
341 MenuExit
= MENU_EXIT_TIMEOUT
;
343 } else if (HaveTimeout
) {
344 refit_call1_wrapper(BS
->Stall
, 100000);
347 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
351 // the user pressed a key, cancel the timeout
352 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
356 // react to key press
357 switch (key
.ScanCode
) {
360 UpdateScroll(&State
, SCROLL_LINE_UP
);
364 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
367 UpdateScroll(&State
, SCROLL_FIRST
);
370 UpdateScroll(&State
, SCROLL_LAST
);
373 UpdateScroll(&State
, SCROLL_PAGE_UP
);
376 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
379 MenuExit
= MENU_EXIT_ESCAPE
;
383 MenuExit
= MENU_EXIT_DETAILS
;
389 switch (key
.UnicodeChar
) {
391 case CHAR_CARRIAGE_RETURN
:
393 MenuExit
= MENU_EXIT_ENTER
;
396 MenuExit
= MENU_EXIT_DETAILS
;
399 KeyAsString
[0] = key
.UnicodeChar
;
401 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
402 if (ShortcutEntry
>= 0) {
403 State
.CurrentSelection
= ShortcutEntry
;
404 MenuExit
= MENU_EXIT_ENTER
;
410 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
413 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
415 } /* static UINTN RunGenericMenu( */
418 // text-mode generic style
421 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
424 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
425 static UINTN MenuPosY
;
426 static CHAR16
**DisplayStrings
;
427 CHAR16
*TimeoutMessage
;
431 case MENU_FUNCTION_INIT
:
434 if (Screen
->InfoLineCount
> 0)
435 MenuPosY
+= Screen
->InfoLineCount
+ 1;
436 MenuHeight
= ConHeight
- MenuPosY
;
437 if (Screen
->TimeoutSeconds
> 0)
439 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
441 // determine width of the menu
442 MenuWidth
= 20; // minimum
443 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
444 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
445 if (MenuWidth
< ItemWidth
)
446 MenuWidth
= ItemWidth
;
448 if (MenuWidth
> ConWidth
- 6)
449 MenuWidth
= ConWidth
- 6;
451 // prepare strings for display
452 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
453 for (i
= 0; i
<= State
->MaxIndex
; i
++)
454 DisplayStrings
[i
] = PoolPrint(L
" %-.*s ", MenuWidth
, Screen
->Entries
[i
]->Title
);
455 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
456 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
457 // TODO: account for double-width characters
460 BeginTextScreen(Screen
->Title
);
461 if (Screen
->InfoLineCount
> 0) {
462 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
463 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
464 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
465 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
471 case MENU_FUNCTION_CLEANUP
:
472 // release temporary memory
473 for (i
= 0; i
<= State
->MaxIndex
; i
++)
474 FreePool(DisplayStrings
[i
]);
475 FreePool(DisplayStrings
);
478 case MENU_FUNCTION_PAINT_ALL
:
479 // paint the whole screen (initially and after scrolling)
480 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
481 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
482 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
483 if (i
== State
->CurrentSelection
)
484 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
486 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
487 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
490 // scrolling indicators
491 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
492 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
493 if (State
->FirstVisible
> 0)
494 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
496 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
497 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
498 if (State
->LastVisible
< State
->MaxIndex
)
499 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
501 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
504 case MENU_FUNCTION_PAINT_SELECTION
:
505 // redraw selection cursor
506 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->LastSelection
- State
->FirstVisible
));
507 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
508 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->LastSelection
]);
509 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
510 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
511 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
514 case MENU_FUNCTION_PAINT_TIMEOUT
:
515 if (ParamText
[0] == 0) {
517 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
518 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
519 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
521 // paint or update message
522 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
523 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
524 TimeoutMessage
= PoolPrint(L
"%s ", ParamText
);
525 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
526 FreePool(TimeoutMessage
);
534 // graphical generic style
538 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
540 // Print(L"Entering DrawMenuText(); Text is '%s', SelectedWidth is %d, XPos is %d, YPos is %d\n",
541 // Text, SelectedWidth, XPos, YPos);
542 if (TextBuffer
== NULL
)
543 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
545 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
546 if (SelectedWidth
> 0) {
547 // draw selection bar background
548 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
549 &SelectionBackgroundPixel
);
553 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
554 BltImage(TextBuffer
, XPos
, YPos
);
557 // Displays sub-menus
558 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
562 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
566 case MENU_FUNCTION_INIT
:
567 InitScroll(State
, Screen
->EntryCount
, 0);
569 // determine width of the menu
570 MenuWidth
= 20; // minimum
571 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
572 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
573 if (MenuWidth
< ItemWidth
)
574 MenuWidth
= ItemWidth
;
576 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
577 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
578 if (MenuWidth
< ItemWidth
)
579 MenuWidth
= ItemWidth
;
581 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
582 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
583 MenuWidth
= LAYOUT_TEXT_WIDTH
;
585 if (Screen
->TitleImage
)
586 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
588 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
589 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
590 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
593 SwitchToGraphicsAndClear();
594 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
595 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
596 if (Screen
->TitleImage
)
597 BltImageAlpha(Screen
->TitleImage
,
598 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
599 &MenuBackgroundPixel
);
600 if (Screen
->InfoLineCount
> 0) {
601 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
602 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
603 EntriesPosY
+= TEXT_LINE_HEIGHT
;
605 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
610 case MENU_FUNCTION_CLEANUP
:
614 case MENU_FUNCTION_PAINT_ALL
:
615 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
616 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
617 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
621 case MENU_FUNCTION_PAINT_SELECTION
:
622 // redraw selection cursor
623 DrawMenuText(Screen
->Entries
[State
->LastSelection
]->Title
, 0,
624 EntriesPosX
, EntriesPosY
+ State
->LastSelection
* TEXT_LINE_HEIGHT
);
625 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
626 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
629 case MENU_FUNCTION_PAINT_TIMEOUT
:
630 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
637 // graphical main menu style
640 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
644 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
645 if (SelectionImages
!= NULL
)
646 BltImageCompositeBadge(SelectionImages
[ImageNum
],
647 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
650 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
652 UINTN TextWidth
, TextPosX
;
654 if (TextBuffer
== NULL
)
655 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
657 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
660 egMeasureText(Text
, &TextWidth
, NULL
);
661 if (TextWidth
> TextBuffer
->Width
)
664 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
665 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
666 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
667 BltImage(TextBuffer
, XPos
, YPos
);
670 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
671 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
674 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
675 AdjustScrollState(State
);
676 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
677 if (Screen
->Entries
[i
]->Row
== 0) {
678 if (i
<= State
->LastVisible
) {
679 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
680 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
683 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
684 itemPosX
[i
], row1PosY
);
687 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
688 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
689 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
690 } // static VOID PaintAll()
692 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
693 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
694 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
695 if ((State
->CurrentSelection
< State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) {
696 DrawMainMenuEntry(Screen
->Entries
[State
->LastSelection
], FALSE
,
697 itemPosX
[State
->LastSelection
- State
->FirstVisible
],
698 (Screen
->Entries
[State
->LastSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
699 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
,
700 itemPosX
[State
->CurrentSelection
- State
->FirstVisible
],
701 (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) ? row0PosY
: row1PosY
);
702 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
703 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
704 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
706 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
708 } // static VOID MoveSelection(VOID)
710 // Display an icon at the specified location. Uses the image specified by
711 // ExternalFilename if it's available, or BuiltInImage if it's not. The
712 // Y position is specified as the center value, and so is adjusted by half
713 // the icon's height. The X position is set along the icon's left
714 // edge if Alignment == ALIGN_LEFT, and along the right edge if
715 // Alignment == ALIGN_RIGHT
716 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
717 EG_IMAGE
*Icon
= NULL
;
719 if (FileExists(SelfDir
, ExternalFilename
))
720 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
722 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
724 if (Alignment
== ALIGN_RIGHT
)
726 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
728 } // static VOID PaintArrow()
730 // Display main menu in graphics mode
731 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
734 extern UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
735 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
736 static UINTN
*itemPosX
;
737 static UINTN row0PosY
, textPosY
;
741 case MENU_FUNCTION_INIT
:
742 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
748 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
749 if (Screen
->Entries
[i
]->Row
== 1) {
753 if (row0Count
< State
->MaxVisible
)
757 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
758 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
759 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
760 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
762 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
766 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
767 row0PosXRunning
= row0PosX
;
768 row1PosXRunning
= row1PosX
;
769 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
770 if (Screen
->Entries
[i
]->Row
== 0) {
771 itemPosX
[i
] = row0PosXRunning
;
772 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
774 itemPosX
[i
] = row1PosXRunning
;
775 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
780 SwitchToGraphicsAndClear();
783 case MENU_FUNCTION_CLEANUP
:
787 case MENU_FUNCTION_PAINT_ALL
:
788 BltClearScreen(TRUE
);
789 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
790 // For PaintIcon() calls, the starting Y position is moved to the midpoint
791 // of the surrounding row; PaintIcon() adjusts this back up by half the
792 // icon's height to properly center it.
793 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
)))
794 PaintIcon(&egemb_arrow_left
, L
"icons\\arrow_left.icns", row0PosX
- TILE_XSPACING
,
795 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
796 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
)))
797 PaintIcon(&egemb_arrow_right
, L
"icons\\arrow_right.icns",
798 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
799 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
802 case MENU_FUNCTION_PAINT_SELECTION
:
803 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
806 case MENU_FUNCTION_PAINT_TIMEOUT
:
807 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
808 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
815 // user-callable dispatcher functions
818 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
820 MENU_STYLE_FUNC Style
= TextMenuStyle
;
822 if (AllowGraphicsMode
)
823 Style
= GraphicsMenuStyle
;
825 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
828 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
830 MENU_STYLE_FUNC Style
= TextMenuStyle
;
831 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
832 REFIT_MENU_ENTRY
*TempChosenEntry
;
834 UINTN DefaultEntryIndex
= -1;
836 if (DefaultSelection
!= NULL
) {
837 // Find a menu entry that includes *DefaultSelection as a substring
838 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
839 // If that didn't work, should we scan more characters? For now, no.
842 if (AllowGraphicsMode
) {
843 Style
= GraphicsMenuStyle
;
844 MainStyle
= MainMenuStyle
;
848 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
849 Screen
->TimeoutSeconds
= 0;
851 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
852 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
853 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
859 *ChosenEntry
= TempChosenEntry
;
861 } /* UINTN RunMainMenu() */