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