]> code.delx.au - refind/blob - refind/menu.c
Version 0.6.5 release.
[refind] / refind / menu.c
1 /*
2 * refit/menu.c
3 * Menu functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "global.h"
46 #include "screen.h"
47 #include "lib.h"
48 #include "menu.h"
49 #include "config.h"
50 #include "libeg.h"
51 #include "libegint.h"
52 #include "../include/refit_call_wrapper.h"
53
54 #include "../include/egemb_back_selected_small.h"
55 #include "../include/egemb_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 //
354 // generic menu function
355 //
356 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN OUT INTN *DefaultEntryIndex,
357 OUT REFIT_MENU_ENTRY **ChosenEntry)
358 {
359 SCROLL_STATE State;
360 EFI_STATUS Status;
361 EFI_INPUT_KEY key;
362 UINTN index;
363 INTN ShortcutEntry;
364 BOOLEAN HaveTimeout = FALSE;
365 UINTN TimeoutCountdown = 0;
366 CHAR16 TimeoutMessage[256];
367 CHAR16 KeyAsString[2];
368 UINTN MenuExit;
369
370 if (Screen->TimeoutSeconds > 0) {
371 HaveTimeout = TRUE;
372 TimeoutCountdown = Screen->TimeoutSeconds * 10;
373 }
374 MenuExit = 0;
375
376 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
377 IdentifyRows(&State, Screen);
378 // override the starting selection with the default index, if any
379 if (*DefaultEntryIndex >= 0 && *DefaultEntryIndex <= State.MaxIndex) {
380 State.CurrentSelection = *DefaultEntryIndex;
381 UpdateScroll(&State, SCROLL_NONE);
382 }
383 State.PaintAll = TRUE;
384
385 while (!MenuExit) {
386 // update the screen
387 if (State.PaintAll) {
388 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
389 State.PaintAll = FALSE;
390 } else if (State.PaintSelection) {
391 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
392 State.PaintSelection = FALSE;
393 }
394
395 if (HaveTimeout) {
396 SPrint(TimeoutMessage, 255, L"%s in %d seconds", Screen->TimeoutText, (TimeoutCountdown + 5) / 10);
397 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
398 }
399
400 // read key press (and wait for it if applicable)
401 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
402 if (Status == EFI_NOT_READY) {
403 if (HaveTimeout && TimeoutCountdown == 0) {
404 // timeout expired
405 MenuExit = MENU_EXIT_TIMEOUT;
406 break;
407 } else if (HaveTimeout) {
408 refit_call1_wrapper(BS->Stall, 100000);
409 TimeoutCountdown--;
410 } else
411 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
412 continue;
413 }
414 if (HaveTimeout) {
415 // the user pressed a key, cancel the timeout
416 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
417 HaveTimeout = FALSE;
418 }
419
420 // react to key press
421 switch (key.ScanCode) {
422 case SCAN_UP:
423 UpdateScroll(&State, SCROLL_LINE_UP);
424 break;
425 case SCAN_LEFT:
426 UpdateScroll(&State, SCROLL_LINE_LEFT);
427 break;
428 case SCAN_DOWN:
429 UpdateScroll(&State, SCROLL_LINE_DOWN);
430 break;
431 case SCAN_RIGHT:
432 UpdateScroll(&State, SCROLL_LINE_RIGHT);
433 break;
434 case SCAN_HOME:
435 UpdateScroll(&State, SCROLL_FIRST);
436 break;
437 case SCAN_END:
438 UpdateScroll(&State, SCROLL_LAST);
439 break;
440 case SCAN_PAGE_UP:
441 UpdateScroll(&State, SCROLL_PAGE_UP);
442 break;
443 case SCAN_PAGE_DOWN:
444 UpdateScroll(&State, SCROLL_PAGE_DOWN);
445 break;
446 case SCAN_ESC:
447 MenuExit = MENU_EXIT_ESCAPE;
448 break;
449 case SCAN_INSERT:
450 case SCAN_F2:
451 MenuExit = MENU_EXIT_DETAILS;
452 break;
453 case SCAN_F10:
454 egScreenShot();
455 break;
456 case 0x0016: // F12
457 if (EjectMedia())
458 MenuExit = MENU_EXIT_ESCAPE;
459 break;
460 }
461 switch (key.UnicodeChar) {
462 case CHAR_LINEFEED:
463 case CHAR_CARRIAGE_RETURN:
464 case ' ':
465 MenuExit = MENU_EXIT_ENTER;
466 break;
467 case '+':
468 MenuExit = MENU_EXIT_DETAILS;
469 break;
470 default:
471 KeyAsString[0] = key.UnicodeChar;
472 KeyAsString[1] = 0;
473 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
474 if (ShortcutEntry >= 0) {
475 State.CurrentSelection = ShortcutEntry;
476 MenuExit = MENU_EXIT_ENTER;
477 }
478 break;
479 }
480 }
481
482 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
483
484 if (ChosenEntry)
485 *ChosenEntry = Screen->Entries[State.CurrentSelection];
486 *DefaultEntryIndex = State.CurrentSelection;
487 return MenuExit;
488 } /* static UINTN RunGenericMenu() */
489
490 //
491 // text-mode generic style
492 //
493
494 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
495 {
496 INTN i;
497 UINTN MenuWidth, ItemWidth, MenuHeight;
498 static UINTN MenuPosY;
499 static CHAR16 **DisplayStrings;
500 CHAR16 TimeoutMessage[256];
501
502 State->ScrollMode = SCROLL_MODE_TEXT;
503 switch (Function) {
504
505 case MENU_FUNCTION_INIT:
506 // vertical layout
507 MenuPosY = 4;
508 if (Screen->InfoLineCount > 0)
509 MenuPosY += Screen->InfoLineCount + 1;
510 MenuHeight = ConHeight - MenuPosY - 3;
511 if (Screen->TimeoutSeconds > 0)
512 MenuHeight -= 2;
513 InitScroll(State, Screen->EntryCount, MenuHeight);
514
515 // determine width of the menu
516 MenuWidth = 20; // minimum
517 for (i = 0; i <= State->MaxIndex; i++) {
518 ItemWidth = StrLen(Screen->Entries[i]->Title);
519 if (MenuWidth < ItemWidth)
520 MenuWidth = ItemWidth;
521 }
522 MenuWidth += 2;
523 if (MenuWidth > ConWidth - 3)
524 MenuWidth = ConWidth - 3;
525
526 // prepare strings for display
527 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
528 for (i = 0; i <= State->MaxIndex; i++) {
529 // Note: Theoretically, SPrint() is a cleaner way to do this; but the
530 // description of the StrSize parameter to SPrint implies it's measured
531 // in characters, but in practice both TianoCore and GNU-EFI seem to
532 // use bytes instead, resulting in truncated displays. I could just
533 // double the size of the StrSize parameter, but that seems unsafe in
534 // case a future library change starts treating this as characters, so
535 // I'm doing it the hard way in this instance.
536 // TODO: Review the above and possibly change other uses of SPrint()
537 DisplayStrings[i] = AllocateZeroPool(2 * sizeof(CHAR16));
538 DisplayStrings[i][0] = L' ';
539 MergeStrings(&DisplayStrings[i], Screen->Entries[i]->Title, 0);
540 if (StrLen(DisplayStrings[i]) > MenuWidth)
541 DisplayStrings[i][MenuWidth - 1] = 0;
542 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
543 // TODO: account for double-width characters
544 } // for
545
546 // initial painting
547 BeginTextScreen(Screen->Title);
548 if (Screen->InfoLineCount > 0) {
549 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
550 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
551 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
552 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
553 }
554 }
555
556 break;
557
558 case MENU_FUNCTION_CLEANUP:
559 // release temporary memory
560 for (i = 0; i <= State->MaxIndex; i++)
561 MyFreePool(DisplayStrings[i]);
562 MyFreePool(DisplayStrings);
563 break;
564
565 case MENU_FUNCTION_PAINT_ALL:
566 // paint the whole screen (initially and after scrolling)
567 for (i = 0; i <= State->MaxIndex; i++) {
568 if (i >= State->FirstVisible && i <= State->LastVisible) {
569 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
570 if (i == State->CurrentSelection)
571 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
572 else
573 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
574 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
575 }
576 }
577 // scrolling indicators
578 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
579 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
580 if (State->FirstVisible > 0)
581 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
582 else
583 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
584 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
585 if (State->LastVisible < State->MaxIndex)
586 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
587 else
588 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
589 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
590 if (Screen->Hint1 != NULL) {
591 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 2);
592 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint1);
593 }
594 if (Screen->Hint2 != NULL) {
595 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
596 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->Hint2);
597 }
598 }
599 break;
600
601 case MENU_FUNCTION_PAINT_SELECTION:
602 // redraw selection cursor
603 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
604 MenuPosY + (State->PreviousSelection - State->FirstVisible));
605 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
606 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->PreviousSelection]);
607 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2,
608 MenuPosY + (State->CurrentSelection - State->FirstVisible));
609 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
610 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
611 break;
612
613 case MENU_FUNCTION_PAINT_TIMEOUT:
614 if (ParamText[0] == 0) {
615 // clear message
616 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
617 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 3);
618 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
619 } else {
620 // paint or update message
621 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
622 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 3);
623 SPrint(TimeoutMessage, 255, L"%s ", ParamText);
624 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
625 }
626 break;
627
628 }
629 }
630
631 //
632 // graphical generic style
633 //
634
635 //
636 // Display a submenu
637 //
638
639 // Display text with a solid background (MenuBackgroundPixel or SelectionBackgroundPixel)
640 static VOID DrawText(IN CHAR16 *Text, IN BOOLEAN Selected, IN UINTN FieldWidth, IN UINTN XPos, IN UINTN YPos)
641 {
642 EG_IMAGE *TextBuffer;
643 EG_PIXEL Bg;
644
645 TextBuffer = egCreateImage(FieldWidth, TEXT_LINE_HEIGHT, FALSE);
646
647 egFillImage(TextBuffer, &MenuBackgroundPixel);
648 Bg = MenuBackgroundPixel;
649 if (Selected) {
650 // draw selection bar background
651 egFillImageArea(TextBuffer, 0, 0, FieldWidth, TextBuffer->Height, &SelectionBackgroundPixel);
652 Bg = SelectionBackgroundPixel;
653 }
654
655 // render the text
656 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN, (Bg.r + Bg.g + Bg.b) / 3);
657 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
658 // BltImage(TextBuffer, XPos, YPos);
659 }
660
661 // Finds the average brightness of the input Image.
662 // NOTE: Passing an Image that covers the whole screen can strain the
663 // capacity of a UINTN on a 32-bit system with a very large display.
664 // Using UINT64 instead is unworkable, since the code won't compile
665 // on a 32-bit system. As the intended use for this function is to handle
666 // a single text string's background, this shouldn't be a problem, but it
667 // may need addressing if it's applied more broadly....
668 static UINT8 AverageBrightness(EG_IMAGE *Image) {
669 UINTN i;
670 UINTN Sum = 0;
671
672 if (Image != NULL) {
673 for (i = 0; i < (Image->Width * Image->Height); i++) {
674 Sum += (Image->PixelData[i].r + Image->PixelData[i].g + Image->PixelData[i].b);
675 }
676 } // if
677 return (UINT8) (Sum / (Image->Width * Image->Height * 3));
678 } // UINT8 AverageBrightness()
679
680 // Display text against the screen's background image
681 static VOID DrawTextWithTransparency(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
682 {
683 UINTN TextWidth, TextPosX;
684 EG_IMAGE *TextBuffer;
685
686 TextBuffer = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos, LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT);
687
688 // render the text
689 egMeasureText(Text, &TextWidth, NULL);
690 if (TextWidth > TextBuffer->Width)
691 TextPosX = 0;
692 else
693 TextPosX = (TextBuffer->Width - TextWidth) / 2;
694 egRenderText(Text, TextBuffer, TextPosX, 0, AverageBrightness(TextBuffer));
695 egDrawImageWithTransparency(TextBuffer, NULL, XPos, YPos, TextBuffer->Width, TextBuffer->Height);
696 }
697
698 // Compute the size & position of the window that will hold a subscreen's information.
699 static VOID ComputeSubScreenWindowSize(REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *XPos, UINTN *YPos, UINTN *Width, UINTN *Height, UINTN *LineWidth) {
700 UINTN i, ItemWidth, HintTop, BannerBottomEdge, TitleWidth;
701
702 *Width = 20;
703 *Height = 5;
704 TitleWidth = StrLen(Screen->Title);
705 if ((Screen->TitleImage) && (TitleWidth > (Screen->TitleImage->Width / FONT_CELL_WIDTH))) {
706 TitleWidth -= (Screen->TitleImage->Width / FONT_CELL_WIDTH);
707 }
708 if (TitleWidth > *Width) {
709 *Width = TitleWidth;
710 }
711 for (i = 0; i < Screen->InfoLineCount; i++) {
712 ItemWidth = StrLen(Screen->InfoLines[i]);
713 if (*Width < ItemWidth) {
714 *Width = ItemWidth;
715 }
716 (*Height)++;
717 }
718 for (i = 0; i <= State->MaxIndex; i++) {
719 ItemWidth = StrLen(Screen->Entries[i]->Title);
720 if (*Width < ItemWidth) {
721 *Width = ItemWidth;
722 }
723 (*Height)++;
724 }
725 *Width = TEXT_XMARGIN * 2 + *Width * FONT_CELL_WIDTH;
726 *LineWidth = *Width;
727 if (Screen->TitleImage)
728 *Width += (Screen->TitleImage->Width + TITLEICON_SPACING * 2 + FONT_CELL_WIDTH);
729 else
730 *Width += FONT_CELL_WIDTH;
731
732 // Keep it within the bounds of the screen, or 2/3 of the screen's width
733 // for screens over 800 pixels wide
734 if (*Width > UGAWidth)
735 *Width = UGAWidth;
736 if ((*Width > (2 * UGAWidth) / 3) && (UGAWidth > 800))
737 *Width = (2 * UGAWidth) / 3;
738
739 *XPos = (UGAWidth - *Width) / 2;
740
741 HintTop = UGAHeight - (FONT_CELL_HEIGHT * 3); // top of hint text
742 *Height *= TEXT_LINE_HEIGHT;
743 if (Screen->TitleImage && (*Height < (Screen->TitleImage->Height + TEXT_LINE_HEIGHT * 4)))
744 *Height = Screen->TitleImage->Height + TEXT_LINE_HEIGHT * 4;
745
746 if (GlobalConfig.BannerBottomEdge >= HintTop) { // probably a full-screen image; treat it as an empty banner
747 BannerBottomEdge = 0;
748 } else {
749 BannerBottomEdge = GlobalConfig.BannerBottomEdge;
750 }
751 if (*Height > (HintTop - BannerBottomEdge - FONT_CELL_HEIGHT * 2)) {
752 BannerBottomEdge = 0;
753 }
754 if (*Height > (HintTop - BannerBottomEdge - FONT_CELL_HEIGHT * 2)) {
755 // TODO: Implement scrolling in text screen.
756 *Height = (HintTop - BannerBottomEdge - FONT_CELL_HEIGHT * 2);
757 }
758
759 *YPos = ((UGAHeight - *Height) / 2);
760 if (*YPos < BannerBottomEdge)
761 *YPos = BannerBottomEdge + FONT_CELL_HEIGHT + (HintTop - BannerBottomEdge - *Height) / 2;
762 } // VOID ComputeSubScreenWindowSize()
763
764 // Displays sub-menus
765 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
766 {
767 INTN i;
768 UINTN ItemWidth;
769 static UINTN LineWidth, MenuWidth, MenuHeight, EntriesPosX, EntriesPosY, TimeoutPosY;
770 EG_IMAGE *Window;
771 EG_PIXEL *BackgroundPixel = &(GlobalConfig.ScreenBackground->PixelData[0]);
772
773 State->ScrollMode = SCROLL_MODE_TEXT;
774 switch (Function) {
775
776 case MENU_FUNCTION_INIT:
777 InitScroll(State, Screen->EntryCount, 0);
778 ComputeSubScreenWindowSize(Screen, State, &EntriesPosX, &EntriesPosY, &MenuWidth, &MenuHeight, &LineWidth);
779 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TEXT_LINE_HEIGHT;
780
781 // initial painting
782 SwitchToGraphicsAndClear();
783 Window = egCreateFilledImage(MenuWidth, MenuHeight, FALSE, BackgroundPixel);
784 egDrawImage(Window, EntriesPosX, EntriesPosY);
785 egMeasureText(Screen->Title, &ItemWidth, NULL);
786 DrawText(Screen->Title, FALSE, (StrLen(Screen->Title) + 2) * FONT_CELL_WIDTH,
787 EntriesPosX + (MenuWidth - ItemWidth) / 2, EntriesPosY += TEXT_LINE_HEIGHT);
788 if (Screen->TitleImage) {
789 BltImageAlpha(Screen->TitleImage, EntriesPosX + TITLEICON_SPACING, EntriesPosY + TEXT_LINE_HEIGHT * 2,
790 BackgroundPixel);
791 EntriesPosX += (Screen->TitleImage->Width + TITLEICON_SPACING * 2);
792 }
793 EntriesPosY += (TEXT_LINE_HEIGHT * 2);
794 if (Screen->InfoLineCount > 0) {
795 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
796 DrawText(Screen->InfoLines[i], FALSE, LineWidth, EntriesPosX, EntriesPosY);
797 EntriesPosY += TEXT_LINE_HEIGHT;
798 }
799 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
800 }
801
802 break;
803
804 case MENU_FUNCTION_CLEANUP:
805 // nothing to do
806 break;
807
808 case MENU_FUNCTION_PAINT_ALL:
809 for (i = 0; i <= State->MaxIndex; i++) {
810 DrawText(Screen->Entries[i]->Title, (i == State->CurrentSelection), LineWidth, EntriesPosX,
811 EntriesPosY + i * TEXT_LINE_HEIGHT);
812 }
813 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
814 if ((Screen->Hint1 != NULL) && (StrLen(Screen->Hint1) > 0))
815 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 3));
816 if ((Screen->Hint2 != NULL) && (StrLen(Screen->Hint2) > 0))
817 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 2));
818 } // if
819 break;
820
821 case MENU_FUNCTION_PAINT_SELECTION:
822 // redraw selection cursor
823 DrawText(Screen->Entries[State->PreviousSelection]->Title, FALSE, LineWidth,
824 EntriesPosX, EntriesPosY + State->PreviousSelection * TEXT_LINE_HEIGHT);
825 DrawText(Screen->Entries[State->CurrentSelection]->Title, TRUE, LineWidth,
826 EntriesPosX, EntriesPosY + State->CurrentSelection * TEXT_LINE_HEIGHT);
827 break;
828
829 case MENU_FUNCTION_PAINT_TIMEOUT:
830 DrawText(ParamText, FALSE, LineWidth, EntriesPosX, TimeoutPosY);
831 break;
832
833 }
834 } // static VOID GraphicsMenuStyle()
835
836 //
837 // graphical main menu style
838 //
839
840 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
841 {
842 EG_IMAGE *Background;
843
844 if (SelectionImages != NULL) {
845 if (selected) {
846 Background = egCropImage(GlobalConfig.ScreenBackground, XPos, YPos,
847 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
848 egComposeImage(Background, SelectionImages[Entry->Row], 0, 0);
849 BltImageCompositeBadge(Background, Entry->Image, Entry->BadgeImage, XPos, YPos);
850 } else { // Image not selected; copy background
851 egDrawImageWithTransparency(Entry->Image, Entry->BadgeImage, XPos, YPos,
852 SelectionImages[Entry->Row]->Width, SelectionImages[Entry->Row]->Height);
853 } // if/else
854 } // if
855 } // VOID DrawMainMenuEntry()
856
857 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
858 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
859 INTN i;
860
861 if (Screen->Entries[State->CurrentSelection]->Row == 0)
862 AdjustScrollState(State);
863 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
864 if (Screen->Entries[i]->Row == 0) {
865 if (i <= State->LastVisible) {
866 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
867 itemPosX[i - State->FirstVisible], row0PosY);
868 } // if
869 } else {
870 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE, itemPosX[i], row1PosY);
871 }
872 }
873 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
874 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
875 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
876
877 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_HINTS)) {
878 DrawTextWithTransparency(Screen->Hint1, (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 3));
879 DrawTextWithTransparency(Screen->Hint2, (UGAWidth - LAYOUT_TEXT_WIDTH) / 2, UGAHeight - (FONT_CELL_HEIGHT * 2));
880 } // if
881 } // static VOID PaintAll()
882
883 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
884 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
885 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
886 UINTN XSelectPrev, XSelectCur, YPosPrev, YPosCur;
887
888 if (((State->CurrentSelection <= State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) ||
889 (State->CurrentSelection >= State->InitialRow1) ) {
890 if (Screen->Entries[State->PreviousSelection]->Row == 0) {
891 XSelectPrev = State->PreviousSelection - State->FirstVisible;
892 YPosPrev = row0PosY;
893 } else {
894 XSelectPrev = State->PreviousSelection;
895 YPosPrev = row1PosY;
896 } // if/else
897 if (Screen->Entries[State->CurrentSelection]->Row == 0) {
898 XSelectCur = State->CurrentSelection - State->FirstVisible;
899 YPosCur = row0PosY;
900 } else {
901 XSelectCur = State->CurrentSelection;
902 YPosCur = row1PosY;
903 } // if/else
904 DrawMainMenuEntry(Screen->Entries[State->PreviousSelection], FALSE, itemPosX[XSelectPrev], YPosPrev);
905 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE, itemPosX[XSelectCur], YPosCur);
906 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
907 DrawTextWithTransparency(Screen->Entries[State->CurrentSelection]->Title,
908 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
909 } else { // Current selection not visible; must redraw the menu....
910 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
911 }
912 } // static VOID MoveSelection(VOID)
913
914 // Display an icon at the specified location. Uses the image specified by
915 // ExternalFilename if it's available, or BuiltInImage if it's not. The
916 // Y position is specified as the center value, and so is adjusted by half
917 // the icon's height. The X position is set along the icon's left
918 // edge if Alignment == ALIGN_LEFT, and along the right edge if
919 // Alignment == ALIGN_RIGHT
920 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
921 EG_IMAGE *Icon = NULL;
922
923 if (FileExists(SelfDir, ExternalFilename))
924 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
925 if (Icon == NULL)
926 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
927 if (Icon != NULL) {
928 if (Alignment == ALIGN_RIGHT)
929 PosX -= Icon->Width;
930 egDrawImageWithTransparency(Icon, NULL, PosX, PosY - (Icon->Height / 2), Icon->Width, Icon->Height);
931 }
932 } // static VOID PaintIcon()
933
934 inline UINTN ComputeRow0PosX(VOID) {
935 return ((UGAHeight / 2) - ROW0_TILESIZE / 2);
936 } // UINTN ComputeRow0PosX()
937
938 // Display (or erase) the arrow icons to the left and right of an icon's row,
939 // as appropriate.
940 static VOID PaintArrows(SCROLL_STATE *State, UINTN PosX, UINTN PosY, UINTN row0Loaders) {
941 CHAR16 FileName[256];
942 EG_IMAGE *TempImage;
943 UINTN Width, Height, RightX, AdjPosY;
944
945 // NOTE: Assume that left and right arrows are of the same size....
946 Width = egemb_arrow_left.Width;
947 Height = egemb_arrow_left.Height;
948 RightX = (UGAWidth + (ROW0_TILESIZE + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING;
949 AdjPosY = PosY - (Height / 2);
950
951 // For PaintIcon() calls, the starting Y position is moved to the midpoint
952 // of the surrounding row; PaintIcon() adjusts this back up by half the
953 // icon's height to properly center it.
954 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
955 SPrint(FileName, 255, L"%s\\arrow_left.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
956 PaintIcon(&egemb_arrow_left, FileName, PosX, PosY, ALIGN_RIGHT);
957 } else {
958 TempImage = egCropImage(GlobalConfig.ScreenBackground, PosX - Width, AdjPosY, Width, Height);
959 BltImage(TempImage, PosX - Width, AdjPosY);
960 egFreeImage(TempImage);
961 } // if/else
962
963 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS))) {
964 SPrint(FileName, 255, L"%s\\arrow_right.icns", GlobalConfig.IconsDir ? GlobalConfig.IconsDir : DEFAULT_ICONS_DIR);
965 PaintIcon(&egemb_arrow_right, FileName, RightX, PosY, ALIGN_LEFT);
966 } else {
967 TempImage = egCropImage(GlobalConfig.ScreenBackground, RightX, AdjPosY, Width, Height);
968 BltImage(TempImage, RightX, AdjPosY);
969 egFreeImage(TempImage);
970 } // if/else
971 } // VOID PaintArrows()
972
973 // Display main menu in graphics mode
974 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
975 {
976 INTN i;
977 static UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
978 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
979 static UINTN *itemPosX;
980 static UINTN row0PosY, textPosY;
981
982 State->ScrollMode = SCROLL_MODE_ICONS;
983 switch (Function) {
984
985 case MENU_FUNCTION_INIT:
986 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
987
988 // layout
989 row0Count = 0;
990 row1Count = 0;
991 row0Loaders = 0;
992 for (i = 0; i <= State->MaxIndex; i++) {
993 if (Screen->Entries[i]->Row == 1) {
994 row1Count++;
995 } else {
996 row0Loaders++;
997 if (row0Count < State->MaxVisible)
998 row0Count++;
999 }
1000 }
1001 row0PosX = (UGAWidth + TILE_XSPACING - (ROW0_TILESIZE + TILE_XSPACING) * row0Count) >> 1;
1002 row0PosY = ComputeRow0PosX();
1003 row1PosX = (UGAWidth + TILE_XSPACING - (ROW1_TILESIZE + TILE_XSPACING) * row1Count) >> 1;
1004 row1PosY = row0PosY + ROW0_TILESIZE + TILE_YSPACING;
1005 if (row1Count > 0)
1006 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
1007 else
1008 textPosY = row1PosY;
1009
1010 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
1011 row0PosXRunning = row0PosX;
1012 row1PosXRunning = row1PosX;
1013 for (i = 0; i <= State->MaxIndex; i++) {
1014 if (Screen->Entries[i]->Row == 0) {
1015 itemPosX[i] = row0PosXRunning;
1016 row0PosXRunning += ROW0_TILESIZE + TILE_XSPACING;
1017 } else {
1018 itemPosX[i] = row1PosXRunning;
1019 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
1020 }
1021 }
1022 // initial painting
1023 InitSelection();
1024 SwitchToGraphicsAndClear();
1025 break;
1026
1027 case MENU_FUNCTION_CLEANUP:
1028 MyFreePool(itemPosX);
1029 break;
1030
1031 case MENU_FUNCTION_PAINT_ALL:
1032 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1033 // For PaintArrows(), the starting Y position is moved to the midpoint
1034 // of the surrounding row; PaintIcon() adjusts this back up by half the
1035 // icon's height to properly center it.
1036 PaintArrows(State, row0PosX - TILE_XSPACING, row0PosY + (ROW0_TILESIZE / 2), row0Loaders);
1037 break;
1038
1039 case MENU_FUNCTION_PAINT_SELECTION:
1040 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
1041 break;
1042
1043 case MENU_FUNCTION_PAINT_TIMEOUT:
1044 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
1045 DrawTextWithTransparency(ParamText, (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY + TEXT_LINE_HEIGHT);
1046 break;
1047
1048 }
1049 } // VOID MainMenuStyle()
1050
1051 // Enable the user to edit boot loader options.
1052 // Returns TRUE if the user exited with edited options; FALSE if the user
1053 // pressed Esc to terminate the edit.
1054 static BOOLEAN EditOptions(LOADER_ENTRY *MenuEntry) {
1055 UINTN x_max, y_max;
1056 CHAR16 *EditedOptions;
1057 BOOLEAN retval = FALSE;
1058
1059 if (GlobalConfig.HideUIFlags & HIDEUI_FLAG_EDITOR) {
1060 return FALSE;
1061 }
1062
1063 refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, ST->ConOut->Mode->Mode, &x_max, &y_max);
1064
1065 if (!GlobalConfig.TextOnly)
1066 SwitchToText(TRUE);
1067
1068 if (line_edit(MenuEntry->LoadOptions, &EditedOptions, x_max)) {
1069 MyFreePool(MenuEntry->LoadOptions);
1070 MenuEntry->LoadOptions = EditedOptions;
1071 retval = TRUE;
1072 } // if
1073 if (!GlobalConfig.TextOnly)
1074 SwitchToGraphics();
1075 return retval;
1076 } // VOID EditOptions()
1077
1078 //
1079 // user-callable dispatcher functions
1080 //
1081
1082 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
1083 {
1084 INTN DefaultEntry = -1;
1085 MENU_STYLE_FUNC Style = TextMenuStyle;
1086
1087 if (AllowGraphicsMode)
1088 Style = GraphicsMenuStyle;
1089
1090 return RunGenericMenu(Screen, Style, &DefaultEntry, ChosenEntry);
1091 }
1092
1093 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
1094 {
1095 MENU_STYLE_FUNC Style = TextMenuStyle;
1096 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
1097 REFIT_MENU_ENTRY *TempChosenEntry;
1098 UINTN MenuExit = 0;
1099 INTN DefaultEntryIndex = -1;
1100 INTN DefaultSubmenuIndex = -1;
1101
1102 if (DefaultSelection != NULL) {
1103 // Find a menu entry that includes *DefaultSelection as a substring
1104 DefaultEntryIndex = FindMenuShortcutEntry(Screen, DefaultSelection);
1105 }
1106
1107 if (AllowGraphicsMode) {
1108 Style = GraphicsMenuStyle;
1109 MainStyle = MainMenuStyle;
1110 }
1111
1112 while (!MenuExit) {
1113 MenuExit = RunGenericMenu(Screen, MainStyle, &DefaultEntryIndex, &TempChosenEntry);
1114 Screen->TimeoutSeconds = 0;
1115
1116 if (MenuExit == MENU_EXIT_DETAILS) {
1117 if (TempChosenEntry->SubScreen != NULL) {
1118 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, &DefaultSubmenuIndex, &TempChosenEntry);
1119 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
1120 MenuExit = 0;
1121 if (MenuExit == MENU_EXIT_DETAILS) {
1122 if (!EditOptions((LOADER_ENTRY *) TempChosenEntry))
1123 MenuExit = 0;
1124 } // if
1125 } else { // no sub-screen; ignore keypress
1126 MenuExit = 0;
1127 }
1128 } // Enter sub-screen
1129 }
1130
1131 if (ChosenEntry)
1132 *ChosenEntry = TempChosenEntry;
1133 return MenuExit;
1134 } /* UINTN RunMainMenu() */