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
;
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
);
311 VOID
FreeMenu(IN REFIT_MENU_SCREEN
*Screen
)
314 FreePool(Screen
->Entries
);
317 static INTN
FindMenuShortcutEntry(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
*Shortcut
)
321 if (Shortcut
== NULL
)
324 if (StrLen(Shortcut
) == 1) {
325 if (Shortcut
[0] >= 'a' && Shortcut
[0] <= 'z')
326 Shortcut
[0] -= ('a' - 'A');
328 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
329 if (Screen
->Entries
[i
]->ShortcutDigit
== Shortcut
[0] || Screen
->Entries
[i
]->ShortcutLetter
== Shortcut
[0]) {
334 } else if (StrLen(Shortcut
) > 1) {
335 for (i
= 0; i
< Screen
->EntryCount
; i
++) {
336 if (StriSubCmp(Shortcut
, Screen
->Entries
[i
]->Title
))
343 // Identify the end of row 0 and the beginning of row 1; store the results in the
344 // appropriate fields in State. Also reduce MaxVisible if that value is greater
345 // than the total number of row-0 tags and if we're in an icon-based screen
346 static VOID
IdentifyRows(IN SCROLL_STATE
*State
, IN REFIT_MENU_SCREEN
*Screen
) {
349 State
->FinalRow0
= 0;
350 State
->InitialRow1
= State
->MaxIndex
;
351 for (i
= 0; i
< State
->MaxIndex
; i
++) {
352 if (Screen
->Entries
[i
]->Row
== 0) {
353 State
->FinalRow0
= i
;
354 } else if ((Screen
->Entries
[i
]->Row
== 1) && (State
->InitialRow1
> i
)) {
355 State
->InitialRow1
= i
;
358 if ((State
->ScrollMode
== SCROLL_MODE_ICONS
) && (State
->MaxVisible
> (State
->FinalRow0
+ 1)))
359 State
->MaxVisible
= State
->FinalRow0
+ 1;
360 } // static VOID IdentifyRows()
363 // generic menu function
365 static UINTN
RunGenericMenu(IN REFIT_MENU_SCREEN
*Screen
, IN MENU_STYLE_FUNC StyleFunc
, IN INTN DefaultEntryIndex
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
372 BOOLEAN HaveTimeout
= FALSE
;
373 UINTN TimeoutCountdown
= 0;
374 CHAR16
*TimeoutMessage
;
375 CHAR16 KeyAsString
[2];
378 if (Screen
->TimeoutSeconds
> 0) {
380 TimeoutCountdown
= Screen
->TimeoutSeconds
* 10;
384 StyleFunc(Screen
, &State
, MENU_FUNCTION_INIT
, NULL
);
385 IdentifyRows(&State
, Screen
);
386 // override the starting selection with the default index, if any
387 if (DefaultEntryIndex
>= 0 && DefaultEntryIndex
<= State
.MaxIndex
) {
388 State
.CurrentSelection
= DefaultEntryIndex
;
389 UpdateScroll(&State
, SCROLL_NONE
);
394 if (State
.PaintAll
) {
395 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
396 State
.PaintAll
= FALSE
;
397 } else if (State
.PaintSelection
) {
398 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_SELECTION
, NULL
);
399 State
.PaintSelection
= FALSE
;
403 TimeoutMessage
= PoolPrint(L
"%s in %d seconds", Screen
->TimeoutText
, (TimeoutCountdown
+ 5) / 10);
404 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, TimeoutMessage
);
405 FreePool(TimeoutMessage
);
408 // read key press (and wait for it if applicable)
409 Status
= refit_call2_wrapper(ST
->ConIn
->ReadKeyStroke
, ST
->ConIn
, &key
);
410 if (Status
== EFI_NOT_READY
) {
411 if (HaveTimeout
&& TimeoutCountdown
== 0) {
413 MenuExit
= MENU_EXIT_TIMEOUT
;
415 } else if (HaveTimeout
) {
416 refit_call1_wrapper(BS
->Stall
, 100000);
419 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &index
);
423 // the user pressed a key, cancel the timeout
424 StyleFunc(Screen
, &State
, MENU_FUNCTION_PAINT_TIMEOUT
, L
"");
428 // react to key press
429 switch (key
.ScanCode
) {
431 UpdateScroll(&State
, SCROLL_LINE_UP
);
434 UpdateScroll(&State
, SCROLL_LINE_LEFT
);
437 UpdateScroll(&State
, SCROLL_LINE_DOWN
);
440 UpdateScroll(&State
, SCROLL_LINE_RIGHT
);
443 UpdateScroll(&State
, SCROLL_FIRST
);
446 UpdateScroll(&State
, SCROLL_LAST
);
449 UpdateScroll(&State
, SCROLL_PAGE_UP
);
452 UpdateScroll(&State
, SCROLL_PAGE_DOWN
);
455 MenuExit
= MENU_EXIT_ESCAPE
;
459 MenuExit
= MENU_EXIT_DETAILS
;
465 switch (key
.UnicodeChar
) {
467 case CHAR_CARRIAGE_RETURN
:
469 MenuExit
= MENU_EXIT_ENTER
;
472 MenuExit
= MENU_EXIT_DETAILS
;
475 KeyAsString
[0] = key
.UnicodeChar
;
477 ShortcutEntry
= FindMenuShortcutEntry(Screen
, KeyAsString
);
478 if (ShortcutEntry
>= 0) {
479 State
.CurrentSelection
= ShortcutEntry
;
480 MenuExit
= MENU_EXIT_ENTER
;
486 StyleFunc(Screen
, &State
, MENU_FUNCTION_CLEANUP
, NULL
);
489 *ChosenEntry
= Screen
->Entries
[State
.CurrentSelection
];
491 } /* static UINTN RunGenericMenu( */
494 // text-mode generic style
497 static VOID
TextMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
500 UINTN MenuWidth
, ItemWidth
, MenuHeight
;
501 static UINTN MenuPosY
;
502 static CHAR16
**DisplayStrings
;
503 CHAR16
*TimeoutMessage
;
505 State
->ScrollMode
= SCROLL_MODE_TEXT
;
508 case MENU_FUNCTION_INIT
:
511 if (Screen
->InfoLineCount
> 0)
512 MenuPosY
+= Screen
->InfoLineCount
+ 1;
513 MenuHeight
= ConHeight
- MenuPosY
;
514 if (Screen
->TimeoutSeconds
> 0)
516 InitScroll(State
, Screen
->EntryCount
, MenuHeight
);
518 // determine width of the menu
519 MenuWidth
= 20; // minimum
520 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
521 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
522 if (MenuWidth
< ItemWidth
)
523 MenuWidth
= ItemWidth
;
525 if (MenuWidth
> ConWidth
- 6)
526 MenuWidth
= ConWidth
- 6;
528 // prepare strings for display
529 DisplayStrings
= AllocatePool(sizeof(CHAR16
*) * Screen
->EntryCount
);
530 for (i
= 0; i
<= State
->MaxIndex
; i
++)
531 DisplayStrings
[i
] = PoolPrint(L
" %-.*s ", MenuWidth
, Screen
->Entries
[i
]->Title
);
532 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
533 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
534 // TODO: account for double-width characters
537 BeginTextScreen(Screen
->Title
);
538 if (Screen
->InfoLineCount
> 0) {
539 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
540 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
541 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, 4 + i
);
542 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, Screen
->InfoLines
[i
]);
548 case MENU_FUNCTION_CLEANUP
:
549 // release temporary memory
550 for (i
= 0; i
<= State
->MaxIndex
; i
++)
551 FreePool(DisplayStrings
[i
]);
552 FreePool(DisplayStrings
);
555 case MENU_FUNCTION_PAINT_ALL
:
556 // paint the whole screen (initially and after scrolling)
557 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
558 if (i
>= State
->FirstVisible
&& i
<= State
->LastVisible
) {
559 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (i
- State
->FirstVisible
));
560 if (i
== State
->CurrentSelection
)
561 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
563 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
564 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[i
]);
567 // scrolling indicators
568 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_SCROLLARROW
);
569 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
);
570 if (State
->FirstVisible
> 0)
571 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowUp
);
573 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
574 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, MenuPosY
+ State
->MaxVisible
);
575 if (State
->LastVisible
< State
->MaxIndex
)
576 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, ArrowDown
);
578 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, L
" ");
581 case MENU_FUNCTION_PAINT_SELECTION
:
582 // redraw selection cursor
583 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->PreviousSelection
- State
->FirstVisible
));
584 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_BASIC
);
585 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->PreviousSelection
]);
586 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 2, MenuPosY
+ (State
->CurrentSelection
- State
->FirstVisible
));
587 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_CHOICE_CURRENT
);
588 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, DisplayStrings
[State
->CurrentSelection
]);
591 case MENU_FUNCTION_PAINT_TIMEOUT
:
592 if (ParamText
[0] == 0) {
594 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_BASIC
);
595 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 0, ConHeight
- 1);
596 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, BlankLine
+ 1);
598 // paint or update message
599 refit_call2_wrapper(ST
->ConOut
->SetAttribute
, ST
->ConOut
, ATTR_ERROR
);
600 refit_call3_wrapper(ST
->ConOut
->SetCursorPosition
, ST
->ConOut
, 3, ConHeight
- 1);
601 TimeoutMessage
= PoolPrint(L
"%s ", ParamText
);
602 refit_call2_wrapper(ST
->ConOut
->OutputString
, ST
->ConOut
, TimeoutMessage
);
603 FreePool(TimeoutMessage
);
611 // graphical generic style
615 static VOID
DrawMenuText(IN CHAR16
*Text
, IN UINTN SelectedWidth
, IN UINTN XPos
, IN UINTN YPos
)
617 if (TextBuffer
== NULL
)
618 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
620 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
621 if (SelectedWidth
> 0) {
622 // draw selection bar background
623 egFillImageArea(TextBuffer
, 0, 0, SelectedWidth
, TextBuffer
->Height
,
624 &SelectionBackgroundPixel
);
628 egRenderText(Text
, TextBuffer
, TEXT_XMARGIN
, TEXT_YMARGIN
);
629 BltImage(TextBuffer
, XPos
, YPos
);
632 // Displays sub-menus
633 static VOID
GraphicsMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
637 static UINTN MenuWidth
, EntriesPosX
, EntriesPosY
, TimeoutPosY
;
639 State
->ScrollMode
= SCROLL_MODE_TEXT
;
642 case MENU_FUNCTION_INIT
:
643 InitScroll(State
, Screen
->EntryCount
, 0);
645 // determine width of the menu
646 MenuWidth
= 20; // minimum
647 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
648 ItemWidth
= StrLen(Screen
->InfoLines
[i
]);
649 if (MenuWidth
< ItemWidth
)
650 MenuWidth
= ItemWidth
;
652 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
653 ItemWidth
= StrLen(Screen
->Entries
[i
]->Title
);
654 if (MenuWidth
< ItemWidth
)
655 MenuWidth
= ItemWidth
;
657 MenuWidth
= TEXT_XMARGIN
* 2 + MenuWidth
* FONT_CELL_WIDTH
;
658 if (MenuWidth
> LAYOUT_TEXT_WIDTH
)
659 MenuWidth
= LAYOUT_TEXT_WIDTH
;
661 if (Screen
->TitleImage
)
662 EntriesPosX
= (UGAWidth
+ (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
) - MenuWidth
) >> 1;
664 EntriesPosX
= (UGAWidth
- MenuWidth
) >> 1;
665 EntriesPosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
+ TEXT_LINE_HEIGHT
* 2;
666 TimeoutPosY
= EntriesPosY
+ (Screen
->EntryCount
+ 1) * TEXT_LINE_HEIGHT
;
669 SwitchToGraphicsAndClear();
670 egMeasureText(Screen
->Title
, &ItemWidth
, NULL
);
671 DrawMenuText(Screen
->Title
, 0, ((UGAWidth
- ItemWidth
) >> 1) - TEXT_XMARGIN
, EntriesPosY
- TEXT_LINE_HEIGHT
* 2);
672 if (Screen
->TitleImage
)
673 BltImageAlpha(Screen
->TitleImage
,
674 EntriesPosX
- (Screen
->TitleImage
->Width
+ TITLEICON_SPACING
), EntriesPosY
,
675 &MenuBackgroundPixel
);
676 if (Screen
->InfoLineCount
> 0) {
677 for (i
= 0; i
< (INTN
)Screen
->InfoLineCount
; i
++) {
678 DrawMenuText(Screen
->InfoLines
[i
], 0, EntriesPosX
, EntriesPosY
);
679 EntriesPosY
+= TEXT_LINE_HEIGHT
;
681 EntriesPosY
+= TEXT_LINE_HEIGHT
; // also add a blank line
686 case MENU_FUNCTION_CLEANUP
:
690 case MENU_FUNCTION_PAINT_ALL
:
691 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
692 DrawMenuText(Screen
->Entries
[i
]->Title
, (i
== State
->CurrentSelection
) ? MenuWidth
: 0,
693 EntriesPosX
, EntriesPosY
+ i
* TEXT_LINE_HEIGHT
);
697 case MENU_FUNCTION_PAINT_SELECTION
:
698 // redraw selection cursor
699 DrawMenuText(Screen
->Entries
[State
->PreviousSelection
]->Title
, 0,
700 EntriesPosX
, EntriesPosY
+ State
->PreviousSelection
* TEXT_LINE_HEIGHT
);
701 DrawMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
, MenuWidth
,
702 EntriesPosX
, EntriesPosY
+ State
->CurrentSelection
* TEXT_LINE_HEIGHT
);
705 case MENU_FUNCTION_PAINT_TIMEOUT
:
706 DrawMenuText(ParamText
, 0, EntriesPosX
, TimeoutPosY
);
713 // graphical main menu style
716 static VOID
DrawMainMenuEntry(REFIT_MENU_ENTRY
*Entry
, BOOLEAN selected
, UINTN XPos
, UINTN YPos
)
720 ImageNum
= ((Entry
->Row
== 0) ? 0 : 2) + (selected
? 0 : 1);
721 if (SelectionImages
!= NULL
)
722 BltImageCompositeBadge(SelectionImages
[ImageNum
],
723 Entry
->Image
, Entry
->BadgeImage
, XPos
, YPos
);
726 static VOID
DrawMainMenuText(IN CHAR16
*Text
, IN UINTN XPos
, IN UINTN YPos
)
728 UINTN TextWidth
, TextPosX
;
730 if (TextBuffer
== NULL
)
731 TextBuffer
= egCreateImage(LAYOUT_TEXT_WIDTH
, TEXT_LINE_HEIGHT
, FALSE
);
733 egFillImage(TextBuffer
, &MenuBackgroundPixel
);
736 egMeasureText(Text
, &TextWidth
, NULL
);
737 if (TextWidth
> TextBuffer
->Width
)
740 TextPosX
= (TextBuffer
->Width
- TextWidth
) / 2;
741 egRenderText(Text
, TextBuffer
, TextPosX
, 0);
742 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
743 BltImage(TextBuffer
, XPos
, YPos
);
746 static VOID
PaintAll(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
747 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
750 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0)
751 AdjustScrollState(State
);
752 for (i
= State
->FirstVisible
; i
<= State
->MaxIndex
; i
++) {
753 if (Screen
->Entries
[i
]->Row
== 0) {
754 if (i
<= State
->LastVisible
) {
755 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
,
756 itemPosX
[i
- State
->FirstVisible
], row0PosY
);
759 DrawMainMenuEntry(Screen
->Entries
[i
], (i
== State
->CurrentSelection
) ? TRUE
: FALSE
, itemPosX
[i
], row1PosY
);
762 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
763 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
764 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
765 } // static VOID PaintAll()
767 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
768 static VOID
PaintSelection(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, UINTN
*itemPosX
,
769 UINTN row0PosY
, UINTN row1PosY
, UINTN textPosY
) {
770 UINTN XSelectPrev
, XSelectCur
, YPosPrev
, YPosCur
;
772 if (((State
->CurrentSelection
<= State
->LastVisible
) && (State
->CurrentSelection
>= State
->FirstVisible
)) ||
773 (State
->CurrentSelection
>= State
->InitialRow1
) ) {
774 if (Screen
->Entries
[State
->PreviousSelection
]->Row
== 0) {
775 XSelectPrev
= State
->PreviousSelection
- State
->FirstVisible
;
778 XSelectPrev
= State
->PreviousSelection
;
781 if (Screen
->Entries
[State
->CurrentSelection
]->Row
== 0) {
782 XSelectCur
= State
->CurrentSelection
- State
->FirstVisible
;
785 XSelectCur
= State
->CurrentSelection
;
788 DrawMainMenuEntry(Screen
->Entries
[State
->PreviousSelection
], FALSE
, itemPosX
[XSelectPrev
], YPosPrev
);
789 DrawMainMenuEntry(Screen
->Entries
[State
->CurrentSelection
], TRUE
, itemPosX
[XSelectCur
], YPosCur
);
790 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
791 DrawMainMenuText(Screen
->Entries
[State
->CurrentSelection
]->Title
,
792 (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
);
793 } else { // Current selection not visible; must redraw the menu....
794 MainMenuStyle(Screen
, State
, MENU_FUNCTION_PAINT_ALL
, NULL
);
796 } // static VOID MoveSelection(VOID)
798 // Display an icon at the specified location. Uses the image specified by
799 // ExternalFilename if it's available, or BuiltInImage if it's not. The
800 // Y position is specified as the center value, and so is adjusted by half
801 // the icon's height. The X position is set along the icon's left
802 // edge if Alignment == ALIGN_LEFT, and along the right edge if
803 // Alignment == ALIGN_RIGHT
804 static VOID
PaintIcon(IN EG_EMBEDDED_IMAGE
*BuiltInIcon
, IN CHAR16
*ExternalFilename
, UINTN PosX
, UINTN PosY
, UINTN Alignment
) {
805 EG_IMAGE
*Icon
= NULL
;
807 if (FileExists(SelfDir
, ExternalFilename
))
808 Icon
= egLoadIcon(SelfDir
, ExternalFilename
, 48);
810 Icon
= egPrepareEmbeddedImage(BuiltInIcon
, TRUE
);
812 if (Alignment
== ALIGN_RIGHT
)
814 BltImageAlpha(Icon
, PosX
, PosY
- (Icon
->Height
/ 2), &MenuBackgroundPixel
);
816 } // static VOID PaintArrow()
818 // Display main menu in graphics mode
819 VOID
MainMenuStyle(IN REFIT_MENU_SCREEN
*Screen
, IN SCROLL_STATE
*State
, IN UINTN Function
, IN CHAR16
*ParamText
)
822 static UINTN row0PosX
, row0PosXRunning
, row1PosY
, row0Loaders
;
823 UINTN row0Count
, row1Count
, row1PosX
, row1PosXRunning
;
824 static UINTN
*itemPosX
;
825 static UINTN row0PosY
, textPosY
;
826 CHAR16 FileName
[256];
828 State
->ScrollMode
= SCROLL_MODE_ICONS
;
831 case MENU_FUNCTION_INIT
:
832 InitScroll(State
, Screen
->EntryCount
, GlobalConfig
.MaxTags
);
838 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
839 if (Screen
->Entries
[i
]->Row
== 1) {
843 if (row0Count
< State
->MaxVisible
)
847 row0PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW0_TILESIZE
+ TILE_XSPACING
) * row0Count
) >> 1;
848 row0PosY
= ((UGAHeight
- LAYOUT_TOTAL_HEIGHT
) >> 1) + LAYOUT_BANNER_YOFFSET
;
849 row1PosX
= (UGAWidth
+ TILE_XSPACING
- (ROW1_TILESIZE
+ TILE_XSPACING
) * row1Count
) >> 1;
850 row1PosY
= row0PosY
+ ROW0_TILESIZE
+ TILE_YSPACING
;
852 textPosY
= row1PosY
+ ROW1_TILESIZE
+ TILE_YSPACING
;
856 itemPosX
= AllocatePool(sizeof(UINTN
) * Screen
->EntryCount
);
857 row0PosXRunning
= row0PosX
;
858 row1PosXRunning
= row1PosX
;
859 for (i
= 0; i
<= State
->MaxIndex
; i
++) {
860 if (Screen
->Entries
[i
]->Row
== 0) {
861 itemPosX
[i
] = row0PosXRunning
;
862 row0PosXRunning
+= ROW0_TILESIZE
+ TILE_XSPACING
;
864 itemPosX
[i
] = row1PosXRunning
;
865 row1PosXRunning
+= ROW1_TILESIZE
+ TILE_XSPACING
;
870 SwitchToGraphicsAndClear();
873 case MENU_FUNCTION_CLEANUP
:
877 case MENU_FUNCTION_PAINT_ALL
:
878 BltClearScreen(TRUE
);
879 PaintAll(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
880 // For PaintIcon() calls, the starting Y position is moved to the midpoint
881 // of the surrounding row; PaintIcon() adjusts this back up by half the
882 // icon's height to properly center it.
883 if ((State
->FirstVisible
> 0) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
884 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
885 PaintIcon(&egemb_arrow_left
, FileName
, row0PosX
- TILE_XSPACING
,
886 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_RIGHT
);
888 if ((State
->LastVisible
< (row0Loaders
- 1)) && (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_ARROWS
))) {
889 SPrint(FileName
, 255, L
"%s\\arrow_left.icns", GlobalConfig
.IconsDir
? GlobalConfig
.IconsDir
: DEFAULT_ICONS_DIR
);
890 PaintIcon(&egemb_arrow_right
, FileName
,
891 (UGAWidth
+ (ROW0_TILESIZE
+ TILE_XSPACING
) * State
->MaxVisible
) / 2 + TILE_XSPACING
,
892 row0PosY
+ (ROW0_TILESIZE
/ 2), ALIGN_LEFT
);
896 case MENU_FUNCTION_PAINT_SELECTION
:
897 PaintSelection(Screen
, State
, itemPosX
, row0PosY
, row1PosY
, textPosY
);
900 case MENU_FUNCTION_PAINT_TIMEOUT
:
901 if (!(GlobalConfig
.HideUIFlags
& HIDEUI_FLAG_LABEL
))
902 DrawMainMenuText(ParamText
, (UGAWidth
- LAYOUT_TEXT_WIDTH
) >> 1, textPosY
+ TEXT_LINE_HEIGHT
);
909 // user-callable dispatcher functions
912 UINTN
RunMenu(IN REFIT_MENU_SCREEN
*Screen
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
914 MENU_STYLE_FUNC Style
= TextMenuStyle
;
916 if (AllowGraphicsMode
)
917 Style
= GraphicsMenuStyle
;
919 return RunGenericMenu(Screen
, Style
, -1, ChosenEntry
);
922 UINTN
RunMainMenu(IN REFIT_MENU_SCREEN
*Screen
, IN CHAR16
* DefaultSelection
, OUT REFIT_MENU_ENTRY
**ChosenEntry
)
924 MENU_STYLE_FUNC Style
= TextMenuStyle
;
925 MENU_STYLE_FUNC MainStyle
= TextMenuStyle
;
926 REFIT_MENU_ENTRY
*TempChosenEntry
;
928 UINTN DefaultEntryIndex
= -1;
930 if (DefaultSelection
!= NULL
) {
931 // Find a menu entry that includes *DefaultSelection as a substring
932 DefaultEntryIndex
= FindMenuShortcutEntry(Screen
, DefaultSelection
);
933 // If that didn't work, should we scan more characters? For now, no.
936 if (AllowGraphicsMode
) {
937 Style
= GraphicsMenuStyle
;
938 MainStyle
= MainMenuStyle
;
942 MenuExit
= RunGenericMenu(Screen
, MainStyle
, DefaultEntryIndex
, &TempChosenEntry
);
943 Screen
->TimeoutSeconds
= 0;
945 if (MenuExit
== MENU_EXIT_DETAILS
&& TempChosenEntry
->SubScreen
!= NULL
) {
946 MenuExit
= RunGenericMenu(TempChosenEntry
->SubScreen
, Style
, -1, &TempChosenEntry
);
947 if (MenuExit
== MENU_EXIT_ESCAPE
|| TempChosenEntry
->Tag
== TAG_RETURN
)
953 *ChosenEntry
= TempChosenEntry
;
955 } /* UINTN RunMainMenu() */