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