]> code.delx.au - refind/blob - refind/menu.c
Merge commit 'd32e77051a063ae5ddd9fa370d4f25c3e9408f0e'
[refind] / refind / menu.c
1 /*
2 * refit/menu.c
3 * Menu functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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.
23 *
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.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
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.
42 *
43 */
44
45 #include "global.h"
46 #include "screen.h"
47 #include "lib.h"
48 #include "menu.h"
49 #include "config.h"
50 #include "libeg.h"
51 #include "libegint.h"
52 #include "../include/refit_call_wrapper.h"
53
54 #include "../include/egemb_back_selected_small.h"
55 #include "../include/egemb_back_selected_big.h"
56 #include "../include/egemb_arrow_left.h"
57 #include "../include/egemb_arrow_right.h"
58
59 // other menu definitions
60
61 #define MENU_FUNCTION_INIT (0)
62 #define MENU_FUNCTION_CLEANUP (1)
63 #define MENU_FUNCTION_PAINT_ALL (2)
64 #define MENU_FUNCTION_PAINT_SELECTION (3)
65 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
66 #define MENU_FUNCTION_PAINT_HINTS (5)
67
68 typedef VOID (*MENU_STYLE_FUNC)(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText);
69
70 static CHAR16 ArrowUp[2] = { ARROW_UP, 0 };
71 static CHAR16 ArrowDown[2] = { ARROW_DOWN, 0 };
72 static UINTN TileSizes[2] = { 144, 64 };
73
74 // Text and icon spacing constants....
75 #define TEXT_YMARGIN (2)
76 #define TITLEICON_SPACING (16)
77
78 #define TILE_XSPACING (8)
79 #define TILE_YSPACING (16)
80
81 // Alignment values for PaintIcon()
82 #define ALIGN_RIGHT 1
83 #define ALIGN_LEFT 0
84
85 static EG_IMAGE *SelectionImages[2] = { NULL, NULL };
86 static EG_PIXEL SelectionBackgroundPixel = { 0xff, 0xff, 0xff, 0 };
87
88 //
89 // Graphics helper functions
90 //
91
92 static VOID InitSelection(VOID)
93 {
94 EG_IMAGE *TempSmallImage = NULL, *TempBigImage = NULL;
95 BOOLEAN LoadedSmallImage = FALSE;
96
97 if (!AllowGraphicsMode)
98 return;
99 if (SelectionImages[0] != NULL)
100 return;
101
102 // load small selection image
103 if (GlobalConfig.SelectionSmallFileName != NULL) {
104 TempSmallImage = egLoadImage(SelfDir, GlobalConfig.SelectionSmallFileName, TRUE);
105 }
106 if (TempSmallImage == NULL)
107 TempSmallImage = egPrepareEmbeddedImage(&egemb_back_selected_small, TRUE);
108 else
109 LoadedSmallImage = TRUE;
110 SelectionImages[1] = egScaleImage(TempSmallImage, TileSizes[1], TileSizes[1]);
111
112 // load big selection image
113 if (GlobalConfig.SelectionBigFileName != NULL) {
114 TempBigImage = egLoadImage(SelfDir, GlobalConfig.SelectionBigFileName, TRUE);
115 }
116 if (TempBigImage == NULL) {
117 if (LoadedSmallImage) {
118 // calculate big selection image from small one
119 TempBigImage = egCopyImage(TempSmallImage);
120 } else {
121 TempBigImage = egPrepareEmbeddedImage(&egemb_back_selected_big, TRUE);
122 }
123 }
124 SelectionImages[0] = egScaleImage(TempBigImage, TileSizes[0], TileSizes[0]);
125
126 if (TempSmallImage)
127 egFreeImage(TempSmallImage);
128 if (TempBigImage)
129 egFreeImage(TempBigImage);
130 } // VOID InitSelection()
131
132 //
133 // Scrolling functions
134 //
135
136 static VOID InitScroll(OUT SCROLL_STATE *State, IN UINTN ItemCount, IN UINTN VisibleSpace)
137 {
138 State->PreviousSelection = State->CurrentSelection = 0;
139 State->MaxIndex = (INTN)ItemCount - 1;
140 State->FirstVisible = 0;
141 if (AllowGraphicsMode) {
142 State->MaxVisible = UGAWidth / (TileSizes[0] + TILE_XSPACING) - 1;
143 } else
144 State->MaxVisible = ConHeight - 4;
145 if ((VisibleSpace > 0) && (VisibleSpace < State->MaxVisible))
146 State->MaxVisible = (INTN)VisibleSpace;
147 State->PaintAll = TRUE;
148 State->PaintSelection = FALSE;
149
150 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
151 }
152
153 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
154 // visible given the current scrolling condition....
155 static VOID AdjustScrollState(IN SCROLL_STATE *State) {
156 if (State->CurrentSelection > State->LastVisible) {
157 State->LastVisible = State->CurrentSelection;
158 State->FirstVisible = 1 + State->CurrentSelection - State->MaxVisible;
159 if (State->FirstVisible < 0) // shouldn't happen, but just in case....
160 State->FirstVisible = 0;
161 State->PaintAll = TRUE;
162 } // Scroll forward
163 if (State->CurrentSelection < State->FirstVisible) {
164 State->FirstVisible = State->CurrentSelection;
165 State->LastVisible = State->CurrentSelection + State->MaxVisible - 1;
166 State->PaintAll = TRUE;
167 } // Scroll backward
168 } // static VOID AdjustScrollState
169
170 static VOID UpdateScroll(IN OUT SCROLL_STATE *State, IN UINTN Movement)
171 {
172 State->PreviousSelection = State->CurrentSelection;
173
174 switch (Movement) {
175 case SCROLL_LINE_LEFT:
176 if (State->CurrentSelection > 0) {
177 State->CurrentSelection --;
178 }
179 break;
180
181 case SCROLL_LINE_RIGHT:
182 if (State->CurrentSelection < State->MaxIndex) {
183 State->CurrentSelection ++;
184 }
185 break;
186
187 case SCROLL_LINE_UP:
188 if (State->ScrollMode == SCROLL_MODE_ICONS) {
189 if (State->CurrentSelection >= State->InitialRow1) {
190 if (State->MaxIndex > State->InitialRow1) { // avoid division by 0!
191 State->CurrentSelection = State->FirstVisible + (State->LastVisible - State->FirstVisible) *
192 (State->CurrentSelection - State->InitialRow1) /
193 (State->MaxIndex - State->InitialRow1);
194 } else {
195 State->CurrentSelection = State->FirstVisible;
196 } // if/else
197 } // if in second row
198 } else {
199 if (State->CurrentSelection > 0)
200 State->CurrentSelection--;
201 } // if/else
202 break;
203
204 case SCROLL_LINE_DOWN:
205 if (State->ScrollMode == SCROLL_MODE_ICONS) {
206 if (State->CurrentSelection <= State->FinalRow0) {
207 if (State->LastVisible > State->FirstVisible) { // avoid division by 0!
208 State->CurrentSelection = State->InitialRow1 + (State->MaxIndex - State->InitialRow1) *
209 (State->CurrentSelection - State->FirstVisible) /
210 (State->LastVisible - State->FirstVisible);
211 } else {
212 State->CurrentSelection = State->InitialRow1;
213 } // if/else
214 } // if in first row
215 } else {
216 if (State->CurrentSelection < State->MaxIndex)
217 State->CurrentSelection++;
218 } // if/else
219 break;
220
221 case SCROLL_PAGE_UP:
222 if (State->CurrentSelection <= State->FinalRow0)
223 State->CurrentSelection -= State->MaxVisible;
224 else if (State->CurrentSelection == State->InitialRow1)
225 State->CurrentSelection = State->FinalRow0;
226 else
227 State->CurrentSelection = State->InitialRow1;
228 if (State->CurrentSelection < 0)
229 State->CurrentSelection = 0;
230 break;
231
232 case SCROLL_FIRST:
233 if (State->CurrentSelection > 0) {
234 State->PaintAll = TRUE;
235 State->CurrentSelection = 0;
236 }
237 break;
238
239 case SCROLL_PAGE_DOWN:
240 if (State->CurrentSelection < State->FinalRow0) {
241 State->CurrentSelection += State->MaxVisible;
242 if (State->CurrentSelection > State->FinalRow0)
243 State->CurrentSelection = State->FinalRow0;
244 } else if (State->CurrentSelection == State->FinalRow0) {
245 State->CurrentSelection++;
246 } else {
247 State->CurrentSelection = State->MaxIndex;
248 }
249 if (State->CurrentSelection > State->MaxIndex)
250 State->CurrentSelection = State->MaxIndex;
251 break;
252
253 case SCROLL_LAST:
254 if (State->CurrentSelection < State->MaxIndex) {
255 State->PaintAll = TRUE;
256 State->CurrentSelection = State->MaxIndex;
257 }
258 break;
259
260 case SCROLL_NONE:
261 break;
262
263 }
264 if (State->ScrollMode == SCROLL_MODE_TEXT)
265 AdjustScrollState(State);
266
267 if (!State->PaintAll && State->CurrentSelection != State->PreviousSelection)
268 State->PaintSelection = TRUE;
269 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
270 } // static VOID UpdateScroll()
271
272 //
273 // menu helper functions
274 //
275
276 VOID AddMenuInfoLine(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *InfoLine)
277 {
278 AddListElement((VOID ***) &(Screen->InfoLines), &(Screen->InfoLineCount), InfoLine);
279 }
280
281 VOID AddMenuEntry(IN REFIT_MENU_SCREEN *Screen, IN REFIT_MENU_ENTRY *Entry)
282 {
283 AddListElement((VOID ***) &(Screen->Entries), &(Screen->EntryCount), Entry);
284 }
285
286
287 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Defaults)
288 {
289 UINTN i, j = 0;
290 CHAR16 *Shortcut;
291
292 while ((Shortcut = FindCommaDelimited(Defaults, j)) != NULL) {
293 if (StrLen(Shortcut) == 1) {
294 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
295 Shortcut[0] -= ('a' - 'A');
296 if (Shortcut[0]) {
297 for (i = 0; i < Screen->EntryCount; i++) {
298 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
299 MyFreePool(Shortcut);
300 return i;
301 } // if
302 } // for
303 } // if
304 } else if (StrLen(Shortcut) > 1) {
305 for (i = 0; i < Screen->EntryCount; i++) {
306 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title)) {
307 MyFreePool(Shortcut);
308 return i;
309 } // if
310 } // for
311 }
312 MyFreePool(Shortcut);
313 j++;
314 } // while()
315 return -1;
316 }
317
318 // Identify the end of row 0 and the beginning of row 1; store the results in the
319 // appropriate fields in State. Also reduce MaxVisible if that value is greater
320 // than the total number of row-0 tags and if we're in an icon-based screen
321 static VOID IdentifyRows(IN SCROLL_STATE *State, IN REFIT_MENU_SCREEN *Screen) {
322 UINTN i;
323
324 State->FinalRow0 = 0;
325 State->InitialRow1 = State->MaxIndex;
326 for (i = 0; i <= State->MaxIndex; i++) {
327 if (Screen->Entries[i]->Row == 0) {
328 State->FinalRow0 = i;
329 } else if ((Screen->Entries[i]->Row == 1) && (State->InitialRow1 > i)) {
330 State->InitialRow1 = i;
331 } // if/else
332 } // for
333 if ((State->ScrollMode == SCROLL_MODE_ICONS) && (State->MaxVisible > (State->FinalRow0 + 1)))
334 State->MaxVisible = State->FinalRow0 + 1;
335 } // static VOID IdentifyRows()
336
337 // Blank the screen, wait for a keypress, and restore banner/background.
338 // Screen may still require redrawing of text and icons on return.
339 // TODO: Support more sophisticated screen savers, such as power-saving
340 // mode and dynamic images.
341 static VOID SaveScreen(VOID) {
342 UINTN index;
343 EG_PIXEL Black = { 0x0, 0x0, 0x0, 0 };
344
345 egClearScreen(&Black);
346 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
347 if (AllowGraphicsMode)
348 SwitchToGraphicsAndClear();
349 ReadAllKeyStrokes();
350 } // VOID SaveScreen()
351
352 //
353 // generic menu function
354 //
355 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN OUT INTN *DefaultEntryIndex,
356 OUT REFIT_MENU_ENTRY **ChosenEntry)
357 {
358 SCROLL_STATE State;
359 EFI_STATUS Status;
360 EFI_INPUT_KEY key;
361 UINTN index;
362 INTN ShortcutEntry;
363 BOOLEAN HaveTimeout = FALSE;
364 BOOLEAN WaitForRelease = FALSE;
365 UINTN TimeoutCountdown = 0;
366 INTN PreviousTime = -1, CurrentTime, TimeSinceKeystroke = 0;
367 CHAR16 TimeoutMessage[256];
368 CHAR16 KeyAsString[2];
369 UINTN MenuExit;
370 // EG_PIXEL Black = { 0x0, 0x0, 0x0, 0 };
371
372 if (Screen->TimeoutSeconds > 0) {
373 HaveTimeout = TRUE;
374 TimeoutCountdown = Screen->TimeoutSeconds * 10;
375 }
376 MenuExit = 0;
377
378 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
379 IdentifyRows(&State, Screen);
380 // override the starting selection with the default index, if any
381 if (*DefaultEntryIndex >= 0 && *DefaultEntryIndex <= State.MaxIndex) {
382 State.CurrentSelection = *DefaultEntryIndex;
383 if (GlobalConfig.ScreensaverTime != -1)
384 UpdateScroll(&State, SCROLL_NONE);
385 }
386
387 if (Screen->TimeoutSeconds == -1) {
388 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
389 if (Status == EFI_NOT_READY) {
390 MenuExit = MENU_EXIT_TIMEOUT;
391 } else {
392 KeyAsString[0] = key.UnicodeChar;
393 KeyAsString[1] = 0;
394 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
395 if (ShortcutEntry >= 0) {
396 State.CurrentSelection = ShortcutEntry;
397 MenuExit = MENU_EXIT_ENTER;
398 } else {
399 WaitForRelease = TRUE;
400 HaveTimeout = FALSE;
401 }
402 }
403 }
404
405 if (GlobalConfig.ScreensaverTime != -1)
406 State.PaintAll = TRUE;
407
408 while (!MenuExit) {
409 // update the screen
410 if (State.PaintAll && (GlobalConfig.ScreensaverTime != -1)) {
411 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
412 State.PaintAll = FALSE;
413 } else if (State.PaintSelection) {
414 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
415 State.PaintSelection = FALSE;
416 }
417
418 if (WaitForRelease) {
419 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
420 if (Status == EFI_SUCCESS) {
421 // reset, because otherwise the buffer gets queued with keystrokes
422 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, FALSE);
423 refit_call1_wrapper(BS->Stall, 100000);
424 } else {
425 WaitForRelease = FALSE;
426 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, TRUE);
427 }
428 continue;
429 }
430
431 if (HaveTimeout) {
432 CurrentTime = (TimeoutCountdown + 5) / 10;
433 if (CurrentTime != PreviousTime) {
434 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, CurrentTime);
435 if (GlobalConfig.ScreensaverTime != -1)
436 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
437 PreviousTime = CurrentTime;
438 }
439 }
440
441 // read key press (and wait for it if applicable)
442 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
443 if (Status != EFI_SUCCESS) {
444 if (HaveTimeout && TimeoutCountdown == 0) {
445 // timeout expired
446 MenuExit = MENU_EXIT_TIMEOUT;
447 break;
448 } else if (HaveTimeout) {
449 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
450 TimeoutCountdown--;
451 TimeSinceKeystroke++;
452 } else if (GlobalConfig.ScreensaverTime > 0) {
453 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
454 TimeSinceKeystroke++;
455 if (TimeSinceKeystroke > (GlobalConfig.ScreensaverTime * 10)) {
456 SaveScreen();
457 State.PaintAll = TRUE;
458 TimeSinceKeystroke = 0;
459 } // if
460 } else {
461 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
462 }
463 continue;
464 } else {
465 TimeSinceKeystroke = 0;
466 } // if/else !read keystroke
467
468 if (HaveTimeout) {
469 // the user pressed a key, cancel the timeout
470 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
471 HaveTimeout = FALSE;
472 if (GlobalConfig.ScreensaverTime == -1) { // cancel start-with-blank-screen coding
473 GlobalConfig.ScreensaverTime = 0;
474 if (!GlobalConfig.TextOnly)
475 BltClearScreen(TRUE);
476 }
477 }
478
479 // react to key press
480 switch (key.ScanCode) {
481 case SCAN_UP:
482 UpdateScroll(&State, SCROLL_LINE_UP);
483 break;
484 case SCAN_LEFT:
485 UpdateScroll(&State, SCROLL_LINE_LEFT);
486 break;
487 case SCAN_DOWN:
488 UpdateScroll(&State, SCROLL_LINE_DOWN);
489 break;
490 case SCAN_RIGHT:
491 UpdateScroll(&State, SCROLL_LINE_RIGHT);
492 break;
493 case SCAN_HOME:
494 UpdateScroll(&State, SCROLL_FIRST);
495 break;
496 case SCAN_END:
497 UpdateScroll(&State, SCROLL_LAST);
498 break;
499 case SCAN_PAGE_UP:
500 UpdateScroll(&State, SCROLL_PAGE_UP);
501 break;
502 case SCAN_PAGE_DOWN:
503 UpdateScroll(&State, SCROLL_PAGE_DOWN);
504 break;
505 case SCAN_ESC:
506 MenuExit = MENU_EXIT_ESCAPE;
507 break;
508 case SCAN_INSERT:
509 case SCAN_F2:
510 MenuExit = MENU_EXIT_DETAILS;
511 break;
512 case SCAN_F10:
513 egScreenShot();
514 break;
515 case 0x0016: // F12
516 if (EjectMedia())
517 MenuExit = MENU_EXIT_ESCAPE;
518 break;
519 }
520 switch (key.UnicodeChar) {
521 case CHAR_LINEFEED:
522 case CHAR_CARRIAGE_RETURN:
523 case ' ':
524 MenuExit = MENU_EXIT_ENTER;
525 break;
526 case '+':
527 MenuExit = MENU_EXIT_DETAILS;
528 break;
529 default:
530 KeyAsString[0] = key.UnicodeChar;
531 KeyAsString[1] = 0;
532 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
533 if (ShortcutEntry >= 0) {
534 State.CurrentSelection = ShortcutEntry;
535 MenuExit = MENU_EXIT_ENTER;
536 }
537 break;
538 }
539 }
540
541 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
542
543 if (ChosenEntry)
544 *ChosenEntry = Screen->Entries[State.CurrentSelection];
545 *DefaultEntryIndex = State.CurrentSelection;
546 return MenuExit;
547 } /* static UINTN RunGenericMenu() */
548
549 //
550 // text-mode generic style
551 //
552
553 // Show information lines in text mode.
554 static VOID ShowTextInfoLines(IN REFIT_MENU_SCREEN *Screen) {
555 INTN i;
556
557 BeginTextScreen(Screen->Title);
558 if (Screen->InfoLineCount > 0) {
559 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
560 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
561 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
562 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
563 }
564 }
565 } // VOID ShowTextInfoLines()
566
567 // Do most of the work for text-based menus....
568 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
569 {
570 INTN i;
571 UINTN MenuWidth, ItemWidth, MenuHeight;
572 static UINTN MenuPosY;
573 static CHAR16 **DisplayStrings;
574 CHAR16 TimeoutMessage[256];
575
576 State->ScrollMode = SCROLL_MODE_TEXT;
577 switch (Function) {
578
579 case MENU_FUNCTION_INIT:
580 // vertical layout
581 MenuPosY = 4;
582 if (Screen->InfoLineCount > 0)
583 MenuPosY += Screen->InfoLineCount + 1;
584 MenuHeight = ConHeight - MenuPosY - 3;
585 if (Screen->TimeoutSeconds > 0)
586 MenuHeight -= 2;
587 InitScroll(State, Screen->EntryCount, MenuHeight);
588
589 // determine width of the menu
590 MenuWidth = 20; // minimum
591 for (i = 0; i <= State->MaxIndex; i++) {
592 ItemWidth = StrLen(Screen->Entries[i]->Title);
593 if (MenuWidth < ItemWidth)
594 MenuWidth = ItemWidth;
595 }
596 MenuWidth += 2;
597 if (MenuWidth > ConWidth - 3)
598 MenuWidth = ConWidth - 3;
599
600 // prepare strings for display
601 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
602 for (i = 0; i <= State->MaxIndex; i++) {
603 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
604 // description of the StrSize parameter to SPrint implies it's measured
605 // in characters, but in practice both TianoCore and GNU-EFI seem to
606 // use bytes instead, resulting in truncated displays. I could just
607 // double the size of the StrSize parameter, but that seems unsafe in
608 // case a future library change starts treating this as characters, so
609 // I'm doing it the hard way in this instance.
610 // TODO: Review the above and possibly change other uses of SPrint()
611 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
612 DisplayStrings[i][0] = L' ';
613 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
614 if (StrLen(DisplayStrings[i]) > MenuWidth)
615 DisplayStrings[i][MenuWidth - 1] = 0;
616 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
617 // TODO: account for double-width characters
618 } // for
619
620 break;
621
622 case MENU_FUNCTION_CLEANUP:
623 // release temporary memory
624 for (i = 0; i <= State->MaxIndex; i++)
625 MyFreePool(DisplayStrings[i]);
626 MyFreePool(DisplayStrings);
627 break;
628
629 case MENU_FUNCTION_PAINT_ALL:
630 // paint the whole screen (initially and after scrolling)
631
632 ShowTextInfoLines(Screen);
633 for (i = 0; i <= State->MaxIndex; i++) {
634 if (i >= State->FirstVisible && i <= State->LastVisible) {
635 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
636 if (i == State->CurrentSelection)
637 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
638 else
639 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
640 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
641 }
642 }
643 // scrolling indicators
644 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
645 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
646 if (State->FirstVisible > 0)
647 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
648 else
649 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
650 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
651 if (State->LastVisible < State->MaxIndex)
652 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
653 else
654 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
655 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
656 if (Screen->Hint1 != NULL) {
657 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
658 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint1);
659 }
660 if (Screen->Hint2 != NULL) {
661 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
662 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint2);
663 }
664 }
665 break;
666
667 case MENU_FUNCTION_PAINT_SELECTION:
668 // redraw selection cursor
669 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
670 MenuPosY + (State->PreviousSelection - State->FirstVisible));
671 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
672 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
673 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
674 MenuPosY + (State->CurrentSelection - State->FirstVisible));
675 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
676 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
677 break;
678
679 case MENU_FUNCTION_PAINT_TIMEOUT:
680 if (ParamText[0] == 0) {
681 // clear message
682 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
683 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 3);
684 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
685 } else {
686 // paint or update message
687 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
688 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 3);
689 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
690 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
691 }
692 break;
693
694 }
695 }
696
697 //
698 // graphical generic style
699 //
700
701 inline static UINTN TextLineHeight(VOID) {
702 return egGetFontHeight() + TEXT_YMARGIN * 2;
703 } // UINTN TextLineHeight()
704
705 //
706 // Display a submenu
707 //
708
709 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
710 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
711 // specified XPos and YPos locations.
712 static VOID DrawText(IN CHAR16 *Text, IN BOOLEAN Selected, IN UINTN FieldWidth, IN UINTN XPos, IN UINTN YPos)
713 {
714 EG_IMAGE *TextBuffer;
715 EG_PIXEL Bg;
716
717 TextBuffer = egCreateImage(FieldWidth, TextLineHeight(), FALSE);
718
719 egFillImage(TextBuffer, &MenuBackgroundPixel);
720 Bg = MenuBackgroundPixel;
721 if (Selected) {
722 // draw selection bar background
723 egFillImageArea(TextBuffer, 0, 0, FieldWidth, TextBuffer->Height, &SelectionBackgroundPixel);
724 Bg = SelectionBackgroundPixel;
725 }
726
727 // render the text
728 egRenderText(Text, TextBuffer, egGetFontCellWidth(), TEXT_YMARGIN, (Bg.r + Bg.g + Bg.b) / 3);
729 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
730 // BltImage(TextBuffer, XPos, YPos);
731 }
732
733 // Finds the average brightness of the input Image.
734 // NOTE: Passing an Image that covers the whole screen can strain the
735 // capacity of a UINTN on a 32-bit system with a very large display.
736 // Using UINT64 instead is unworkable, since the code won't compile
737 // on a 32-bit system. As the intended use for this function is to handle
738 // a single text string's background, this shouldn't be a problem, but it
739 // may need addressing if it's applied more broadly....
740 static UINT8 AverageBrightness(EG_IMAGE *Image) {
741 UINTN i;
742 UINTN Sum = 0;
743
744 if (Image != NULL) {
745 for (i = 0; i < (Image->Width * Image->Height); i++) {
746 Sum += (Image->PixelData[i].r + Image->PixelData[i].g + Image->PixelData[i].b);
747 }
748 } // if
749 return (UINT8) (Sum / (Image->Width * Image->Height * 3));
750 } // UINT8 AverageBrightness()
751
752 // Display text against the screen's background image. Special case: If Text is NULL
753 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
754 // to the specified XPos and YPos values.
755 static VOID DrawTextWithTransparency(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
756 {
757 UINTN TextWidth;
758 EG_IMAGE *TextBuffer = NULL;
759
760 if (Text == NULL)
761 Text = L"";
762
763 egMeasureText(Text, &TextWidth, NULL);
764 if (TextWidth == 0) {
765 TextWidth = UGAWidth;
766 XPos = 0;
767 }
768
769 TextBuffer = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, TextWidth, TextLineHeight());
770 if (TextBuffer == NULL)
771 return;
772
773 // render the text
774 egRenderText(Text, TextBuffer, 0, 0, AverageBrightness(TextBuffer));
775 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
776 egFreeImage(TextBuffer);
777 }
778
779 // Compute the size & position of the window that will hold a subscreen's information.
780 static VOID ComputeSubScreenWindowSize(REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *XPos, UINTN *YPos,
781 UINTN *Width, UINTN *Height, UINTN *LineWidth) {
782 UINTN i, ItemWidth, HintTop, BannerBottomEdge, TitleWidth;
783 UINTN FontCellWidth = egGetFontCellWidth();
784 UINTN FontCellHeight = egGetFontHeight();
785
786 *Width = 20;
787 *Height = 5;
788 TitleWidth = egComputeTextWidth(Screen->Title);
789
790 for (i = 0; i < Screen->InfoLineCount; i++) {
791 ItemWidth = StrLen(Screen->InfoLines[i]);
792 if (*Width < ItemWidth) {
793 *Width = ItemWidth;
794 }
795 (*Height)++;
796 }
797 for (i = 0; i <= State->MaxIndex; i++) {
798 ItemWidth = StrLen(Screen->Entries[i]->Title);
799 if (*Width < ItemWidth) {
800 *Width = ItemWidth;
801 }
802 (*Height)++;
803 }
804 *Width = (*Width + 2) * FontCellWidth;
805 *LineWidth = *Width;
806 if (Screen->TitleImage)
807 *Width += (Screen->TitleImage->Width + TITLEICON_SPACING * 2 + FontCellWidth);
808 else
809 *Width += FontCellWidth;
810
811 if (*Width < TitleWidth)
812 *Width = TitleWidth + 2 * FontCellWidth;
813
814 // Keep it within the bounds of the screen, or 2/3 of the screen's width
815 // for screens over 800 pixels wide
816 if (*Width > UGAWidth)
817 *Width = UGAWidth;
818
819 *XPos = (UGAWidth - *Width) / 2;
820
821 HintTop = UGAHeight - (FontCellHeight * 3); // top of hint text
822 *Height *= TextLineHeight();
823 if (Screen->TitleImage && (*Height < (Screen->TitleImage->Height + TextLineHeight() * 4)))
824 *Height = Screen->TitleImage->Height + TextLineHeight() * 4;
825
826 if (GlobalConfig.BannerBottomEdge >= HintTop) { // probably a full-screen image; treat it as an empty banner
827 BannerBottomEdge = 0;
828 } else {
829 BannerBottomEdge = GlobalConfig.BannerBottomEdge;
830 }
831 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
832 BannerBottomEdge = 0;
833 }
834 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
835 // TODO: Implement scrolling in text screen.
836 *Height = (HintTop - BannerBottomEdge - FontCellHeight * 2);
837 }
838
839 *YPos = ((UGAHeight - *Height) / 2);
840 if (*YPos < BannerBottomEdge)
841 *YPos = BannerBottomEdge + FontCellHeight + (HintTop - BannerBottomEdge - *Height) / 2;
842 } // VOID ComputeSubScreenWindowSize()
843
844 // Displays sub-menus
845 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
846 {
847 INTN i;
848 UINTN ItemWidth;
849 static UINTN LineWidth, MenuWidth, MenuHeight, EntriesPosX, TitlePosX, EntriesPosY, TimeoutPosY, CharWidth;
850 EG_IMAGE *Window;
851 EG_PIXEL *BackgroundPixel = &(GlobalConfig.ScreenBackground->PixelData[0]);
852
853 CharWidth = egGetFontCellWidth();
854 State->ScrollMode = SCROLL_MODE_TEXT;
855 switch (Function) {
856
857 case MENU_FUNCTION_INIT:
858 InitScroll(State, Screen->EntryCount, 0);
859 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
860 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TextLineHeight();
861
862 // initial painting
863 SwitchToGraphicsAndClear();
864 Window = egCreateFilledImage(MenuWidth, MenuHeight, FALSE, BackgroundPixel);
865 egDrawImage(Window, EntriesPosX, EntriesPosY);
866 ItemWidth = egComputeTextWidth(Screen->Title);
867 if (MenuWidth > ItemWidth) {
868 TitlePosX = EntriesPosX + (MenuWidth - ItemWidth) / 2 - CharWidth;
869 } else {
870 TitlePosX = EntriesPosX;
871 if (CharWidth > 0) {
872 i = MenuWidth / CharWidth - 2;
873 if (i > 0)
874 Screen->Title[i] = 0;
875 } // if
876 } // if/else
877 break;
878
879 case MENU_FUNCTION_CLEANUP:
880 // nothing to do
881 break;
882
883 case MENU_FUNCTION_PAINT_ALL:
884 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
885 DrawText(Screen->Title, FALSE, (StrLen(Screen->Title) + 2) * CharWidth, TitlePosX, EntriesPosY += TextLineHeight());
886 if (Screen->TitleImage) {
887 BltImageAlpha(Screen->TitleImage, EntriesPosX + TITLEICON_SPACING, EntriesPosY + TextLineHeight() * 2,
888 BackgroundPixel);
889 EntriesPosX += (Screen->TitleImage->Width + TITLEICON_SPACING * 2);
890 }
891 EntriesPosY += (TextLineHeight() * 2);
892 if (Screen->InfoLineCount > 0) {
893 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
894 DrawText(Screen->InfoLines[i], FALSE, LineWidth, EntriesPosX, EntriesPosY);
895 EntriesPosY += TextLineHeight();
896 }
897 EntriesPosY += TextLineHeight(); // also add a blank line
898 }
899
900 for (i = 0; i <= State->MaxIndex; i++) {
901 DrawText(Screen->Entries[i]->Title, (i == State->CurrentSelection), LineWidth, EntriesPosX,
902 EntriesPosY + i * TextLineHeight());
903 }
904 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
905 if ((Screen->Hint1 != NULL) && (StrLen(Screen->Hint1) > 0))
906 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
907 UGAHeight - (egGetFontHeight() * 3));
908 if ((Screen->Hint2 != NULL) && (StrLen(Screen->Hint2) > 0))
909 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
910 UGAHeight - (egGetFontHeight() * 2));
911 } // if
912 break;
913
914 case MENU_FUNCTION_PAINT_SELECTION:
915 // redraw selection cursor
916 DrawText(Screen->Entries[State->PreviousSelection]->Title, FALSE, LineWidth,
917 EntriesPosX, EntriesPosY + State->PreviousSelection * TextLineHeight());
918 DrawText(Screen->Entries[State->CurrentSelection]->Title, TRUE, LineWidth,
919 EntriesPosX, EntriesPosY + State->CurrentSelection * TextLineHeight());
920 break;
921
922 case MENU_FUNCTION_PAINT_TIMEOUT:
923 DrawText(ParamText, FALSE, LineWidth, EntriesPosX, TimeoutPosY);
924 break;
925
926 }
927 } // static VOID GraphicsMenuStyle()
928
929 //
930 // graphical main menu style
931 //
932
933 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
934 {
935 EG_IMAGE *Background;
936
937 if (SelectionImages != NULL) {
938 if (selected) {
939 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos,
940 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
941 egComposeImage(Background, SelectionImages[Entry->Row], 0, 0);
942 BltImageCompositeBadge(Background, Entry->Image, Entry->BadgeImage, XPos, YPos);
943 } else { // Image not selected; copy background
944 egDrawImageWithTransparency(Entry->Image, Entry->BadgeImage, XPos, YPos,
945 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
946 } // if/else
947 } // if
948 } // VOID DrawMainMenuEntry()
949
950 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
951 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
952 INTN i;
953
954 if (Screen->Entries[State->CurrentSelection]->Row == 0)
955 AdjustScrollState(State);
956 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
957 if (Screen->Entries[i]->Row == 0) {
958 if (i <= State->LastVisible) {
959 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
960 itemPosX[i - State->FirstVisible], row0PosY);
961 } // if
962 } else {
963 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
964 }
965 }
966 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
967 DrawTextWithTransparency(L"", 0, textPosY);
968 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
969 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
970 textPosY);
971 }
972
973 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
974 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
975 UGAHeight - (egGetFontHeight() * 3));
976 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
977 UGAHeight - (egGetFontHeight() * 2));
978 } // if
979 } // static VOID PaintAll()
980
981 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
982 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
983 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
984 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
985
986 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
987 (State->CurrentSelection >= State->InitialRow1) ) {
988 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
989 XSelectPrev = State->PreviousSelection - State->FirstVisible;
990 YPosPrev = row0PosY;
991 } else {
992 XSelectPrev = State->PreviousSelection;
993 YPosPrev = row1PosY;
994 } // if/else
995 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
996 XSelectCur = State->CurrentSelection - State->FirstVisible;
997 YPosCur = row0PosY;
998 } else {
999 XSelectCur = State->CurrentSelection;
1000 YPosCur = row1PosY;
1001 } // if/else
1002 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
1003 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
1004 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1005 DrawTextWithTransparency(L"", 0, textPosY);
1006 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
1007 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
1008 textPosY);
1009 }
1010 } else { // Current selection not visible; must redraw the menu....
1011 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
1012 }
1013 } // static VOID MoveSelection(VOID)
1014
1015 // Display a 48x48 icon at the specified location. Uses the image specified by
1016 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1017 // Y position is specified as the center value, and so is adjusted by half
1018 // the icon's height. The X position is set along the icon's left
1019 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1020 // Alignment == ALIGN_RIGHT
1021 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
1022 EG_IMAGE *Icon = NULL;
1023
1024 Icon = egFindIcon(ExternalFilename, GlobalConfig.IconSizes[ICON_SIZE_SMALL]);
1025 if (Icon == NULL)
1026 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
1027 if (Icon != NULL) {
1028 if (Alignment == ALIGN_RIGHT)
1029 PosX -= Icon->Width;
1030 egDrawImageWithTransparency(Icon, NULL, PosX, PosY - (Icon->Height / 2), Icon->Width, Icon->Height);
1031 }
1032 } // static VOID ()
1033
1034 inline UINTN ComputeRow0PosY(VOID) {
1035 return ((UGAHeight / 2) - TileSizes[0] / 2);
1036 } // UINTN ComputeRow0PosY()
1037
1038 // Display (or erase) the arrow icons to the left and right of an icon's row,
1039 // as appropriate.
1040 static VOID PaintArrows(SCROLL_STATE *State, UINTN PosX, UINTN PosY, UINTN row0Loaders) {
1041 EG_IMAGE *TempImage;
1042 UINTN Width, Height, RightX, AdjPosY;
1043
1044 // NOTE: Assume that left and right arrows are of the same size....
1045 Width = egemb_arrow_left.Width;
1046 Height = egemb_arrow_left.Height;
1047 RightX = (UGAWidth + (TileSizes[0] + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING;
1048 AdjPosY = PosY - (Height / 2);
1049
1050 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1051 // of the surrounding row; PaintIcon() adjusts this back up by half the
1052 // icon's height to properly center it.
1053 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1054 PaintIcon(&egemb_arrow_left, L"arrow_left", PosX, PosY, ALIGN_RIGHT);
1055 } else {
1056 TempImage = egCropImage(GlobalConfig.ScreenBackground, PosX - Width, AdjPosY, Width, Height);
1057 BltImage(TempImage, PosX - Width, AdjPosY);
1058 egFreeImage(TempImage);
1059 } // if/else
1060
1061 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1062 PaintIcon(&egemb_arrow_right, L"arrow_right", RightX, PosY, ALIGN_LEFT);
1063 } else {
1064 TempImage = egCropImage(GlobalConfig.ScreenBackground, RightX, AdjPosY, Width, Height);
1065 BltImage(TempImage, RightX, AdjPosY);
1066 egFreeImage(TempImage);
1067 } // if/else
1068 } // VOID PaintArrows()
1069
1070 // Display main menu in graphics mode
1071 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
1072 {
1073 INTN i;
1074 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
1075 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
1076 static UINTN *itemPosX;
1077 static UINTN row0PosY, textPosY;
1078
1079 State->ScrollMode = SCROLL_MODE_ICONS;
1080 switch (Function) {
1081
1082 case MENU_FUNCTION_INIT:
1083 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
1084
1085 // layout
1086 row0Count = 0;
1087 row1Count = 0;
1088 row0Loaders = 0;
1089 for (i = 0; i <= State->MaxIndex; i++) {
1090 if (Screen->Entries[i]->Row == 1) {
1091 row1Count++;
1092 } else {
1093 row0Loaders++;
1094 if (row0Count < State->MaxVisible)
1095 row0Count++;
1096 }
1097 }
1098 row0PosX = (UGAWidth + TILE_XSPACING - (TileSizes[0] + TILE_XSPACING) * row0Count) >> 1;
1099 row0PosY = ComputeRow0PosY();
1100 row1PosX = (UGAWidth + TILE_XSPACING - (TileSizes[1] + TILE_XSPACING) * row1Count) >> 1;
1101 row1PosY = row0PosY + TileSizes[0] + TILE_YSPACING;
1102 if (row1Count > 0)
1103 textPosY = row1PosY + TileSizes[1] + TILE_YSPACING;
1104 else
1105 textPosY = row1PosY;
1106
1107 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1108 row0PosXRunning = row0PosX;
1109 row1PosXRunning = row1PosX;
1110 for (i = 0; i <= State->MaxIndex; i++) {
1111 if (Screen->Entries[i]->Row == 0) {
1112 itemPosX[i] = row0PosXRunning;
1113 row0PosXRunning += TileSizes[0] + TILE_XSPACING;
1114 } else {
1115 itemPosX[i] = row1PosXRunning;
1116 row1PosXRunning += TileSizes[1] + TILE_XSPACING;
1117 }
1118 }
1119 // initial painting
1120 InitSelection();
1121 SwitchToGraphicsAndClear();
1122 break;
1123
1124 case MENU_FUNCTION_CLEANUP:
1125 MyFreePool(itemPosX);
1126 break;
1127
1128 case MENU_FUNCTION_PAINT_ALL:
1129 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1130 // For PaintArrows(), the starting Y position is moved to the midpoint
1131 // of the surrounding row; PaintIcon() adjusts this back up by half the
1132 // icon's height to properly center it.
1133 PaintArrows(State, row0PosX - TILE_XSPACING, row0PosY + (TileSizes[0] / 2), row0Loaders);
1134 break;
1135
1136 case MENU_FUNCTION_PAINT_SELECTION:
1137 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1138 break;
1139
1140 case MENU_FUNCTION_PAINT_TIMEOUT:
1141 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1142 DrawTextWithTransparency(L"", 0, textPosY + TextLineHeight());
1143 DrawTextWithTransparency(ParamText, (UGAWidth - egComputeTextWidth(ParamText)) >> 1, textPosY + TextLineHeight());
1144 }
1145 break;
1146
1147 }
1148 } // VOID MainMenuStyle()
1149
1150 // Enable the user to edit boot loader options.
1151 // Returns TRUE if the user exited with edited options; FALSE if the user
1152 // pressed Esc to terminate the edit.
1153 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
1154 UINTN x_max, y_max;
1155 CHAR16 *EditedOptions;
1156 BOOLEAN retval = FALSE;
1157
1158 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
1159 return FALSE;
1160 }
1161
1162 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
1163
1164 if (!GlobalConfig.TextOnly)
1165 SwitchToText(TRUE);
1166
1167 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
1168 MyFreePool(MenuEntry->LoadOptions);
1169 MenuEntry->LoadOptions = EditedOptions;
1170 retval = TRUE;
1171 } // if
1172 if (!GlobalConfig.TextOnly)
1173 SwitchToGraphics();
1174 return retval;
1175 } // VOID EditOptions()
1176
1177 //
1178 // user-callable dispatcher functions
1179 //
1180
1181 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
1182 {
1183 INTN DefaultEntry = -1;
1184 MENU_STYLE_FUNC Style = TextMenuStyle;
1185
1186 if (AllowGraphicsMode)
1187 Style = GraphicsMenuStyle;
1188
1189 return RunGenericMenu(Screen, Style, &DefaultEntry, ChosenEntry);
1190 }
1191
1192 UINTN RunMainMenu(REFIT_MENU_SCREEN *Screen, CHAR16** DefaultSelection, REFIT_MENU_ENTRY **ChosenEntry)
1193 {
1194 MENU_STYLE_FUNC Style = TextMenuStyle;
1195 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
1196 REFIT_MENU_ENTRY *TempChosenEntry;
1197 CHAR16 *MenuTitle;
1198 UINTN MenuExit = 0;
1199 INTN DefaultEntryIndex = -1;
1200 INTN DefaultSubmenuIndex = -1;
1201
1202 TileSizes[0] = (GlobalConfig.IconSizes[ICON_SIZE_BIG] * 9) / 8;
1203 TileSizes[1] = (GlobalConfig.IconSizes[ICON_SIZE_SMALL] * 4) / 3;
1204
1205 if ((DefaultSelection != NULL) && (*DefaultSelection != NULL)) {
1206 // Find a menu entry that includes *DefaultSelection as a substring
1207 DefaultEntryIndex = FindMenuShortcutEntry(Screen, *DefaultSelection);
1208 }
1209
1210 if (AllowGraphicsMode) {
1211 Style = GraphicsMenuStyle;
1212 MainStyle = MainMenuStyle;
1213 }
1214
1215 while (!MenuExit) {
1216 MenuExit = RunGenericMenu(Screen, MainStyle, &DefaultEntryIndex, &TempChosenEntry);
1217 Screen->TimeoutSeconds = 0;
1218
1219 MenuTitle = StrDuplicate(TempChosenEntry->Title);
1220 if (MenuExit == MENU_EXIT_DETAILS) {
1221 if (TempChosenEntry->SubScreen != NULL) {
1222 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, &DefaultSubmenuIndex, &TempChosenEntry);
1223 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1224 MenuExit = 0;
1225 if (MenuExit == MENU_EXIT_DETAILS) {
1226 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1227 MenuExit = 0;
1228 } // if
1229 } else { // no sub-screen; ignore keypress
1230 MenuExit = 0;
1231 }
1232 } // Enter sub-screen
1233 }
1234
1235 if (ChosenEntry)
1236 *ChosenEntry = TempChosenEntry;
1237 if (DefaultSelection) {
1238 MyFreePool(*DefaultSelection);
1239 *DefaultSelection = MenuTitle;
1240 } // if
1241 return MenuExit;
1242 } /* UINTN RunMainMenu() */