]> code.delx.au - refind/blob - refind/menu.c
Version 0.8.3 release
[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
371 if (Screen->TimeoutSeconds > 0) {
372 HaveTimeout = TRUE;
373 TimeoutCountdown = Screen->TimeoutSeconds * 10;
374 }
375 MenuExit = 0;
376
377 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
378 IdentifyRows(&State, Screen);
379 // override the starting selection with the default index, if any
380 if (*DefaultEntryIndex >= 0 && *DefaultEntryIndex <= State.MaxIndex) {
381 State.CurrentSelection = *DefaultEntryIndex;
382 if (GlobalConfig.ScreensaverTime != -1)
383 UpdateScroll(&State, SCROLL_NONE);
384 }
385
386 if (Screen->TimeoutSeconds == -1) {
387 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
388 if (Status == EFI_NOT_READY) {
389 MenuExit = MENU_EXIT_TIMEOUT;
390 } else {
391 KeyAsString[0] = key.UnicodeChar;
392 KeyAsString[1] = 0;
393 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
394 if (ShortcutEntry >= 0) {
395 State.CurrentSelection = ShortcutEntry;
396 MenuExit = MENU_EXIT_ENTER;
397 } else {
398 WaitForRelease = TRUE;
399 HaveTimeout = FALSE;
400 }
401 }
402 }
403
404 if (GlobalConfig.ScreensaverTime != -1)
405 State.PaintAll = TRUE;
406
407 while (!MenuExit) {
408 // update the screen
409 if (State.PaintAll && (GlobalConfig.ScreensaverTime != -1)) {
410 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
411 State.PaintAll = FALSE;
412 } else if (State.PaintSelection) {
413 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
414 State.PaintSelection = FALSE;
415 }
416
417 if (WaitForRelease) {
418 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
419 if (Status == EFI_SUCCESS) {
420 // reset, because otherwise the buffer gets queued with keystrokes
421 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, FALSE);
422 refit_call1_wrapper(BS->Stall, 100000);
423 } else {
424 WaitForRelease = FALSE;
425 refit_call2_wrapper(ST->ConIn->Reset, ST->ConIn, TRUE);
426 }
427 continue;
428 }
429
430 if (HaveTimeout) {
431 CurrentTime = (TimeoutCountdown + 5) / 10;
432 if (CurrentTime != PreviousTime) {
433 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, CurrentTime);
434 if (GlobalConfig.ScreensaverTime != -1)
435 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
436 PreviousTime = CurrentTime;
437 }
438 }
439
440 // read key press (and wait for it if applicable)
441 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
442 if (Status != EFI_SUCCESS) {
443 if (HaveTimeout && TimeoutCountdown == 0) {
444 // timeout expired
445 MenuExit = MENU_EXIT_TIMEOUT;
446 break;
447 } else if (HaveTimeout) {
448 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
449 TimeoutCountdown--;
450 TimeSinceKeystroke++;
451 } else if (GlobalConfig.ScreensaverTime > 0) {
452 refit_call1_wrapper(BS->Stall, 100000); // Pause for 100 ms
453 TimeSinceKeystroke++;
454 if (TimeSinceKeystroke > (GlobalConfig.ScreensaverTime * 10)) {
455 SaveScreen();
456 State.PaintAll = TRUE;
457 TimeSinceKeystroke = 0;
458 } // if
459 } else {
460 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
461 }
462 continue;
463 } else {
464 TimeSinceKeystroke = 0;
465 } // if/else !read keystroke
466
467 if (HaveTimeout) {
468 // the user pressed a key, cancel the timeout
469 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
470 HaveTimeout = FALSE;
471 if (GlobalConfig.ScreensaverTime == -1) { // cancel start-with-blank-screen coding
472 GlobalConfig.ScreensaverTime = 0;
473 if (!GlobalConfig.TextOnly)
474 BltClearScreen(TRUE);
475 }
476 }
477
478 // react to key press
479 switch (key.ScanCode) {
480 case SCAN_UP:
481 UpdateScroll(&State, SCROLL_LINE_UP);
482 break;
483 case SCAN_LEFT:
484 UpdateScroll(&State, SCROLL_LINE_LEFT);
485 break;
486 case SCAN_DOWN:
487 UpdateScroll(&State, SCROLL_LINE_DOWN);
488 break;
489 case SCAN_RIGHT:
490 UpdateScroll(&State, SCROLL_LINE_RIGHT);
491 break;
492 case SCAN_HOME:
493 UpdateScroll(&State, SCROLL_FIRST);
494 break;
495 case SCAN_END:
496 UpdateScroll(&State, SCROLL_LAST);
497 break;
498 case SCAN_PAGE_UP:
499 UpdateScroll(&State, SCROLL_PAGE_UP);
500 break;
501 case SCAN_PAGE_DOWN:
502 UpdateScroll(&State, SCROLL_PAGE_DOWN);
503 break;
504 case SCAN_ESC:
505 MenuExit = MENU_EXIT_ESCAPE;
506 break;
507 case SCAN_INSERT:
508 case SCAN_F2:
509 MenuExit = MENU_EXIT_DETAILS;
510 break;
511 case SCAN_F10:
512 egScreenShot();
513 break;
514 case 0x0016: // F12
515 if (EjectMedia())
516 MenuExit = MENU_EXIT_ESCAPE;
517 break;
518 }
519 switch (key.UnicodeChar) {
520 case CHAR_LINEFEED:
521 case CHAR_CARRIAGE_RETURN:
522 case ' ':
523 MenuExit = MENU_EXIT_ENTER;
524 break;
525 case '+':
526 MenuExit = MENU_EXIT_DETAILS;
527 break;
528 default:
529 KeyAsString[0] = key.UnicodeChar;
530 KeyAsString[1] = 0;
531 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
532 if (ShortcutEntry >= 0) {
533 State.CurrentSelection = ShortcutEntry;
534 MenuExit = MENU_EXIT_ENTER;
535 }
536 break;
537 }
538 }
539
540 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
541
542 if (ChosenEntry)
543 *ChosenEntry = Screen->Entries[State.CurrentSelection];
544 *DefaultEntryIndex = State.CurrentSelection;
545 return MenuExit;
546 } /* static UINTN RunGenericMenu() */
547
548 //
549 // text-mode generic style
550 //
551
552 // Show information lines in text mode.
553 static VOID ShowTextInfoLines(IN REFIT_MENU_SCREEN *Screen) {
554 INTN i;
555
556 BeginTextScreen(Screen->Title);
557 if (Screen->InfoLineCount > 0) {
558 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
559 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
560 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
561 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
562 }
563 }
564 } // VOID ShowTextInfoLines()
565
566 // Do most of the work for text-based menus....
567 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
568 {
569 INTN i;
570 UINTN MenuWidth, ItemWidth, MenuHeight;
571 static UINTN MenuPosY;
572 static CHAR16 **DisplayStrings;
573 CHAR16 TimeoutMessage[256];
574
575 State->ScrollMode = SCROLL_MODE_TEXT;
576 switch (Function) {
577
578 case MENU_FUNCTION_INIT:
579 // vertical layout
580 MenuPosY = 4;
581 if (Screen->InfoLineCount > 0)
582 MenuPosY += Screen->InfoLineCount + 1;
583 MenuHeight = ConHeight - MenuPosY - 3;
584 if (Screen->TimeoutSeconds > 0)
585 MenuHeight -= 2;
586 InitScroll(State, Screen->EntryCount, MenuHeight);
587
588 // determine width of the menu
589 MenuWidth = 20; // minimum
590 for (i = 0; i <= State->MaxIndex; i++) {
591 ItemWidth = StrLen(Screen->Entries[i]->Title);
592 if (MenuWidth < ItemWidth)
593 MenuWidth = ItemWidth;
594 }
595 MenuWidth += 2;
596 if (MenuWidth > ConWidth - 3)
597 MenuWidth = ConWidth - 3;
598
599 // prepare strings for display
600 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
601 for (i = 0; i <= State->MaxIndex; i++) {
602 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
603 // description of the StrSize parameter to SPrint implies it's measured
604 // in characters, but in practice both TianoCore and GNU-EFI seem to
605 // use bytes instead, resulting in truncated displays. I could just
606 // double the size of the StrSize parameter, but that seems unsafe in
607 // case a future library change starts treating this as characters, so
608 // I'm doing it the hard way in this instance.
609 // TODO: Review the above and possibly change other uses of SPrint()
610 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
611 DisplayStrings[i][0] = L' ';
612 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
613 if (StrLen(DisplayStrings[i]) > MenuWidth)
614 DisplayStrings[i][MenuWidth - 1] = 0;
615 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
616 // TODO: account for double-width characters
617 } // for
618
619 break;
620
621 case MENU_FUNCTION_CLEANUP:
622 // release temporary memory
623 for (i = 0; i <= State->MaxIndex; i++)
624 MyFreePool(DisplayStrings[i]);
625 MyFreePool(DisplayStrings);
626 break;
627
628 case MENU_FUNCTION_PAINT_ALL:
629 // paint the whole screen (initially and after scrolling)
630
631 ShowTextInfoLines(Screen);
632 for (i = 0; i <= State->MaxIndex; i++) {
633 if (i >= State->FirstVisible && i <= State->LastVisible) {
634 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
635 if (i == State->CurrentSelection)
636 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
637 else
638 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
639 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
640 }
641 }
642 // scrolling indicators
643 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
644 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
645 if (State->FirstVisible > 0)
646 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
647 else
648 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
649 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
650 if (State->LastVisible < State->MaxIndex)
651 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
652 else
653 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
654 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
655 if (Screen->Hint1 != NULL) {
656 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
657 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint1);
658 }
659 if (Screen->Hint2 != NULL) {
660 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
661 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint2);
662 }
663 }
664 break;
665
666 case MENU_FUNCTION_PAINT_SELECTION:
667 // redraw selection cursor
668 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
669 MenuPosY + (State->PreviousSelection - State->FirstVisible));
670 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
671 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
672 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
673 MenuPosY + (State->CurrentSelection - State->FirstVisible));
674 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
675 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
676 break;
677
678 case MENU_FUNCTION_PAINT_TIMEOUT:
679 if (ParamText[0] == 0) {
680 // clear message
681 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
682 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 3);
683 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
684 } else {
685 // paint or update message
686 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
687 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 3);
688 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
689 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
690 }
691 break;
692
693 }
694 }
695
696 //
697 // graphical generic style
698 //
699
700 inline static UINTN TextLineHeight(VOID) {
701 return egGetFontHeight() + TEXT_YMARGIN * 2;
702 } // UINTN TextLineHeight()
703
704 //
705 // Display a submenu
706 //
707
708 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel).
709 // Indents text by one character and placed TEXT_YMARGIN pixels down from the
710 // specified XPos and YPos locations.
711 static VOID DrawText(IN CHAR16 *Text, IN BOOLEAN Selected, IN UINTN FieldWidth, IN UINTN XPos, IN UINTN YPos)
712 {
713 EG_IMAGE *TextBuffer;
714 EG_PIXEL Bg;
715
716 TextBuffer = egCreateImage(FieldWidth, TextLineHeight(), FALSE);
717
718 egFillImage(TextBuffer, &MenuBackgroundPixel);
719 Bg = MenuBackgroundPixel;
720 if (Selected) {
721 // draw selection bar background
722 egFillImageArea(TextBuffer, 0, 0, FieldWidth, TextBuffer->Height, &SelectionBackgroundPixel);
723 Bg = SelectionBackgroundPixel;
724 }
725
726 // render the text
727 egRenderText(Text, TextBuffer, egGetFontCellWidth(), TEXT_YMARGIN, (Bg.r + Bg.g + Bg.b) / 3);
728 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
729 // BltImage(TextBuffer, XPos, YPos);
730 }
731
732 // Finds the average brightness of the input Image.
733 // NOTE: Passing an Image that covers the whole screen can strain the
734 // capacity of a UINTN on a 32-bit system with a very large display.
735 // Using UINT64 instead is unworkable, since the code won't compile
736 // on a 32-bit system. As the intended use for this function is to handle
737 // a single text string's background, this shouldn't be a problem, but it
738 // may need addressing if it's applied more broadly....
739 static UINT8 AverageBrightness(EG_IMAGE *Image) {
740 UINTN i;
741 UINTN Sum = 0;
742
743 if (Image != NULL) {
744 for (i = 0; i < (Image->Width * Image->Height); i++) {
745 Sum += (Image->PixelData[i].r + Image->PixelData[i].g + Image->PixelData[i].b);
746 }
747 } // if
748 return (UINT8) (Sum / (Image->Width * Image->Height * 3));
749 } // UINT8 AverageBrightness()
750
751 // Display text against the screen's background image. Special case: If Text is NULL
752 // or 0-length, clear the line. Does NOT indent the text or reposition it relative
753 // to the specified XPos and YPos values.
754 static VOID DrawTextWithTransparency(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
755 {
756 UINTN TextWidth;
757 EG_IMAGE *TextBuffer = NULL;
758
759 if (Text == NULL)
760 Text = L"";
761
762 egMeasureText(Text, &TextWidth, NULL);
763 if (TextWidth == 0) {
764 TextWidth = UGAWidth;
765 XPos = 0;
766 }
767
768 TextBuffer = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, TextWidth, TextLineHeight());
769 if (TextBuffer == NULL)
770 return;
771
772 // render the text
773 egRenderText(Text, TextBuffer, 0, 0, AverageBrightness(TextBuffer));
774 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
775 egFreeImage(TextBuffer);
776 }
777
778 // Compute the size & position of the window that will hold a subscreen's information.
779 static VOID ComputeSubScreenWindowSize(REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *XPos, UINTN *YPos,
780 UINTN *Width, UINTN *Height, UINTN *LineWidth) {
781 UINTN i, ItemWidth, HintTop, BannerBottomEdge, TitleWidth;
782 UINTN FontCellWidth = egGetFontCellWidth();
783 UINTN FontCellHeight = egGetFontHeight();
784
785 *Width = 20;
786 *Height = 5;
787 TitleWidth = egComputeTextWidth(Screen->Title);
788
789 for (i = 0; i < Screen->InfoLineCount; i++) {
790 ItemWidth = StrLen(Screen->InfoLines[i]);
791 if (*Width < ItemWidth) {
792 *Width = ItemWidth;
793 }
794 (*Height)++;
795 }
796 for (i = 0; i <= State->MaxIndex; i++) {
797 ItemWidth = StrLen(Screen->Entries[i]->Title);
798 if (*Width < ItemWidth) {
799 *Width = ItemWidth;
800 }
801 (*Height)++;
802 }
803 *Width = (*Width + 2) * FontCellWidth;
804 *LineWidth = *Width;
805 if (Screen->TitleImage)
806 *Width += (Screen->TitleImage->Width + TITLEICON_SPACING * 2 + FontCellWidth);
807 else
808 *Width += FontCellWidth;
809
810 if (*Width < TitleWidth)
811 *Width = TitleWidth + 2 * FontCellWidth;
812
813 // Keep it within the bounds of the screen, or 2/3 of the screen's width
814 // for screens over 800 pixels wide
815 if (*Width > UGAWidth)
816 *Width = UGAWidth;
817
818 *XPos = (UGAWidth - *Width) / 2;
819
820 HintTop = UGAHeight - (FontCellHeight * 3); // top of hint text
821 *Height *= TextLineHeight();
822 if (Screen->TitleImage && (*Height < (Screen->TitleImage->Height + TextLineHeight() * 4)))
823 *Height = Screen->TitleImage->Height + TextLineHeight() * 4;
824
825 if (GlobalConfig.BannerBottomEdge >= HintTop) { // probably a full-screen image; treat it as an empty banner
826 BannerBottomEdge = 0;
827 } else {
828 BannerBottomEdge = GlobalConfig.BannerBottomEdge;
829 }
830 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
831 BannerBottomEdge = 0;
832 }
833 if (*Height > (HintTop - BannerBottomEdge - FontCellHeight * 2)) {
834 // TODO: Implement scrolling in text screen.
835 *Height = (HintTop - BannerBottomEdge - FontCellHeight * 2);
836 }
837
838 *YPos = ((UGAHeight - *Height) / 2);
839 if (*YPos < BannerBottomEdge)
840 *YPos = BannerBottomEdge + FontCellHeight + (HintTop - BannerBottomEdge - *Height) / 2;
841 } // VOID ComputeSubScreenWindowSize()
842
843 // Displays sub-menus
844 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
845 {
846 INTN i;
847 UINTN ItemWidth;
848 static UINTN LineWidth, MenuWidth, MenuHeight, EntriesPosX, TitlePosX, EntriesPosY, TimeoutPosY, CharWidth;
849 EG_IMAGE *Window;
850 EG_PIXEL *BackgroundPixel = &(GlobalConfig.ScreenBackground->PixelData[0]);
851
852 CharWidth = egGetFontCellWidth();
853 State->ScrollMode = SCROLL_MODE_TEXT;
854 switch (Function) {
855
856 case MENU_FUNCTION_INIT:
857 InitScroll(State, Screen->EntryCount, 0);
858 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
859 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TextLineHeight();
860
861 // initial painting
862 SwitchToGraphicsAndClear();
863 Window = egCreateFilledImage(MenuWidth, MenuHeight, FALSE, BackgroundPixel);
864 egDrawImage(Window, EntriesPosX, EntriesPosY);
865 ItemWidth = egComputeTextWidth(Screen->Title);
866 if (MenuWidth > ItemWidth) {
867 TitlePosX = EntriesPosX + (MenuWidth - ItemWidth) / 2 - CharWidth;
868 } else {
869 TitlePosX = EntriesPosX;
870 if (CharWidth > 0) {
871 i = MenuWidth / CharWidth - 2;
872 if (i > 0)
873 Screen->Title[i] = 0;
874 } // if
875 } // if/else
876 break;
877
878 case MENU_FUNCTION_CLEANUP:
879 // nothing to do
880 break;
881
882 case MENU_FUNCTION_PAINT_ALL:
883 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
884 DrawText(Screen->Title, FALSE, (StrLen(Screen->Title) + 2) * CharWidth, TitlePosX, EntriesPosY += TextLineHeight());
885 if (Screen->TitleImage) {
886 BltImageAlpha(Screen->TitleImage, EntriesPosX + TITLEICON_SPACING, EntriesPosY + TextLineHeight() * 2,
887 BackgroundPixel);
888 EntriesPosX += (Screen->TitleImage->Width + TITLEICON_SPACING * 2);
889 }
890 EntriesPosY += (TextLineHeight() * 2);
891 if (Screen->InfoLineCount > 0) {
892 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
893 DrawText(Screen->InfoLines[i], FALSE, LineWidth, EntriesPosX, EntriesPosY);
894 EntriesPosY += TextLineHeight();
895 }
896 EntriesPosY += TextLineHeight(); // also add a blank line
897 }
898
899 for (i = 0; i <= State->MaxIndex; i++) {
900 DrawText(Screen->Entries[i]->Title, (i == State->CurrentSelection), LineWidth, EntriesPosX,
901 EntriesPosY + i * TextLineHeight());
902 }
903 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
904 if ((Screen->Hint1 != NULL) && (StrLen(Screen->Hint1) > 0))
905 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
906 UGAHeight - (egGetFontHeight() * 3));
907 if ((Screen->Hint2 != NULL) && (StrLen(Screen->Hint2) > 0))
908 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
909 UGAHeight - (egGetFontHeight() * 2));
910 } // if
911 break;
912
913 case MENU_FUNCTION_PAINT_SELECTION:
914 // redraw selection cursor
915 DrawText(Screen->Entries[State->PreviousSelection]->Title, FALSE, LineWidth,
916 EntriesPosX, EntriesPosY + State->PreviousSelection * TextLineHeight());
917 DrawText(Screen->Entries[State->CurrentSelection]->Title, TRUE, LineWidth,
918 EntriesPosX, EntriesPosY + State->CurrentSelection * TextLineHeight());
919 break;
920
921 case MENU_FUNCTION_PAINT_TIMEOUT:
922 DrawText(ParamText, FALSE, LineWidth, EntriesPosX, TimeoutPosY);
923 break;
924
925 }
926 } // static VOID GraphicsMenuStyle()
927
928 //
929 // graphical main menu style
930 //
931
932 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
933 {
934 EG_IMAGE *Background;
935
936 if (SelectionImages != NULL) {
937 if (selected) {
938 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos,
939 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
940 egComposeImage(Background, SelectionImages[Entry->Row], 0, 0);
941 BltImageCompositeBadge(Background, Entry->Image, Entry->BadgeImage, XPos, YPos);
942 } else { // Image not selected; copy background
943 egDrawImageWithTransparency(Entry->Image, Entry->BadgeImage, XPos, YPos,
944 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
945 } // if/else
946 } // if
947 } // VOID DrawMainMenuEntry()
948
949 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
950 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
951 INTN i;
952
953 if (Screen->Entries[State->CurrentSelection]->Row == 0)
954 AdjustScrollState(State);
955 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
956 if (Screen->Entries[i]->Row == 0) {
957 if (i <= State->LastVisible) {
958 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
959 itemPosX[i - State->FirstVisible], row0PosY);
960 } // if
961 } else {
962 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
963 }
964 }
965 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
966 DrawTextWithTransparency(L"", 0, textPosY);
967 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
968 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
969 textPosY);
970 }
971
972 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
973 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - egComputeTextWidth(Screen->Hint1)) / 2,
974 UGAHeight - (egGetFontHeight() * 3));
975 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - egComputeTextWidth(Screen->Hint2)) / 2,
976 UGAHeight - (egGetFontHeight() * 2));
977 } // if
978 } // static VOID PaintAll()
979
980 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
981 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
982 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
983 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
984
985 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
986 (State->CurrentSelection >= State->InitialRow1) ) {
987 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
988 XSelectPrev = State->PreviousSelection - State->FirstVisible;
989 YPosPrev = row0PosY;
990 } else {
991 XSelectPrev = State->PreviousSelection;
992 YPosPrev = row1PosY;
993 } // if/else
994 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
995 XSelectCur = State->CurrentSelection - State->FirstVisible;
996 YPosCur = row0PosY;
997 } else {
998 XSelectCur = State->CurrentSelection;
999 YPosCur = row1PosY;
1000 } // if/else
1001 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
1002 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
1003 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1004 DrawTextWithTransparency(L"", 0, textPosY);
1005 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
1006 (UGAWidth - egComputeTextWidth(Screen->Entries[State->CurrentSelection]->Title)) >> 1,
1007 textPosY);
1008 }
1009 } else { // Current selection not visible; must redraw the menu....
1010 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
1011 }
1012 } // static VOID MoveSelection(VOID)
1013
1014 // Display a 48x48 icon at the specified location. Uses the image specified by
1015 // ExternalFilename if it's available, or BuiltInImage if it's not. The
1016 // Y position is specified as the center value, and so is adjusted by half
1017 // the icon's height. The X position is set along the icon's left
1018 // edge if Alignment == ALIGN_LEFT, and along the right edge if
1019 // Alignment == ALIGN_RIGHT
1020 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
1021 EG_IMAGE *Icon = NULL;
1022
1023 Icon = egFindIcon(ExternalFilename, GlobalConfig.IconSizes[ICON_SIZE_SMALL]);
1024 if (Icon == NULL)
1025 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
1026 if (Icon != NULL) {
1027 if (Alignment == ALIGN_RIGHT)
1028 PosX -= Icon->Width;
1029 egDrawImageWithTransparency(Icon, NULL, PosX, PosY - (Icon->Height / 2), Icon->Width, Icon->Height);
1030 }
1031 } // static VOID ()
1032
1033 inline UINTN ComputeRow0PosY(VOID) {
1034 return ((UGAHeight / 2) - TileSizes[0] / 2);
1035 } // UINTN ComputeRow0PosY()
1036
1037 // Display (or erase) the arrow icons to the left and right of an icon's row,
1038 // as appropriate.
1039 static VOID PaintArrows(SCROLL_STATE *State, UINTN PosX, UINTN PosY, UINTN row0Loaders) {
1040 EG_IMAGE *TempImage;
1041 UINTN Width, Height, RightX, AdjPosY;
1042
1043 // NOTE: Assume that left and right arrows are of the same size....
1044 Width = egemb_arrow_left.Width;
1045 Height = egemb_arrow_left.Height;
1046 RightX = (UGAWidth + (TileSizes[0] + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING;
1047 AdjPosY = PosY - (Height / 2);
1048
1049 // For PaintIcon() calls, the starting Y position is moved to the midpoint
1050 // of the surrounding row; PaintIcon() adjusts this back up by half the
1051 // icon's height to properly center it.
1052 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1053 PaintIcon(&egemb_arrow_left, L"arrow_left", PosX, PosY, ALIGN_RIGHT);
1054 } else {
1055 TempImage = egCropImage(GlobalConfig.ScreenBackground, PosX - Width, AdjPosY, Width, Height);
1056 BltImage(TempImage, PosX - Width, AdjPosY);
1057 egFreeImage(TempImage);
1058 } // if/else
1059
1060 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
1061 PaintIcon(&egemb_arrow_right, L"arrow_right", RightX, PosY, ALIGN_LEFT);
1062 } else {
1063 TempImage = egCropImage(GlobalConfig.ScreenBackground, RightX, AdjPosY, Width, Height);
1064 BltImage(TempImage, RightX, AdjPosY);
1065 egFreeImage(TempImage);
1066 } // if/else
1067 } // VOID PaintArrows()
1068
1069 // Display main menu in graphics mode
1070 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
1071 {
1072 INTN i;
1073 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
1074 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
1075 static UINTN *itemPosX;
1076 static UINTN row0PosY, textPosY;
1077
1078 State->ScrollMode = SCROLL_MODE_ICONS;
1079 switch (Function) {
1080
1081 case MENU_FUNCTION_INIT:
1082 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
1083
1084 // layout
1085 row0Count = 0;
1086 row1Count = 0;
1087 row0Loaders = 0;
1088 for (i = 0; i <= State->MaxIndex; i++) {
1089 if (Screen->Entries[i]->Row == 1) {
1090 row1Count++;
1091 } else {
1092 row0Loaders++;
1093 if (row0Count < State->MaxVisible)
1094 row0Count++;
1095 }
1096 }
1097 row0PosX = (UGAWidth + TILE_XSPACING - (TileSizes[0] + TILE_XSPACING) * row0Count) >> 1;
1098 row0PosY = ComputeRow0PosY();
1099 row1PosX = (UGAWidth + TILE_XSPACING - (TileSizes[1] + TILE_XSPACING) * row1Count) >> 1;
1100 row1PosY = row0PosY + TileSizes[0] + TILE_YSPACING;
1101 if (row1Count > 0)
1102 textPosY = row1PosY + TileSizes[1] + TILE_YSPACING;
1103 else
1104 textPosY = row1PosY;
1105
1106 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1107 row0PosXRunning = row0PosX;
1108 row1PosXRunning = row1PosX;
1109 for (i = 0; i <= State->MaxIndex; i++) {
1110 if (Screen->Entries[i]->Row == 0) {
1111 itemPosX[i] = row0PosXRunning;
1112 row0PosXRunning += TileSizes[0] + TILE_XSPACING;
1113 } else {
1114 itemPosX[i] = row1PosXRunning;
1115 row1PosXRunning += TileSizes[1] + TILE_XSPACING;
1116 }
1117 }
1118 // initial painting
1119 InitSelection();
1120 SwitchToGraphicsAndClear();
1121 break;
1122
1123 case MENU_FUNCTION_CLEANUP:
1124 MyFreePool(itemPosX);
1125 break;
1126
1127 case MENU_FUNCTION_PAINT_ALL:
1128 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1129 // For PaintArrows(), the starting Y position is moved to the midpoint
1130 // of the surrounding row; PaintIcon() adjusts this back up by half the
1131 // icon's height to properly center it.
1132 PaintArrows(State, row0PosX - TILE_XSPACING, row0PosY + (TileSizes[0] / 2), row0Loaders);
1133 break;
1134
1135 case MENU_FUNCTION_PAINT_SELECTION:
1136 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1137 break;
1138
1139 case MENU_FUNCTION_PAINT_TIMEOUT:
1140 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL)) {
1141 DrawTextWithTransparency(L"", 0, textPosY + TextLineHeight());
1142 DrawTextWithTransparency(ParamText, (UGAWidth - egComputeTextWidth(ParamText)) >> 1, textPosY + TextLineHeight());
1143 }
1144 break;
1145
1146 }
1147 } // VOID MainMenuStyle()
1148
1149 // Enable the user to edit boot loader options.
1150 // Returns TRUE if the user exited with edited options; FALSE if the user
1151 // pressed Esc to terminate the edit.
1152 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
1153 UINTN x_max, y_max;
1154 CHAR16 *EditedOptions;
1155 BOOLEAN retval = FALSE;
1156
1157 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
1158 return FALSE;
1159 }
1160
1161 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
1162
1163 if (!GlobalConfig.TextOnly)
1164 SwitchToText(TRUE);
1165
1166 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
1167 MyFreePool(MenuEntry->LoadOptions);
1168 MenuEntry->LoadOptions = EditedOptions;
1169 retval = TRUE;
1170 } // if
1171 if (!GlobalConfig.TextOnly)
1172 SwitchToGraphics();
1173 return retval;
1174 } // VOID EditOptions()
1175
1176 //
1177 // user-callable dispatcher functions
1178 //
1179
1180 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
1181 {
1182 INTN DefaultEntry = -1;
1183 MENU_STYLE_FUNC Style = TextMenuStyle;
1184
1185 if (AllowGraphicsMode)
1186 Style = GraphicsMenuStyle;
1187
1188 return RunGenericMenu(Screen, Style, &DefaultEntry, ChosenEntry);
1189 }
1190
1191 UINTN RunMainMenu(REFIT_MENU_SCREEN *Screen, CHAR16** DefaultSelection, REFIT_MENU_ENTRY **ChosenEntry)
1192 {
1193 MENU_STYLE_FUNC Style = TextMenuStyle;
1194 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
1195 REFIT_MENU_ENTRY *TempChosenEntry;
1196 CHAR16 *MenuTitle;
1197 UINTN MenuExit = 0;
1198 INTN DefaultEntryIndex = -1;
1199 INTN DefaultSubmenuIndex = -1;
1200
1201 TileSizes[0] = (GlobalConfig.IconSizes[ICON_SIZE_BIG] * 9) / 8;
1202 TileSizes[1] = (GlobalConfig.IconSizes[ICON_SIZE_SMALL] * 4) / 3;
1203
1204 if ((DefaultSelection != NULL) && (*DefaultSelection != NULL)) {
1205 // Find a menu entry that includes *DefaultSelection as a substring
1206 DefaultEntryIndex = FindMenuShortcutEntry(Screen, *DefaultSelection);
1207 }
1208
1209 if (AllowGraphicsMode) {
1210 Style = GraphicsMenuStyle;
1211 MainStyle = MainMenuStyle;
1212 }
1213
1214 while (!MenuExit) {
1215 MenuExit = RunGenericMenu(Screen, MainStyle, &DefaultEntryIndex, &TempChosenEntry);
1216 Screen->TimeoutSeconds = 0;
1217
1218 MenuTitle = StrDuplicate(TempChosenEntry->Title);
1219 if (MenuExit == MENU_EXIT_DETAILS) {
1220 if (TempChosenEntry->SubScreen != NULL) {
1221 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, &DefaultSubmenuIndex, &TempChosenEntry);
1222 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1223 MenuExit = 0;
1224 if (MenuExit == MENU_EXIT_DETAILS) {
1225 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1226 MenuExit = 0;
1227 } // if
1228 } else { // no sub-screen; ignore keypress
1229 MenuExit = 0;
1230 }
1231 } // Enter sub-screen
1232 }
1233
1234 if (ChosenEntry)
1235 *ChosenEntry = TempChosenEntry;
1236 if (DefaultSelection) {
1237 MyFreePool(*DefaultSelection);
1238 *DefaultSelection = MenuTitle;
1239 } // if
1240 return MenuExit;
1241 } /* UINTN RunMainMenu() */