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