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