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)
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
;
89 // Graphics helper functions
92 static VOID
InitSelection(VOID
)
94 UINTN x
, y
, src_x
, src_y
;
95 EG_PIXEL
*DestPtr
, *SrcPtr
;
97 if (!AllowGraphicsMode
)
99 if (SelectionImages
[0] != NULL
)
102 // load small selection image
103 if (GlobalConfig
.SelectionSmallFileName
!= NULL
) {
104 SelectionImages
[2] = egLoadImage(SelfDir
, GlobalConfig
.SelectionSmallFileName
, FALSE
);
106 if (SelectionImages
[2] == NULL
)
107 SelectionImages
[2] = egPrepareEmbeddedImage(&egemb_back_selected_small
, FALSE
);
108 SelectionImages
[2] = egEnsureImageSize(SelectionImages
[2],
109 ROW1_TILESIZE
, ROW1_TILESIZE
, &MenuBackgroundPixel
);
110 if (SelectionImages
[2] == NULL
)
113 // load big selection image
114 if (GlobalConfig
.SelectionBigFileName
!= NULL
) {
115 SelectionImages
[0] = egLoadImage(SelfDir
, GlobalConfig
.SelectionBigFileName
, FALSE
);
116 SelectionImages
[0] = egEnsureImageSize(SelectionImages
[0],
117 ROW0_TILESIZE
, ROW0_TILESIZE
, &MenuBackgroundPixel
);
119 if (SelectionImages
[0] == NULL
) {
120 // calculate big selection image from small one
122 SelectionImages
[0] = egCreateImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
);
123 if (SelectionImages
[0] == NULL
) {
124 egFreeImage(SelectionImages
[2]);
125 SelectionImages
[2] = NULL
;
129 DestPtr
= SelectionImages
[0]->PixelData
;
130 SrcPtr
= SelectionImages
[2]->PixelData
;
131 for (y
= 0; y
< ROW0_TILESIZE
; y
++) {
132 if (y
< (ROW1_TILESIZE
>> 1))
134 else if (y
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
135 src_y
= (ROW1_TILESIZE
>> 1);
137 src_y
= y
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
139 for (x
= 0; x
< ROW0_TILESIZE
; x
++) {
140 if (x
< (ROW1_TILESIZE
>> 1))
142 else if (x
< (ROW0_TILESIZE
- (ROW1_TILESIZE
>> 1)))
143 src_x
= (ROW1_TILESIZE
>> 1);
145 src_x
= x
- (ROW0_TILESIZE
- ROW1_TILESIZE
);
147 *DestPtr
++ = SrcPtr
[src_y
* ROW1_TILESIZE
+ src_x
];
152 // non-selected background images
153 SelectionImages
[1] = egCreateFilledImage(ROW0_TILESIZE
, ROW0_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
154 SelectionImages
[3] = egCreateFilledImage(ROW1_TILESIZE
, ROW1_TILESIZE
, FALSE
, &MenuBackgroundPixel
);
158 // Scrolling functions
161 static VOID
InitScroll(OUT SCROLL_STATE
*State
, IN UINTN ItemCount
, IN UINTN VisibleSpace
)
163 State
->PreviousSelection
= State
->CurrentSelection
= 0;
164 State
->MaxIndex
= (INTN
)ItemCount
- 1;
165 State
->FirstVisible
= 0;
166 if (AllowGraphicsMode
) {
167 State
->MaxVisible
= UGAWidth
/ (ROW0_TILESIZE
+ TILE_XSPACING
) - 1;
169 State
->MaxVisible
= ConHeight
- 4;
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 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
179 // visible given the current scrolling condition....
180 static VOID
AdjustScrollState(IN SCROLL_STATE
*State
) {
181 if (State
->CurrentSelection
> State
->LastVisible
) {
182 State
->LastVisible
= State
->CurrentSelection
;
183 State
->FirstVisible
= 1 + State
->CurrentSelection
- State
->MaxVisible
;
184 if (State
->FirstVisible
< 0) // shouldn't happen, but just in case....
185 State
->FirstVisible
= 0;
186 State
->PaintAll
= TRUE
;
188 if (State
->CurrentSelection
< State
->FirstVisible
) {
189 State
->FirstVisible
= State
->CurrentSelection
;
190 State
->LastVisible
= State
->CurrentSelection
+ State
->MaxVisible
- 1;
191 State
->PaintAll
= TRUE
;
193 } // static VOID AdjustScrollState
195 static VOID
UpdateScroll(IN OUT SCROLL_STATE
*State
, IN UINTN Movement
)
197 State
->PreviousSelection
= State
->CurrentSelection
;
200 case SCROLL_LINE_LEFT
:
201 if (State
->CurrentSelection
> 0) {
202 State
->CurrentSelection
--;
206 case SCROLL_LINE_RIGHT
:
207 if (State
->CurrentSelection
< State
->MaxIndex
) {
208 State
->CurrentSelection
++;
213 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
214 if (State
->CurrentSelection
>= State
->InitialRow1
) {
215 if (State
->MaxIndex
> State
->InitialRow1
) { // avoid division by 0!
216 State
->CurrentSelection
= State
->FirstVisible
+ (State
->LastVisible
- State
->FirstVisible
) *
217 (State
->CurrentSelection
- State
->InitialRow1
) /
218 (State
->MaxIndex
- State
->InitialRow1
);
220 State
->CurrentSelection
= State
->FirstVisible
;
222 } // if in second row
224 if (State
->CurrentSelection
> 0)
225 State
->CurrentSelection
--;
229 case SCROLL_LINE_DOWN
:
230 if (State
->ScrollMode
== SCROLL_MODE_ICONS
) {
231 if (State
->CurrentSelection
<= State
->FinalRow0
) {
232 if (State
->LastVisible
> State
->FirstVisible
) { // avoid division by 0!
233 State
->CurrentSelection
= State
->InitialRow1
+ (State
->MaxIndex
- State
->InitialRow1
) *
234 (State
->CurrentSelection
- State
->FirstVisible
) /
235 (State
->LastVisible
- State
->FirstVisible
);
237 State
->CurrentSelection
= State
->InitialRow1
;
241 if (State
->CurrentSelection
< State
->MaxIndex
)
242 State
->CurrentSelection
++;
247 if (State
->CurrentSelection
<= State
->FinalRow0
)
248 State
->CurrentSelection
-= State
->MaxVisible
;
249 else if (State
->CurrentSelection
== State
->InitialRow1
)
250 State
->CurrentSelection
= State
->FinalRow0
;
252 State
->CurrentSelection
= State
->InitialRow1
;
253 if (State
->CurrentSelection
< 0)
254 State
->CurrentSelection
= 0;
258 if (State
->CurrentSelection
> 0) {
259 State
->PaintAll
= TRUE
;
260 State
->CurrentSelection
= 0;
264 case SCROLL_PAGE_DOWN
:
265 if (State
->CurrentSelection
< State
->FinalRow0
) {
266 State
->CurrentSelection
+= State
->MaxVisible
;
267 if (State
->CurrentSelection
> State
->FinalRow0
)
268 State
->CurrentSelection
= State
->FinalRow0
;
269 } else if (State
->CurrentSelection
== State
->FinalRow0
) {
270 State
->CurrentSelection
++;
272 State
->CurrentSelection
= State
->MaxIndex
;
274 if (State
->CurrentSelection
> State
->MaxIndex
)
275 State
->CurrentSelection
= State
->MaxIndex
;
279 if (State
->CurrentSelection
< State
->MaxIndex
) {
280 State
->PaintAll
= TRUE
;
281 State
->CurrentSelection
= State
->MaxIndex
;
289 if (State
->ScrollMode
== SCROLL_MODE_TEXT
)
290 AdjustScrollState(State
);
292 if (!State
->PaintAll
&& State
->CurrentSelection
!= State
->PreviousSelection
)
293 State
->PaintSelection
= TRUE
;
294 State
->LastVisible
= State
->FirstVisible
+ State
->MaxVisible
- 1;
295 } // static VOID UpdateScroll()
298 // menu helper functions
301 VOID
AddMenuInfoLine(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*InfoLine
)
303 AddListElement((VOID
***) &(Screen
->InfoLines
), &(Screen
->InfoLineCount
), InfoLine
);
306 VOID
AddMenuEntry(IN REFIT_MENU_SCREEN
*Screen
, IN REFIT_MENU_ENTRY
*Entry
)
308 AddListElement((VOID
***) &(Screen
->Entries
), &(Screen
->EntryCount
), Entry
);
312 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
316 if (Shortcut
== NULL
)
319 if (StrLen(Shortcut
) == 1) {
320 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
321 Shortcut
[0] -= ('a' - 'A');
323 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
324 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
329 } else if (StrLen(Shortcut
) > 1) {
330 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
331 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
338 // Identify the end of row 0 and the beginning of row 1; store the results in the
339 // appropriate fields in State. Also reduce MaxVisible if that value is greater
340 // than the total number of row-0 tags and if we're in an icon-based screen
341 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
344 State
->FinalRow0
= 0;
345 State
->InitialRow1
= State
->MaxIndex
;
346 for (i
= 0; i
< State
->MaxIndex
; i
++) {
347 if (Screen
->Entries
[i
]->Row
== 0) {
348 State
->FinalRow0
= i
;
349 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
350 State
->InitialRow1
= i
;
353 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
354 State
->MaxVisible
= State
->FinalRow0
+ 1;
355 } // static VOID IdentifyRows()
358 // generic menu function
360 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
367 BOOLEAN HaveTimeout
= FALSE
;
368 UINTN TimeoutCountdown
= 0;
369 CHAR16 TimeoutMessage
[256];
370 CHAR16 KeyAsString
[2];
373 if (Screen
->TimeoutSeconds
> 0) {
375 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
379 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
380 IdentifyRows(&State
, Screen
);
381 // override the starting selection with the default index, if any
382 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
383 State
.CurrentSelection
= DefaultEntryIndex
;
384 UpdateScroll(&State
, SCROLL_NONE
);
389 if (State
.PaintAll
) {
390 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
391 State
.PaintAll
= FALSE
;
392 } else if (State
.PaintSelection
) {
393 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
394 State
.PaintSelection
= FALSE
;
398 SPrint(TimeoutMessage
, 255, L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
399 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
402 // read key press (and wait for it if applicable)
403 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
404 if (Status
== EFI_NOT_READY
) {
405 if (HaveTimeout
&& TimeoutCountdown
== 0) {
407 MenuExit
= MENU_EXIT_TIMEOUT
;
409 } else if (HaveTimeout
) {
410 refit_call1_wrapper(BS
->Stall
, 100000);
413 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
417 // the user pressed a key, cancel the timeout
418 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
422 // react to key press
423 switch (key
.ScanCode
) {
425 UpdateScroll(&State
, SCROLL_LINE_UP
);
428 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
431 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
434 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
437 UpdateScroll(&State
, SCROLL_FIRST
);
440 UpdateScroll(&State
, SCROLL_LAST
);
443 UpdateScroll(&State
, SCROLL_PAGE_UP
);
446 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
449 MenuExit
= MENU_EXIT_ESCAPE
;
453 MenuExit
= MENU_EXIT_DETAILS
;
460 MenuExit
= MENU_EXIT_ESCAPE
;
463 switch (key
.UnicodeChar
) {
465 case CHAR_CARRIAGE_RETURN
:
467 MenuExit
= MENU_EXIT_ENTER
;
470 MenuExit
= MENU_EXIT_DETAILS
;
473 KeyAsString
[0] = key
.UnicodeChar
;
475 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
476 if (ShortcutEntry
>= 0) {
477 State
.CurrentSelection
= ShortcutEntry
;
478 MenuExit
= MENU_EXIT_ENTER
;
484 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
487 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
489 } /* static UINTN RunGenericMenu() */
492 // text-mode generic style
495 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
498 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
499 static UINTN MenuPosY
;
500 static CHAR16
**DisplayStrings
;
501 CHAR16 TimeoutMessage
[256];
503 State
->ScrollMode
= SCROLL_MODE_TEXT
;
506 case MENU_FUNCTION_INIT
:
509 if (Screen
->InfoLineCount
> 0)
510 MenuPosY
+= Screen
->InfoLineCount
+ 1;
511 MenuHeight
= ConHeight
- MenuPosY
;
512 if (Screen
->TimeoutSeconds
> 0)
514 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
516 // determine width of the menu
517 MenuWidth
= 20; // minimum
518 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
519 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
520 if (MenuWidth
< ItemWidth
)
521 MenuWidth
= ItemWidth
;
523 if (MenuWidth
> ConWidth
- 6)
524 MenuWidth
= ConWidth
- 6;
526 // prepare strings for display
527 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
528 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
529 DisplayStrings
[i
] = AllocateZeroPool(256 * sizeof(CHAR16
));
530 SPrint(DisplayStrings
[i
], 255, L
" %-.*s ", MenuWidth
, Screen
->Entries
[i
]->Title
);
531 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
532 // TODO: account for double-width characters
536 BeginTextScreen(Screen
->Title
);
537 if (Screen
->InfoLineCount
> 0) {
538 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
539 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
540 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
541 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
547 case MENU_FUNCTION_CLEANUP
:
548 // release temporary memory
549 for (i
= 0; i
<= State
->MaxIndex
; i
++)
550 MyFreePool(DisplayStrings
[i
]);
551 MyFreePool(DisplayStrings
);
554 case MENU_FUNCTION_PAINT_ALL
:
555 // paint the whole screen (initially and after scrolling)
556 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
557 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
558 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
559 if (i
== State
->CurrentSelection
)
560 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
562 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
563 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
566 // scrolling indicators
567 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
568 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
569 if (State
->FirstVisible
> 0)
570 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
572 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
573 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
574 if (State
->LastVisible
< State
->MaxIndex
)
575 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
577 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
580 case MENU_FUNCTION_PAINT_SELECTION
:
581 // redraw selection cursor
582 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
583 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
584 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
585 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
586 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
587 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
590 case MENU_FUNCTION_PAINT_TIMEOUT
:
591 if (ParamText
[0] == 0) {
593 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
594 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
595 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
597 // paint or update message
598 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
599 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
600 SPrint(TimeoutMessage
, 255, L
"%s ", ParamText
);
601 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
609 // graphical generic style
613 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
615 if (TextBuffer
== NULL
)
616 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
618 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
619 if (SelectedWidth
> 0) {
620 // draw selection bar background
621 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
622 &SelectionBackgroundPixel
);
626 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
627 BltImage(TextBuffer
, XPos
, YPos
);
630 // Displays sub-menus
631 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
635 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
637 State
->ScrollMode
= SCROLL_MODE_TEXT
;
640 case MENU_FUNCTION_INIT
:
641 InitScroll(State
, Screen
->EntryCount
, 0);
643 // determine width of the menu
644 MenuWidth
= 20; // minimum
645 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
646 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
647 if (MenuWidth
< ItemWidth
)
648 MenuWidth
= ItemWidth
;
650 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
651 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
652 if (MenuWidth
< ItemWidth
)
653 MenuWidth
= ItemWidth
;
655 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
656 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
657 MenuWidth
= LAYOUT_TEXT_WIDTH
;
659 if (Screen
->TitleImage
)
660 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
662 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
663 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
664 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
667 SwitchToGraphicsAndClear();
668 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
669 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
670 if (Screen
->TitleImage
)
671 BltImageAlpha(Screen
->TitleImage
,
672 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
673 &MenuBackgroundPixel
);
674 if (Screen
->InfoLineCount
> 0) {
675 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
676 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
677 EntriesPosY
+= TEXT_LINE_HEIGHT
;
679 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
684 case MENU_FUNCTION_CLEANUP
:
688 case MENU_FUNCTION_PAINT_ALL
:
689 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
690 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
691 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
695 case MENU_FUNCTION_PAINT_SELECTION
:
696 // redraw selection cursor
697 DrawMenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
698 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
699 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
700 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
703 case MENU_FUNCTION_PAINT_TIMEOUT
:
704 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
708 } // static VOID GraphicsMenuStyle()
711 // graphical main menu style
714 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
718 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
719 if (SelectionImages
!= NULL
)
720 BltImageCompositeBadge(SelectionImages
[ImageNum
],
721 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
724 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
726 UINTN TextWidth
, TextPosX
;
728 if (TextBuffer
== NULL
)
729 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
731 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
734 egMeasureText(Text
, &TextWidth
, NULL
);
735 if (TextWidth
> TextBuffer
->Width
)
738 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
739 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
740 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
741 BltImage(TextBuffer
, XPos
, YPos
);
744 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
745 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
748 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
749 AdjustScrollState(State
);
750 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
751 if (Screen
->Entries
[i
]->Row
== 0) {
752 if (i
<= State
->LastVisible
) {
753 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
754 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
757 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
760 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
761 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
762 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
763 } // static VOID PaintAll()
765 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
766 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
767 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
768 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
770 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
771 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
772 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
773 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
776 XSelectPrev
= State
->PreviousSelection
;
779 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
780 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
783 XSelectCur
= State
->CurrentSelection
;
786 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
787 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
788 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
789 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
790 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
791 } else { // Current selection not visible; must redraw the menu....
792 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
794 } // static VOID MoveSelection(VOID)
796 // Display an icon at the specified location. Uses the image specified by
797 // ExternalFilename if it's available, or BuiltInImage if it's not. The
798 // Y position is specified as the center value, and so is adjusted by half
799 // the icon's height. The X position is set along the icon's left
800 // edge if Alignment == ALIGN_LEFT, and along the right edge if
801 // Alignment == ALIGN_RIGHT
802 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
803 EG_IMAGE
*Icon
= NULL
;
805 if (FileExists(SelfDir
, ExternalFilename
))
806 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
808 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
810 if (Alignment
== ALIGN_RIGHT
)
812 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
814 } // static VOID PaintIcon()
816 // Display main menu in graphics mode
817 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
820 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
821 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
822 static UINTN
*itemPosX
;
823 static UINTN row0PosY
, textPosY
;
824 CHAR16 FileName
[256];
826 State
->ScrollMode
= SCROLL_MODE_ICONS
;
829 case MENU_FUNCTION_INIT
:
830 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
836 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
837 if (Screen
->Entries
[i
]->Row
== 1) {
841 if (row0Count
< State
->MaxVisible
)
845 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
846 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
847 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
848 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
850 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
854 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
855 row0PosXRunning
= row0PosX
;
856 row1PosXRunning
= row1PosX
;
857 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
858 if (Screen
->Entries
[i
]->Row
== 0) {
859 itemPosX
[i
] = row0PosXRunning
;
860 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
862 itemPosX
[i
] = row1PosXRunning
;
863 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
868 SwitchToGraphicsAndClear();
871 case MENU_FUNCTION_CLEANUP
:
872 MyFreePool(itemPosX
);
875 case MENU_FUNCTION_PAINT_ALL
:
876 BltClearScreen(TRUE
);
877 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
878 // For PaintIcon() calls, the starting Y position is moved to the midpoint
879 // of the surrounding row; PaintIcon() adjusts this back up by half the
880 // icon's height to properly center it.
881 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
882 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
883 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
884 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
886 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
887 SPrint(FileName
, 255, L
"%s\\arrow_right.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
888 PaintIcon(&egemb_arrow_right
, FileName
,
889 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
890 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
894 case MENU_FUNCTION_PAINT_SELECTION
:
895 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
898 case MENU_FUNCTION_PAINT_TIMEOUT
:
899 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
900 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
904 } // VOID MainMenuStyle()
906 // Enable the user to edit boot loader options.
907 // Returns TRUE if the user exited with edited options; FALSE if the user
908 // pressed Esc to terminate the edit.
909 static BOOLEAN
EditOptions(LOADER_ENTRY
*MenuEntry
) {
911 CHAR16
*EditedOptions
;
912 CHAR16 message
[] = L
"Use cursor keys to edit, Esc to exit, Enter to boot with edited options";
913 EG_PIXEL DarkBackgroundPixel
= { 0x0, 0x0, 0x0, 0 };
914 BOOLEAN retval
= FALSE
;
916 refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, ST
->ConOut
->Mode
->Mode
, &x_max
, &y_max
);
918 if (!GlobalConfig
.TextOnly
)
921 egClearScreen(&DarkBackgroundPixel
);
923 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, y_max
- 1);
924 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, message
);
926 if (line_edit(MenuEntry
->LoadOptions
, &EditedOptions
, x_max
, 1)) {
927 MyFreePool(MenuEntry
->LoadOptions
);
928 MenuEntry
->LoadOptions
= EditedOptions
;
931 if (!GlobalConfig
.TextOnly
)
934 } // VOID EditOptions()
937 // user-callable dispatcher functions
940 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
942 MENU_STYLE_FUNC Style
= TextMenuStyle
;
944 if (AllowGraphicsMode
)
945 Style
= GraphicsMenuStyle
;
947 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
950 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
952 MENU_STYLE_FUNC Style
= TextMenuStyle
;
953 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
954 REFIT_MENU_ENTRY
*TempChosenEntry
;
956 UINTN DefaultEntryIndex
= -1;
958 if (DefaultSelection
!= NULL
) {
959 // Find a menu entry that includes *DefaultSelection as a substring
960 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
961 // If that didn't work, should we scan more characters? For now, no.
964 if (AllowGraphicsMode
) {
965 Style
= GraphicsMenuStyle
;
966 MainStyle
= MainMenuStyle
;
970 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
971 Screen
->TimeoutSeconds
= 0;
973 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
974 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
975 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
977 if (MenuExit
== MENU_EXIT_DETAILS
) {
978 if (!EditOptions((LOADER_ENTRY
*) TempChosenEntry
))
985 *ChosenEntry
= TempChosenEntry
;
987 } /* UINTN RunMainMenu() */