]> code.delx.au - refind/blob - refind/menu.c
Improved text-mode display's initial list & scrolling
[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 "refit_call_wrapper.h"
52
53 #include "egemb_back_selected_small.h"
54 #include "egemb_arrow_left.h"
55 #include "egemb_arrow_right.h"
56
57 // other menu definitions
58
59 #define MENU_FUNCTION_INIT (0)
60 #define MENU_FUNCTION_CLEANUP (1)
61 #define MENU_FUNCTION_PAINT_ALL (2)
62 #define MENU_FUNCTION_PAINT_SELECTION (3)
63 #define MENU_FUNCTION_PAINT_TIMEOUT (4)
64
65 typedef VOID (*MENU_STYLE_FUNC)(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText);
66
67 static CHAR16 ArrowUp[2] = { ARROW_UP, 0 };
68 static CHAR16 ArrowDown[2] = { ARROW_DOWN, 0 };
69
70 #define TEXT_YMARGIN (2)
71 #define TEXT_XMARGIN (8)
72 #define TEXT_LINE_HEIGHT (FONT_CELL_HEIGHT + TEXT_YMARGIN * 2)
73 #define TITLEICON_SPACING (16)
74
75 #define ROW0_TILESIZE (144)
76 #define ROW1_TILESIZE (64)
77 #define TILE_XSPACING (8)
78 #define TILE_YSPACING (16)
79
80 // Alignment values for PaintIcon()
81 #define ALIGN_RIGHT 1
82 #define ALIGN_LEFT 0
83
84 static EG_IMAGE *SelectionImages[4] = { NULL, NULL, NULL, NULL };
85 static EG_PIXEL SelectionBackgroundPixel = { 0xff, 0xff, 0xff, 0 };
86 static EG_IMAGE *TextBuffer = NULL;
87
88 // Used in MainMenuStyle(), but must be persistent....
89 UINTN row0PosX = 0, row0PosXRunning = 0, row1PosY = 0, row0Loaders = 0;
90
91 //
92 // Graphics helper functions
93 //
94
95 static VOID InitSelection(VOID)
96 {
97 UINTN x, y, src_x, src_y;
98 EG_PIXEL *DestPtr, *SrcPtr;
99
100 if (!AllowGraphicsMode)
101 return;
102 if (SelectionImages[0] != NULL)
103 return;
104
105 // load small selection image
106 if (GlobalConfig.SelectionSmallFileName != NULL) {
107 SelectionImages[2] = egLoadImage(SelfDir, GlobalConfig.SelectionSmallFileName, FALSE);
108 }
109 if (SelectionImages[2] == NULL)
110 SelectionImages[2] = egPrepareEmbeddedImage(&egemb_back_selected_small, FALSE);
111 SelectionImages[2] = egEnsureImageSize(SelectionImages[2],
112 ROW1_TILESIZE, ROW1_TILESIZE, &MenuBackgroundPixel);
113 if (SelectionImages[2] == NULL)
114 return;
115
116 // load big selection image
117 if (GlobalConfig.SelectionBigFileName != NULL) {
118 SelectionImages[0] = egLoadImage(SelfDir, GlobalConfig.SelectionBigFileName, FALSE);
119 SelectionImages[0] = egEnsureImageSize(SelectionImages[0],
120 ROW0_TILESIZE, ROW0_TILESIZE, &MenuBackgroundPixel);
121 }
122 if (SelectionImages[0] == NULL) {
123 // calculate big selection image from small one
124
125 SelectionImages[0] = egCreateImage(ROW0_TILESIZE, ROW0_TILESIZE, FALSE);
126 if (SelectionImages[0] == NULL) {
127 egFreeImage(SelectionImages[2]);
128 SelectionImages[2] = NULL;
129 return;
130 }
131
132 DestPtr = SelectionImages[0]->PixelData;
133 SrcPtr = SelectionImages[2]->PixelData;
134 for (y = 0; y < ROW0_TILESIZE; y++) {
135 if (y < (ROW1_TILESIZE >> 1))
136 src_y = y;
137 else if (y < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
138 src_y = (ROW1_TILESIZE >> 1);
139 else
140 src_y = y - (ROW0_TILESIZE - ROW1_TILESIZE);
141
142 for (x = 0; x < ROW0_TILESIZE; x++) {
143 if (x < (ROW1_TILESIZE >> 1))
144 src_x = x;
145 else if (x < (ROW0_TILESIZE - (ROW1_TILESIZE >> 1)))
146 src_x = (ROW1_TILESIZE >> 1);
147 else
148 src_x = x - (ROW0_TILESIZE - ROW1_TILESIZE);
149
150 *DestPtr++ = SrcPtr[src_y * ROW1_TILESIZE + src_x];
151 }
152 }
153 }
154
155 // non-selected background images
156 SelectionImages[1] = egCreateFilledImage(ROW0_TILESIZE, ROW0_TILESIZE, FALSE, &MenuBackgroundPixel);
157 SelectionImages[3] = egCreateFilledImage(ROW1_TILESIZE, ROW1_TILESIZE, FALSE, &MenuBackgroundPixel);
158 }
159
160 //
161 // Scrolling functions
162 //
163
164 static VOID InitScroll(OUT SCROLL_STATE *State, IN UINTN ItemCount, IN UINTN VisibleSpace)
165 {
166 State->LastSelection = State->CurrentSelection = 0;
167 State->MaxIndex = (INTN)ItemCount - 1;
168 State->FirstVisible = 0;
169 if (AllowGraphicsMode)
170 State->MaxVisible = UGAWidth / (ROW0_TILESIZE + TILE_XSPACING) - 1;
171 else
172 State->MaxVisible = ConHeight - 4;
173 if ((VisibleSpace > 0) && (VisibleSpace < State->MaxVisible))
174 State->MaxVisible = (INTN)VisibleSpace;
175 State->PaintAll = TRUE;
176 State->PaintSelection = FALSE;
177
178 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
179 }
180
181 // Adjust variables relating to the scrolling of tags, for when a selected icon isn't
182 // visible given the current scrolling condition....
183 static VOID AdjustScrollState(/* IN REFIT_MENU_SCREEN *Screen, */ IN SCROLL_STATE *State) {
184 if (State->CurrentSelection > State->LastVisible) {
185 State->LastVisible = State->CurrentSelection;
186 State->FirstVisible = 1 + State->CurrentSelection - State->MaxVisible;
187 if (State->FirstVisible < 0) // shouldn't happen, but just in case....
188 State->FirstVisible = 0;
189 State->PaintAll = TRUE;
190 } // Scroll forward
191 if (State->CurrentSelection < State->FirstVisible) {
192 State->FirstVisible = State->CurrentSelection;
193 State->LastVisible = State->CurrentSelection + State->MaxVisible - 1;
194 State->PaintAll = TRUE;
195 } // Scroll backward
196 } // static VOID AdjustScrollState
197
198 static VOID UpdateScroll(IN OUT SCROLL_STATE *State, IN UINTN Movement)
199 {
200 State->LastSelection = State->CurrentSelection;
201
202 switch (Movement) {
203 case SCROLL_LINE_UP:
204 if (State->CurrentSelection > 0) {
205 State->CurrentSelection --;
206 }
207 break;
208
209 case SCROLL_LINE_DOWN:
210 if (State->CurrentSelection < State->MaxIndex) {
211 State->CurrentSelection ++;
212 }
213 break;
214
215 // TODO: Better handling of SCROLL_PAGE_UP & SCROLL_PAGE_DOWN
216 case SCROLL_PAGE_UP:
217 case SCROLL_FIRST:
218 if (State->CurrentSelection > 0) {
219 State->PaintAll = TRUE;
220 State->CurrentSelection = 0;
221 }
222 break;
223
224 case SCROLL_PAGE_DOWN:
225 case SCROLL_LAST:
226 if (State->CurrentSelection < State->MaxIndex) {
227 State->PaintAll = TRUE;
228 State->CurrentSelection = State->MaxIndex;
229 }
230 break;
231
232 case SCROLL_NONE:
233 break;
234
235 }
236 if (!AllowGraphicsMode)
237 AdjustScrollState(State);
238
239 if (!State->PaintAll && State->CurrentSelection != State->LastSelection)
240 State->PaintSelection = TRUE;
241 State->LastVisible = State->FirstVisible + State->MaxVisible - 1;
242 }
243
244 //
245 // menu helper functions
246 //
247
248 VOID AddMenuInfoLine(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *InfoLine)
249 {
250 AddListElement((VOID ***) &(Screen->InfoLines), &(Screen->InfoLineCount), InfoLine);
251 }
252
253 VOID AddMenuEntry(IN REFIT_MENU_SCREEN *Screen, IN REFIT_MENU_ENTRY *Entry)
254 {
255 AddListElement((VOID ***) &(Screen->Entries), &(Screen->EntryCount), Entry);
256 }
257
258 VOID FreeMenu(IN REFIT_MENU_SCREEN *Screen)
259 {
260 if (Screen->Entries)
261 FreePool(Screen->Entries);
262 }
263
264 static INTN FindMenuShortcutEntry(IN REFIT_MENU_SCREEN *Screen, IN CHAR16 *Shortcut)
265 {
266 UINTN i;
267
268 if (Shortcut == NULL)
269 return (-1);
270
271 if (StrLen(Shortcut) == 1) {
272 if (Shortcut[0] >= 'a' && Shortcut[0] <= 'z')
273 Shortcut[0] -= ('a' - 'A');
274 if (Shortcut[0]) {
275 for (i = 0; i < Screen->EntryCount; i++) {
276 if (Screen->Entries[i]->ShortcutDigit == Shortcut[0] || Screen->Entries[i]->ShortcutLetter == Shortcut[0]) {
277 return i;
278 } // if
279 } // for
280 } // if
281 } else if (StrLen(Shortcut) > 1) {
282 for (i = 0; i < Screen->EntryCount; i++) {
283 if (StriSubCmp(Shortcut, Screen->Entries[i]->Title))
284 return i;
285 } // for
286 }
287 return -1;
288 }
289
290 //
291 // generic menu function
292 //
293
294 static UINTN RunGenericMenu(IN REFIT_MENU_SCREEN *Screen, IN MENU_STYLE_FUNC StyleFunc, IN INTN DefaultEntryIndex, OUT REFIT_MENU_ENTRY **ChosenEntry)
295 {
296 SCROLL_STATE State;
297 EFI_STATUS Status;
298 EFI_INPUT_KEY key;
299 UINTN index;
300 INTN ShortcutEntry;
301 BOOLEAN HaveTimeout = FALSE;
302 UINTN TimeoutCountdown = 0;
303 CHAR16 *TimeoutMessage;
304 CHAR16 KeyAsString[2];
305 UINTN MenuExit;
306
307 if (Screen->TimeoutSeconds > 0) {
308 HaveTimeout = TRUE;
309 TimeoutCountdown = Screen->TimeoutSeconds * 10;
310 }
311 MenuExit = 0;
312
313 StyleFunc(Screen, &State, MENU_FUNCTION_INIT, NULL);
314 // override the starting selection with the default index, if any
315 if (DefaultEntryIndex >= 0 && DefaultEntryIndex <= State.MaxIndex) {
316 State.CurrentSelection = DefaultEntryIndex;
317 UpdateScroll(&State, SCROLL_NONE);
318 }
319
320 while (!MenuExit) {
321 // update the screen
322 if (State.PaintAll) {
323 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_ALL, NULL);
324 State.PaintAll = FALSE;
325 } else if (State.PaintSelection) {
326 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_SELECTION, NULL);
327 State.PaintSelection = FALSE;
328 }
329
330 if (HaveTimeout) {
331 TimeoutMessage = PoolPrint(L"%s in %d seconds", Screen->TimeoutText, (TimeoutCountdown + 5) / 10);
332 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, TimeoutMessage);
333 FreePool(TimeoutMessage);
334 }
335
336 // read key press (and wait for it if applicable)
337 Status = refit_call2_wrapper(ST->ConIn->ReadKeyStroke, ST->ConIn, &key);
338 if (Status == EFI_NOT_READY) {
339 if (HaveTimeout && TimeoutCountdown == 0) {
340 // timeout expired
341 MenuExit = MENU_EXIT_TIMEOUT;
342 break;
343 } else if (HaveTimeout) {
344 refit_call1_wrapper(BS->Stall, 100000);
345 TimeoutCountdown--;
346 } else
347 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &index);
348 continue;
349 }
350 if (HaveTimeout) {
351 // the user pressed a key, cancel the timeout
352 StyleFunc(Screen, &State, MENU_FUNCTION_PAINT_TIMEOUT, L"");
353 HaveTimeout = FALSE;
354 }
355
356 // react to key press
357 switch (key.ScanCode) {
358 case SCAN_UP:
359 case SCAN_LEFT:
360 UpdateScroll(&State, SCROLL_LINE_UP);
361 break;
362 case SCAN_DOWN:
363 case SCAN_RIGHT:
364 UpdateScroll(&State, SCROLL_LINE_DOWN);
365 break;
366 case SCAN_HOME:
367 UpdateScroll(&State, SCROLL_FIRST);
368 break;
369 case SCAN_END:
370 UpdateScroll(&State, SCROLL_LAST);
371 break;
372 case SCAN_PAGE_UP:
373 UpdateScroll(&State, SCROLL_PAGE_UP);
374 break;
375 case SCAN_PAGE_DOWN:
376 UpdateScroll(&State, SCROLL_PAGE_DOWN);
377 break;
378 case SCAN_ESC:
379 MenuExit = MENU_EXIT_ESCAPE;
380 break;
381 case SCAN_INSERT:
382 case SCAN_F2:
383 MenuExit = MENU_EXIT_DETAILS;
384 break;
385 case SCAN_F10:
386 egScreenShot();
387 break;
388 }
389 switch (key.UnicodeChar) {
390 case CHAR_LINEFEED:
391 case CHAR_CARRIAGE_RETURN:
392 case ' ':
393 MenuExit = MENU_EXIT_ENTER;
394 break;
395 case '+':
396 MenuExit = MENU_EXIT_DETAILS;
397 break;
398 default:
399 KeyAsString[0] = key.UnicodeChar;
400 KeyAsString[1] = 0;
401 ShortcutEntry = FindMenuShortcutEntry(Screen, KeyAsString);
402 if (ShortcutEntry >= 0) {
403 State.CurrentSelection = ShortcutEntry;
404 MenuExit = MENU_EXIT_ENTER;
405 }
406 break;
407 }
408 }
409
410 StyleFunc(Screen, &State, MENU_FUNCTION_CLEANUP, NULL);
411
412 if (ChosenEntry)
413 *ChosenEntry = Screen->Entries[State.CurrentSelection];
414 return MenuExit;
415 } /* static UINTN RunGenericMenu( */
416
417 //
418 // text-mode generic style
419 //
420
421 static VOID TextMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
422 {
423 INTN i;
424 UINTN MenuWidth, ItemWidth, MenuHeight;
425 static UINTN MenuPosY;
426 static CHAR16 **DisplayStrings;
427 CHAR16 *TimeoutMessage;
428
429 switch (Function) {
430
431 case MENU_FUNCTION_INIT:
432 // vertical layout
433 MenuPosY = 4;
434 if (Screen->InfoLineCount > 0)
435 MenuPosY += Screen->InfoLineCount + 1;
436 MenuHeight = ConHeight - MenuPosY;
437 if (Screen->TimeoutSeconds > 0)
438 MenuHeight -= 2;
439 InitScroll(State, Screen->EntryCount, MenuHeight);
440
441 // determine width of the menu
442 MenuWidth = 20; // minimum
443 for (i = 0; i <= State->MaxIndex; i++) {
444 ItemWidth = StrLen(Screen->Entries[i]->Title);
445 if (MenuWidth < ItemWidth)
446 MenuWidth = ItemWidth;
447 }
448 if (MenuWidth > ConWidth - 6)
449 MenuWidth = ConWidth - 6;
450
451 // prepare strings for display
452 DisplayStrings = AllocatePool(sizeof(CHAR16 *) * Screen->EntryCount);
453 for (i = 0; i <= State->MaxIndex; i++)
454 DisplayStrings[i] = PoolPrint(L" %-.*s ", MenuWidth, Screen->Entries[i]->Title);
455 // TODO: shorten strings that are too long (PoolPrint doesn't do that...)
456 // TODO: use more elaborate techniques for shortening too long strings (ellipses in the middle)
457 // TODO: account for double-width characters
458
459 // initial painting
460 BeginTextScreen(Screen->Title);
461 if (Screen->InfoLineCount > 0) {
462 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
463 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
464 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, 4 + i);
465 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, Screen->InfoLines[i]);
466 }
467 }
468
469 break;
470
471 case MENU_FUNCTION_CLEANUP:
472 // release temporary memory
473 for (i = 0; i <= State->MaxIndex; i++)
474 FreePool(DisplayStrings[i]);
475 FreePool(DisplayStrings);
476 break;
477
478 case MENU_FUNCTION_PAINT_ALL:
479 // paint the whole screen (initially and after scrolling)
480 for (i = 0; i <= State->MaxIndex; i++) {
481 if (i >= State->FirstVisible && i <= State->LastVisible) {
482 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (i - State->FirstVisible));
483 if (i == State->CurrentSelection)
484 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
485 else
486 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
487 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[i]);
488 }
489 }
490 // scrolling indicators
491 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_SCROLLARROW);
492 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY);
493 if (State->FirstVisible > 0)
494 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowUp);
495 else
496 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
497 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, MenuPosY + State->MaxVisible);
498 if (State->LastVisible < State->MaxIndex)
499 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, ArrowDown);
500 else
501 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, L" ");
502 break;
503
504 case MENU_FUNCTION_PAINT_SELECTION:
505 // redraw selection cursor
506 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->LastSelection - State->FirstVisible));
507 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_BASIC);
508 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->LastSelection]);
509 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 2, MenuPosY + (State->CurrentSelection - State->FirstVisible));
510 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_CHOICE_CURRENT);
511 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, DisplayStrings[State->CurrentSelection]);
512 break;
513
514 case MENU_FUNCTION_PAINT_TIMEOUT:
515 if (ParamText[0] == 0) {
516 // clear message
517 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_BASIC);
518 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 0, ConHeight - 1);
519 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, BlankLine + 1);
520 } else {
521 // paint or update message
522 refit_call2_wrapper(ST->ConOut->SetAttribute, ST->ConOut, ATTR_ERROR);
523 refit_call3_wrapper(ST->ConOut->SetCursorPosition, ST->ConOut, 3, ConHeight - 1);
524 TimeoutMessage = PoolPrint(L"%s ", ParamText);
525 refit_call2_wrapper(ST->ConOut->OutputString, ST->ConOut, TimeoutMessage);
526 FreePool(TimeoutMessage);
527 }
528 break;
529
530 }
531 }
532
533 //
534 // graphical generic style
535 //
536
537
538 static VOID DrawMenuText(IN CHAR16 *Text, IN UINTN SelectedWidth, IN UINTN XPos, IN UINTN YPos)
539 {
540 // Print(L"Entering DrawMenuText(); Text is '%s', SelectedWidth is %d, XPos is %d, YPos is %d\n",
541 // Text, SelectedWidth, XPos, YPos);
542 if (TextBuffer == NULL)
543 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
544
545 egFillImage(TextBuffer, &MenuBackgroundPixel);
546 if (SelectedWidth > 0) {
547 // draw selection bar background
548 egFillImageArea(TextBuffer, 0, 0, SelectedWidth, TextBuffer->Height,
549 &SelectionBackgroundPixel);
550 }
551
552 // render the text
553 egRenderText(Text, TextBuffer, TEXT_XMARGIN, TEXT_YMARGIN);
554 BltImage(TextBuffer, XPos, YPos);
555 }
556
557 // Displays sub-menus
558 static VOID GraphicsMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
559 {
560 INTN i;
561 UINTN ItemWidth;
562 static UINTN MenuWidth, EntriesPosX, EntriesPosY, TimeoutPosY;
563
564 switch (Function) {
565
566 case MENU_FUNCTION_INIT:
567 InitScroll(State, Screen->EntryCount, 0);
568
569 // determine width of the menu
570 MenuWidth = 20; // minimum
571 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
572 ItemWidth = StrLen(Screen->InfoLines[i]);
573 if (MenuWidth < ItemWidth)
574 MenuWidth = ItemWidth;
575 }
576 for (i = 0; i <= State->MaxIndex; i++) {
577 ItemWidth = StrLen(Screen->Entries[i]->Title);
578 if (MenuWidth < ItemWidth)
579 MenuWidth = ItemWidth;
580 }
581 MenuWidth = TEXT_XMARGIN * 2 + MenuWidth * FONT_CELL_WIDTH;
582 if (MenuWidth > LAYOUT_TEXT_WIDTH)
583 MenuWidth = LAYOUT_TEXT_WIDTH;
584
585 if (Screen->TitleImage)
586 EntriesPosX = (UGAWidth + (Screen->TitleImage->Width + TITLEICON_SPACING) - MenuWidth) >> 1;
587 else
588 EntriesPosX = (UGAWidth - MenuWidth) >> 1;
589 EntriesPosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET + TEXT_LINE_HEIGHT * 2;
590 TimeoutPosY = EntriesPosY + (Screen->EntryCount + 1) * TEXT_LINE_HEIGHT;
591
592 // initial painting
593 SwitchToGraphicsAndClear();
594 egMeasureText(Screen->Title, &ItemWidth, NULL);
595 DrawMenuText(Screen->Title, 0, ((UGAWidth - ItemWidth) >> 1) - TEXT_XMARGIN, EntriesPosY - TEXT_LINE_HEIGHT * 2);
596 if (Screen->TitleImage)
597 BltImageAlpha(Screen->TitleImage,
598 EntriesPosX - (Screen->TitleImage->Width + TITLEICON_SPACING), EntriesPosY,
599 &MenuBackgroundPixel);
600 if (Screen->InfoLineCount > 0) {
601 for (i = 0; i < (INTN)Screen->InfoLineCount; i++) {
602 DrawMenuText(Screen->InfoLines[i], 0, EntriesPosX, EntriesPosY);
603 EntriesPosY += TEXT_LINE_HEIGHT;
604 }
605 EntriesPosY += TEXT_LINE_HEIGHT; // also add a blank line
606 }
607
608 break;
609
610 case MENU_FUNCTION_CLEANUP:
611 // nothing to do
612 break;
613
614 case MENU_FUNCTION_PAINT_ALL:
615 for (i = 0; i <= State->MaxIndex; i++) {
616 DrawMenuText(Screen->Entries[i]->Title, (i == State->CurrentSelection) ? MenuWidth : 0,
617 EntriesPosX, EntriesPosY + i * TEXT_LINE_HEIGHT);
618 }
619 break;
620
621 case MENU_FUNCTION_PAINT_SELECTION:
622 // redraw selection cursor
623 DrawMenuText(Screen->Entries[State->LastSelection]->Title, 0,
624 EntriesPosX, EntriesPosY + State->LastSelection * TEXT_LINE_HEIGHT);
625 DrawMenuText(Screen->Entries[State->CurrentSelection]->Title, MenuWidth,
626 EntriesPosX, EntriesPosY + State->CurrentSelection * TEXT_LINE_HEIGHT);
627 break;
628
629 case MENU_FUNCTION_PAINT_TIMEOUT:
630 DrawMenuText(ParamText, 0, EntriesPosX, TimeoutPosY);
631 break;
632
633 }
634 }
635
636 //
637 // graphical main menu style
638 //
639
640 static VOID DrawMainMenuEntry(REFIT_MENU_ENTRY *Entry, BOOLEAN selected, UINTN XPos, UINTN YPos)
641 {
642 UINTN ImageNum;
643
644 ImageNum = ((Entry->Row == 0) ? 0 : 2) + (selected ? 0 : 1);
645 if (SelectionImages != NULL)
646 BltImageCompositeBadge(SelectionImages[ImageNum],
647 Entry->Image, Entry->BadgeImage, XPos, YPos);
648 }
649
650 static VOID DrawMainMenuText(IN CHAR16 *Text, IN UINTN XPos, IN UINTN YPos)
651 {
652 UINTN TextWidth, TextPosX;
653
654 if (TextBuffer == NULL)
655 TextBuffer = egCreateImage(LAYOUT_TEXT_WIDTH, TEXT_LINE_HEIGHT, FALSE);
656
657 egFillImage(TextBuffer, &MenuBackgroundPixel);
658
659 // render the text
660 egMeasureText(Text, &TextWidth, NULL);
661 if (TextWidth > TextBuffer->Width)
662 TextPosX = 0;
663 else
664 TextPosX = (TextBuffer->Width - TextWidth) / 2;
665 egRenderText(Text, TextBuffer, TextPosX, 0);
666 // egRenderText(Text, TextBuffer, (TextBuffer->Width - TextWidth) >> 1, 0);
667 BltImage(TextBuffer, XPos, YPos);
668 }
669
670 static VOID PaintAll(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
671 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
672 INTN i;
673
674 if (Screen->Entries[State->CurrentSelection]->Row == 0)
675 AdjustScrollState(State);
676 for (i = State->FirstVisible; i <= State->MaxIndex; i++) {
677 if (Screen->Entries[i]->Row == 0) {
678 if (i <= State->LastVisible) {
679 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
680 itemPosX[i - State->FirstVisible], row0PosY);
681 } // if
682 } else {
683 DrawMainMenuEntry(Screen->Entries[i], (i == State->CurrentSelection) ? TRUE : FALSE,
684 itemPosX[i], row1PosY);
685 }
686 }
687 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
688 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
689 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
690 } // static VOID PaintAll()
691
692 // Move the selection to State->CurrentSelection, adjusting icon row if necessary...
693 static VOID PaintSelection(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, UINTN *itemPosX,
694 UINTN row0PosY, UINTN row1PosY, UINTN textPosY) {
695 if ((State->CurrentSelection < State->LastVisible) && (State->CurrentSelection >= State->FirstVisible)) {
696 DrawMainMenuEntry(Screen->Entries[State->LastSelection], FALSE,
697 itemPosX[State->LastSelection - State->FirstVisible],
698 (Screen->Entries[State->LastSelection]->Row == 0) ? row0PosY : row1PosY);
699 DrawMainMenuEntry(Screen->Entries[State->CurrentSelection], TRUE,
700 itemPosX[State->CurrentSelection - State->FirstVisible],
701 (Screen->Entries[State->CurrentSelection]->Row == 0) ? row0PosY : row1PosY);
702 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
703 DrawMainMenuText(Screen->Entries[State->CurrentSelection]->Title,
704 (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY);
705 } else {
706 MainMenuStyle(Screen, State, MENU_FUNCTION_PAINT_ALL, NULL);
707 }
708 } // static VOID MoveSelection(VOID)
709
710 // Display an icon at the specified location. Uses the image specified by
711 // ExternalFilename if it's available, or BuiltInImage if it's not. The
712 // Y position is specified as the center value, and so is adjusted by half
713 // the icon's height. The X position is set along the icon's left
714 // edge if Alignment == ALIGN_LEFT, and along the right edge if
715 // Alignment == ALIGN_RIGHT
716 static VOID PaintIcon(IN EG_EMBEDDED_IMAGE *BuiltInIcon, IN CHAR16 *ExternalFilename, UINTN PosX, UINTN PosY, UINTN Alignment) {
717 EG_IMAGE *Icon = NULL;
718
719 if (FileExists(SelfDir, ExternalFilename))
720 Icon = egLoadIcon(SelfDir, ExternalFilename, 48);
721 if (Icon == NULL)
722 Icon = egPrepareEmbeddedImage(BuiltInIcon, TRUE);
723 if (Icon != NULL) {
724 if (Alignment == ALIGN_RIGHT)
725 PosX -= Icon->Width;
726 BltImageAlpha(Icon, PosX, PosY - (Icon->Height / 2), &MenuBackgroundPixel);
727 }
728 } // static VOID PaintArrow()
729
730 // Display main menu in graphics mode
731 VOID MainMenuStyle(IN REFIT_MENU_SCREEN *Screen, IN SCROLL_STATE *State, IN UINTN Function, IN CHAR16 *ParamText)
732 {
733 INTN i;
734 extern UINTN row0PosX, row0PosXRunning, row1PosY, row0Loaders;
735 UINTN row0Count, row1Count, row1PosX, row1PosXRunning;
736 static UINTN *itemPosX;
737 static UINTN row0PosY, textPosY;
738
739 switch (Function) {
740
741 case MENU_FUNCTION_INIT:
742 InitScroll(State, Screen->EntryCount, GlobalConfig.MaxTags);
743
744 // layout
745 row0Count = 0;
746 row1Count = 0;
747 row0Loaders = 0;
748 for (i = 0; i <= State->MaxIndex; i++) {
749 if (Screen->Entries[i]->Row == 1) {
750 row1Count++;
751 } else {
752 row0Loaders++;
753 if (row0Count < State->MaxVisible)
754 row0Count++;
755 }
756 }
757 row0PosX = (UGAWidth + TILE_XSPACING - (ROW0_TILESIZE + TILE_XSPACING) * row0Count) >> 1;
758 row0PosY = ((UGAHeight - LAYOUT_TOTAL_HEIGHT) >> 1) + LAYOUT_BANNER_YOFFSET;
759 row1PosX = (UGAWidth + TILE_XSPACING - (ROW1_TILESIZE + TILE_XSPACING) * row1Count) >> 1;
760 row1PosY = row0PosY + ROW0_TILESIZE + TILE_YSPACING;
761 if (row1Count > 0)
762 textPosY = row1PosY + ROW1_TILESIZE + TILE_YSPACING;
763 else
764 textPosY = row1PosY;
765
766 itemPosX = AllocatePool(sizeof(UINTN) * Screen->EntryCount);
767 row0PosXRunning = row0PosX;
768 row1PosXRunning = row1PosX;
769 for (i = 0; i <= State->MaxIndex; i++) {
770 if (Screen->Entries[i]->Row == 0) {
771 itemPosX[i] = row0PosXRunning;
772 row0PosXRunning += ROW0_TILESIZE + TILE_XSPACING;
773 } else {
774 itemPosX[i] = row1PosXRunning;
775 row1PosXRunning += ROW1_TILESIZE + TILE_XSPACING;
776 }
777 }
778 // initial painting
779 InitSelection();
780 SwitchToGraphicsAndClear();
781 break;
782
783 case MENU_FUNCTION_CLEANUP:
784 FreePool(itemPosX);
785 break;
786
787 case MENU_FUNCTION_PAINT_ALL:
788 BltClearScreen(TRUE);
789 PaintAll(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
790 // For PaintIcon() calls, the starting Y position is moved to the midpoint
791 // of the surrounding row; PaintIcon() adjusts this back up by half the
792 // icon's height to properly center it.
793 if ((State->FirstVisible > 0) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS)))
794 PaintIcon(&egemb_arrow_left, L"icons\\arrow_left.icns", row0PosX - TILE_XSPACING,
795 row0PosY + (ROW0_TILESIZE / 2), ALIGN_RIGHT);
796 if ((State->LastVisible < (row0Loaders - 1)) && (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_ARROWS)))
797 PaintIcon(&egemb_arrow_right, L"icons\\arrow_right.icns",
798 (UGAWidth + (ROW0_TILESIZE + TILE_XSPACING) * State->MaxVisible) / 2 + TILE_XSPACING,
799 row0PosY + (ROW0_TILESIZE / 2), ALIGN_LEFT);
800 break;
801
802 case MENU_FUNCTION_PAINT_SELECTION:
803 PaintSelection(Screen, State, itemPosX, row0PosY, row1PosY, textPosY);
804 break;
805
806 case MENU_FUNCTION_PAINT_TIMEOUT:
807 if (!(GlobalConfig.HideUIFlags & HIDEUI_FLAG_LABEL))
808 DrawMainMenuText(ParamText, (UGAWidth - LAYOUT_TEXT_WIDTH) >> 1, textPosY + TEXT_LINE_HEIGHT);
809 break;
810
811 }
812 }
813
814 //
815 // user-callable dispatcher functions
816 //
817
818 UINTN RunMenu(IN REFIT_MENU_SCREEN *Screen, OUT REFIT_MENU_ENTRY **ChosenEntry)
819 {
820 MENU_STYLE_FUNC Style = TextMenuStyle;
821
822 if (AllowGraphicsMode)
823 Style = GraphicsMenuStyle;
824
825 return RunGenericMenu(Screen, Style, -1, ChosenEntry);
826 }
827
828 UINTN RunMainMenu(IN REFIT_MENU_SCREEN *Screen, IN CHAR16* DefaultSelection, OUT REFIT_MENU_ENTRY **ChosenEntry)
829 {
830 MENU_STYLE_FUNC Style = TextMenuStyle;
831 MENU_STYLE_FUNC MainStyle = TextMenuStyle;
832 REFIT_MENU_ENTRY *TempChosenEntry;
833 UINTN MenuExit = 0;
834 UINTN DefaultEntryIndex = -1;
835
836 if (DefaultSelection != NULL) {
837 // Find a menu entry that includes *DefaultSelection as a substring
838 DefaultEntryIndex = FindMenuShortcutEntry(Screen, DefaultSelection);
839 // If that didn't work, should we scan more characters? For now, no.
840 }
841
842 if (AllowGraphicsMode) {
843 Style = GraphicsMenuStyle;
844 MainStyle = MainMenuStyle;
845 }
846
847 while (!MenuExit) {
848 MenuExit = RunGenericMenu(Screen, MainStyle, DefaultEntryIndex, &TempChosenEntry);
849 Screen->TimeoutSeconds = 0;
850
851 if (MenuExit == MENU_EXIT_DETAILS && TempChosenEntry->SubScreen != NULL) {
852 MenuExit = RunGenericMenu(TempChosenEntry->SubScreen, Style, -1, &TempChosenEntry);
853 if (MenuExit == MENU_EXIT_ESCAPE || TempChosenEntry->Tag == TAG_RETURN)
854 MenuExit = 0;
855 }
856 }
857
858 if (ChosenEntry)
859 *ChosenEntry = TempChosenEntry;
860 return MenuExit;
861 } /* UINTN RunMainMenu() */