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