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