Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
nbgl_layout_nanos.c
Go to the documentation of this file.
1
5
6#ifndef HAVE_SE_TOUCH
7/*********************
8 * INCLUDES
9 *********************/
10#include <string.h>
11#include <stdlib.h>
12#include "nbgl_debug.h"
13#include "nbgl_front.h"
15#include "nbgl_obj.h"
16#include "nbgl_draw.h"
17#include "nbgl_screen.h"
18#include "nbgl_touch.h"
19#include "glyphs.h"
20#include "os_pic.h"
21#include "os_helpers.h"
22#include "lcx_rng.h"
23
24/*********************
25 * DEFINES
26 *********************/
27
28#define NB_MAX_LAYOUTS 3
29
30// used by screen
31#define NB_MAX_SCREEN_CHILDREN 7
32
33#define ELLIPSIS "..."
34#define ELLIPSIS_SIZE sizeof(ELLIPSIS) // includes '\0'
35
36/**********************
37 * MACROS
38 **********************/
39
40/**********************
41 * TYPEDEFS
42 **********************/
43
44/**********************
45 * VARIABLES
46 **********************/
47
53
54/**********************
55 * STATIC PROTOTYPES
56 **********************/
57
58static void buttonCallback(nbgl_screen_t *screen, nbgl_buttonEvent_t buttonEvent)
59{
60 uint8_t i = NB_MAX_LAYOUTS;
61 nbgl_layoutInternal_t *layout = NULL;
62
63 // parse all layouts (starting with modals) to find the object
64 while (i > 0) {
65 i--;
66 if ((screen->index == gLayout[i].layer) && (gLayout[i].nbChildren > 0)) {
67 // found
68 layout = &gLayout[i];
69 break;
70 }
71 }
72 if (layout == NULL) {
75 "touchCallback(): screen->index = %d, buttonEvent = %d, no matching active layout\n",
76 screen->index,
77 buttonEvent);
78 return;
79 }
80
81#ifdef NBGL_KEYPAD
82 // special case of keypad
84 if (kpd) {
85 nbgl_keypadCallback(kpd, buttonEvent);
86 return;
87 }
88 else
89#endif // NBGL_KEYPAD
90#ifdef NBGL_KEYBOARD
91 {
93 if (kbd) {
94 nbgl_keyboardCallback(kbd, buttonEvent);
95 return;
96 }
97 }
98#endif // NBGL_KEYBOARD
99 if (layout->callback != NULL) {
100 layout->callback((nbgl_layout_t *) layout, buttonEvent);
101 }
102}
103
104/**********************
105 * GLOBAL FUNCTIONS
106 **********************/
107
115{
116 if (layout->nbChildren >= NB_MAX_SCREEN_CHILDREN) {
117 LOG_FATAL(LAYOUT_LOGGER, "layoutAddObject(): No more object\n");
118 // LOG_FATAL is a no-op in release builds: never write past the array
119 return;
120 }
121 layout->children[layout->nbChildren] = obj;
122 layout->nbChildren++;
123}
124
135static void getButtonTextLen(const char *text,
136 uint16_t maxWidth,
137 uint16_t maxLen,
138 uint16_t *len,
139 uint16_t *width)
140{
142 BAGL_FONT_OPEN_SANS_EXTRABOLD_11px_1bpp, text, maxWidth, len, width, true);
143 // a button cannot contain more than the buffer used to display it
144 if (*len > maxLen) {
145 *len = maxLen;
146 // don't cut in the middle of an UTF-8 char
147 while ((*len > 0) && ((text[*len] & 0xC0) == 0x80)) {
148 (*len)--;
149 }
150 *width
152 }
153}
154
167 nbgl_container_t *container,
168 nbgl_text_area_t *textArea,
169 uint8_t nbLines,
170 uint16_t areaHeight)
171{
172 const char *text = textArea->text;
173 const uint16_t lineHeight = nbgl_getFontLineHeight(textArea->fontId);
174 const uint16_t ellipsisWidth = nbgl_getTextWidth(textArea->fontId, ELLIPSIS);
175 nbgl_text_area_t *lastLineArea;
176 nbgl_text_area_t *ellipsisArea;
177 uint16_t firstLinesLen = 0;
178 uint16_t lastLineStart = 0;
179 uint16_t lastLineLen = 0;
180 uint16_t lastLineWidth = 0;
181
182 // all lines but the last one are fully displayed by the given text area (empty if a single
183 // line), vertically centered with the last line in the area
184 if (nbLines > 1) {
186 textArea->fontId, text, AVAILABLE_WIDTH, nbLines - 1, &firstLinesLen, true);
187 // the '\n' ending these lines is not displayed
188 lastLineStart = (text[firstLinesLen] == '\n') ? (firstLinesLen + 1) : firstLinesLen;
189 }
190 // a len of 0 would mean the whole text, so nothing to display is a NULL text
191 if (firstLinesLen == 0) {
192 textArea->text = NULL;
193 }
194 textArea->len = firstLinesLen;
195 textArea->nbMaxLines = nbLines - 1;
196 textArea->obj.area.height = (nbLines - 1) * lineHeight;
197 textArea->obj.alignmentMarginY += (areaHeight - nbLines * lineHeight) / 2;
198
199 // the last line is displayed by another text area, under the first lines
200 lastLineArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
201 lastLineArea->textColor = WHITE;
202 lastLineArea->fontId = textArea->fontId;
203 lastLineArea->obj.alignTo = (nbgl_obj_t *) textArea;
204 container->children[2] = (nbgl_obj_t *) lastLineArea;
205 // the last line must leave room for the ellipsis
206 nbgl_getTextMaxLenAndWidth(textArea->fontId,
207 &text[lastLineStart],
208 AVAILABLE_WIDTH - ellipsisWidth,
209 &lastLineLen,
210 &lastLineWidth,
211 true);
212 // the last line may be empty (if cut at an empty line), then only the ellipsis is displayed
213 lastLineArea->text = (lastLineLen > 0) ? &text[lastLineStart] : NULL;
214 lastLineArea->len = lastLineLen;
215 lastLineArea->nbMaxLines = 1;
216 lastLineArea->wrapping = false;
217 lastLineArea->textAlignment = CENTER;
218 lastLineArea->obj.area.width = lastLineWidth;
219 lastLineArea->obj.area.height = lineHeight;
220 lastLineArea->obj.alignment = BOTTOM_MIDDLE;
221 // the last line and the ellipsis are horizontally centered together
222 lastLineArea->obj.alignmentMarginX = -(ellipsisWidth / 2);
223
224 // the ellipsis is on the right of the last line
225 ellipsisArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
226 ellipsisArea->textColor = WHITE;
227 ellipsisArea->text = (const char *) PIC(ELLIPSIS);
228 ellipsisArea->fontId = textArea->fontId;
229 ellipsisArea->textAlignment = CENTER;
230 ellipsisArea->obj.area.width = ellipsisWidth;
231 ellipsisArea->obj.area.height = lineHeight;
232 ellipsisArea->obj.alignment = MID_RIGHT;
233 ellipsisArea->obj.alignTo = (nbgl_obj_t *) lastLineArea;
234 container->children[3] = (nbgl_obj_t *) ellipsisArea;
235}
236
246uint16_t layoutGetButtonRowLen(const char *text, uint16_t *len, uint16_t *width)
247{
248 uint16_t consumed;
249
250 // the rest of the text goes in the next button
253 consumed = *len;
254 // a '\n' ending the button is consumed but not displayed
255 if (text[consumed] == '\n') {
256 consumed++;
257 }
258 // always consume at least one char, to be sure to progress in the text
259 if ((consumed == 0) && (text[0] != '\0')) {
260 consumed = 1;
261 }
262 return consumed;
263}
264
272{
273 nbgl_layoutInternal_t *layout = NULL;
274
275 // find an empty layout in the proper "layer"
276 if (description->modal) {
277 if (gLayout[1].nbChildren == 0) {
278 layout = &gLayout[1];
279 }
280 else if (gLayout[2].nbChildren == 0) {
281 layout = &gLayout[2];
282 }
283 }
284 else {
285 // automatically "release" a potentially opened non-modal layout
286 gLayout[0].nbChildren = 0;
287 layout = &gLayout[0];
288 }
289 if (layout == NULL) {
290 LOG_WARN(LAYOUT_LOGGER, "nbgl_layoutGet(): impossible to get a layout!\n");
291 return NULL;
292 }
293
294 // reset globals
295 memset(layout, 0, sizeof(nbgl_layoutInternal_t));
296
297 layout->callback = (nbgl_layoutButtonCallback_t) PIC(description->onActionCallback);
298 layout->modal = description->modal;
299 if (description->modal) {
300 layout->layer = nbgl_screenPush(&layout->children,
302 &description->ticker,
304 }
305 else {
306 nbgl_screenSet(&layout->children,
308 &description->ticker,
310 layout->layer = 0;
311 }
312
313 return (nbgl_layout_t *) layout;
314}
315
324{
325 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
326
327 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddNavigation():\n");
328 if (layout == NULL) {
329 return -1;
330 }
331
332 nbgl_image_t *image;
333 if (info->indication & LEFT_ARROW) {
334 image = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer);
335 image->foregroundColor = WHITE;
336 image->buffer = (info->direction == HORIZONTAL_NAV) ? &C_icon_left : &C_icon_up;
337 image->obj.area.bpp = NBGL_BPP_1;
338 image->obj.alignment = MID_LEFT;
339 layoutAddObject(layoutInt, (nbgl_obj_t *) image);
340 }
341 if (info->indication & RIGHT_ARROW) {
342 image = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer);
343 image->foregroundColor = WHITE;
344 image->buffer = (info->direction == HORIZONTAL_NAV) ? &C_icon_right : &C_icon_down;
345 image->obj.area.bpp = NBGL_BPP_1;
346 image->obj.alignment = MID_RIGHT;
347 layoutAddObject(layoutInt, (nbgl_obj_t *) image);
348 }
349 return 0;
350}
351
363 const char *text,
364 const char *subText,
366{
367 // In BUTTON_INFO style, the sub text is displayed in at most 2 buttons, the last one ending
368 // with an ellipsis if it doesn't fit (the 3rd button is only used by steps, to split long texts
369 // in pages). Other sub texts are simply cut.
370 if (style == BUTTON_INFO) {
371 return layoutAddText(layout, text, subText, style, NB_MAX_BUTTON_ROWS_PUBLIC, true);
372 }
373 return layoutAddText(layout, text, subText, style, 0, false);
374}
375
393 const char *text,
394 const char *subText,
396 uint8_t nbMaxLines,
397 bool ellipsis)
398{
399 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
400 nbgl_container_t *container;
401 nbgl_text_area_t *textArea;
402 uint16_t fullHeight = 0;
403 // max number of buttons in BUTTON_INFO style, or of lines in other styles, for the sub text
404 uint8_t subTextMaxLines = 0;
405 uint16_t subTextNbLines = 0;
406
407 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddText():\n");
408 if (layout == NULL) {
409 return -1;
410 }
411 container = (nbgl_container_t *) nbgl_objPoolGet(CONTAINER, layoutInt->layer);
412
413 // get container children: main text, then sub text
414 container->nbChildren = 1;
415 if (subText != NULL) {
416 if (style == BUTTON_INFO) {
417 subTextMaxLines = ((nbMaxLines > 0) && (nbMaxLines < NB_MAX_BUTTON_ROWS))
418 ? nbMaxLines
420 // possibly subTextMaxLines buttons
421 container->nbChildren += subTextMaxLines;
422 }
423 else {
424 // at most NB_MAX_LINES-1 lines are available on screen, under the main text
425 subTextMaxLines = ((nbMaxLines > 0) && (nbMaxLines < (NB_MAX_LINES - 1)))
426 ? nbMaxLines
427 : (NB_MAX_LINES - 1);
428 subTextNbLines = nbgl_getTextNbLinesInWidth(
430 // a text area, plus 2 others (last line and ellipsis) if the sub text is cut with an
431 // ellipsis
432 container->nbChildren += ((subTextNbLines > subTextMaxLines) && ellipsis) ? 3 : 1;
433 }
434 }
435
436 container->children = nbgl_containerPoolGet(container->nbChildren, layoutInt->layer);
437 container->obj.area.width = AVAILABLE_WIDTH;
438
439 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
440 textArea->textColor = WHITE;
441 textArea->text = PIC(text);
442 textArea->textAlignment = CENTER;
443 textArea->fontId = (style == REGULAR_INFO) ? BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp
445 textArea->obj.area.width = AVAILABLE_WIDTH;
446
447 uint16_t nbLines
448 = nbgl_getTextNbLinesInWidth(textArea->fontId, textArea->text, AVAILABLE_WIDTH, true);
449 // if more than available lines on screen
450 if (nbLines > NB_MAX_LINES) {
451 uint16_t len;
452
453 nbLines = NB_MAX_LINES;
454 textArea->nbMaxLines = NB_MAX_LINES;
456 textArea->fontId, textArea->text, AVAILABLE_WIDTH, nbLines, &len, true);
457 textArea->len = len;
458 }
459 const nbgl_font_t *font = nbgl_getFont(textArea->fontId);
460 textArea->obj.area.height = nbLines * font->line_height;
461 textArea->wrapping = true;
462 textArea->obj.alignment = TOP_MIDDLE;
463 fullHeight += textArea->obj.area.height;
464 container->children[0] = (nbgl_obj_t *) textArea;
465
466 if (subText != NULL) {
467 if (style != BUTTON_INFO) {
468 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
469 textArea->textColor = WHITE;
470 textArea->text = PIC(subText);
471 textArea->wrapping = true;
472 textArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
473 textArea->obj.area.width = AVAILABLE_WIDTH;
474 nbLines = subTextNbLines;
475 // if more than available lines
476 bool truncated = (nbLines > subTextMaxLines);
477 if (truncated) {
478 uint16_t len;
479 nbLines = subTextMaxLines;
480 textArea->nbMaxLines = nbLines;
482 textArea->fontId, textArea->text, AVAILABLE_WIDTH, nbLines, &len, true);
483 textArea->len = len;
484 }
485 if (style == REGULAR_INFO) {
486 textArea->obj.area.height = nbLines * font->line_height;
487 }
488 else {
489 // the sub text must be vertically centered in a 3 lines container
490 textArea->obj.area.height = 3 * font->line_height;
491 }
492 textArea->textAlignment = CENTER;
493 textArea->obj.alignment = NO_ALIGNMENT;
494 textArea->obj.alignmentMarginY = 2;
495 fullHeight += textArea->obj.area.height + textArea->obj.alignmentMarginY;
496 container->children[1] = (nbgl_obj_t *) textArea;
497 if (truncated && ellipsis) {
498 // the end of the last line is replaced by an ellipsis
500 layoutInt, container, textArea, nbLines, textArea->obj.area.height);
501 }
502 }
503 else {
504 // vertical margin of each button, depending on the number of buttons
505 static const int16_t marginsY[NB_MAX_BUTTON_ROWS][NB_MAX_BUTTON_ROWS] = {
506 {8},
507 {8 - 7, 8 + 7},
508 {-8, 6, 20}
509 };
510 // the texts of the buttons are copied in the layout, so that they stay valid as long
511 // as the layout is displayed, whatever the given subText becomes
512 char(*buttonTexts)[NB_MAX_CHAR_IN_BUTTON_ROW] = layoutInt->buttonTexts;
514 const char *rowText = (const char *) PIC(subText);
515 uint8_t nbRows = 0;
516 uint8_t maxRows = subTextMaxLines;
517
518 // draw buttons under each other, each with the text remaining from the previous one
519 while ((rowText != NULL) && (nbRows < maxRows)) {
520 nbgl_button_t *button = (nbgl_button_t *) nbgl_objPoolGet(BUTTON, layoutInt->layer);
521 uint16_t len = 0;
522 uint16_t width = 0;
523 uint16_t consumed;
524
525 button->foregroundColor = BLACK;
526 button->innerColor = WHITE;
527 button->borderColor = WHITE;
528 button->radius = RADIUS_3_PIXELS;
529 button->text = rowText;
531 button->obj.area.height = 14;
532 button->obj.alignment = CENTER;
533
534 consumed = layoutGetButtonRowLen(rowText, &len, &width);
535 if (ellipsis && (rowText[consumed] != '\0') && (nbRows == (maxRows - 1))) {
536 // text remaining after the last button: end it with an ellipsis
537 const uint16_t ellipsisWidth = nbgl_getTextWidth(button->fontId, ELLIPSIS);
538 getButtonTextLen(rowText,
539 AVAILABLE_WIDTH - BUTTON_MARGIN_Y - ellipsisWidth,
541 &len,
542 &width);
543 button->obj.area.width = width + ellipsisWidth + BUTTON_MARGIN_Y;
544 // copy the first 'len' chars then append ELLIPSIS (ELLIPSIS_SIZE includes '\0')
545 // (len is already limited by getButtonTextLen(), MIN() makes it explicit)
546 uint16_t copyLen = MIN(len, (NB_MAX_CHAR_IN_BUTTON_ROW - ELLIPSIS_SIZE));
547 memcpy(buttonTexts[nbRows], rowText, copyLen);
548 memcpy(buttonTexts[nbRows] + copyLen, ELLIPSIS, ELLIPSIS_SIZE);
549 button->text = PIC(buttonTexts[nbRows]);
550 rowText = NULL;
551 }
552 else {
553 button->obj.area.width = width + BUTTON_MARGIN_Y;
554 // copy the 'len' chars displayed in this button
555 // (len is already limited by layoutGetButtonRowLen(), MIN() makes it explicit)
556 uint16_t copyLen = MIN(len, (NB_MAX_CHAR_IN_BUTTON_ROW - 1));
557 memcpy(buttonTexts[nbRows], rowText, copyLen);
558 // NULL termination
559 buttonTexts[nbRows][copyLen] = '\0';
560 button->text = PIC(buttonTexts[nbRows]);
561 // the remaining text goes in the next button
562 rowText = (rowText[consumed] != '\0') ? (rowText + consumed) : NULL;
563 }
564 buttons[nbRows] = button;
565 container->children[1 + nbRows] = (nbgl_obj_t *) button;
566 nbRows++;
567 }
568 for (uint8_t i = 0; i < nbRows; i++) {
569 buttons[i]->obj.alignmentMarginY = marginsY[nbRows - 1][i];
570 }
571
572 fullHeight += (nbRows == NB_MAX_BUTTON_ROWS) ? 48 : 44;
573 }
574 }
575 container->obj.area.height = fullHeight;
576 container->layout = VERTICAL;
577 container->obj.alignment = CENTER;
578 // set this new obj as child of main container
579 layoutAddObject(layoutInt, (nbgl_obj_t *) container);
580
581 return 0;
582}
583
593{
594 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
595 uint8_t i;
596
597 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddMenuList():\n");
598 if (layout == NULL) {
599 return -1;
600 }
601 for (i = 0; i < list->nbChoices; i++) {
602 nbgl_text_area_t *textArea;
603
604 // check whether this object is visible or not
605 // only the two objects above or below the selected one are visible
606 if (((list->selectedChoice > 2) && (i < (list->selectedChoice - 2)))
607 || (i > (list->selectedChoice + 2))) {
608 continue;
609 }
610
611 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
612
613 // init text area for this choice
614 textArea->text = list->callback(i);
615 textArea->textAlignment = CENTER;
616 textArea->obj.area.width = AVAILABLE_WIDTH;
617 textArea->obj.area.height = 16;
618 textArea->style = NO_STYLE;
619 textArea->obj.alignment = CENTER;
620 textArea->obj.alignmentMarginY = ((i - list->selectedChoice) * 16);
621 textArea->textColor = WHITE;
622
623 // highlight init choice
624 if (i == list->selectedChoice) {
626 }
627 else {
628 textArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
629 }
630
631 // set this new obj as child of main container
632 layoutAddObject(layoutInt, (nbgl_obj_t *) textArea);
633 }
634
635 return 0;
636}
637
647{
648 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
649 nbgl_container_t *container;
650 nbgl_text_area_t *textArea = NULL;
651 nbgl_image_t *image = NULL;
652 uint16_t fullHeight = 0;
653
654 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddCenteredInfo():\n");
655 if (layout == NULL) {
656 return -1;
657 }
658
659 // When the caller asks for the icon to be at the screen bottom (and a
660 // primary text is provided), render the texts via nbgl_layoutAddText so
661 // the exact same geometry is used as on non-bottomIcon pages (sibling
662 // CHOICES_LIST entries reach the layout through nbgl_stepDrawText, which
663 // also calls nbgl_layoutAddText). The icon is then appended as a
664 // standalone BOTTOM_MIDDLE element. Falling back to the regular path
665 // when text1 is NULL keeps nbgl_layoutAddText's "main text mandatory"
666 // contract.
667 // Note: nbgl_layoutAddText always centers its container, so this branch
668 // ignores info->onTop — the combination bottomIcon=true && onTop=true is
669 // documented as incompatible on nbgl_contentCenteredInfo_t::bottomIcon.
670 if ((info->icon != NULL) && info->bottomIcon && (info->text1 != NULL)) {
671 // bottomIcon pages put the meaningful content in text1 (the icon is
672 // only a contextual hint at the bottom — selection marker, sub-menu
673 // chevron, ...). Force BOLD_TEXT1_INFO when only text1 is set so the
674 // primary content is visually prominent; when text2 is also set, keep
675 // the caller's style (BOLD_TEXT1_INFO is already implied by the
676 // standard drawStep style selection in that case).
678 if ((style == REGULAR_INFO) && (info->text2 == NULL)) {
679 style = BOLD_TEXT1_INFO;
680 }
681 nbgl_layoutAddText(layout, info->text1, info->text2, style);
682 image = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer);
683 image->foregroundColor = WHITE;
684 image->buffer = PIC(info->icon);
685 image->obj.area.bpp = NBGL_BPP_1;
686 image->obj.alignment = BOTTOM_MIDDLE;
687 image->obj.alignTo = NULL;
688 layoutAddObject(layoutInt, (nbgl_obj_t *) image);
689 return 0;
690 }
691
692 container = (nbgl_container_t *) nbgl_objPoolGet(CONTAINER, layoutInt->layer);
693
694 // 3 children at max
695 container->children = nbgl_containerPoolGet(3, layoutInt->layer);
696 container->nbChildren = 0;
697 if (info->icon != NULL) {
698 image = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer);
699 image->foregroundColor = WHITE;
700 image->buffer = PIC(info->icon);
701 image->obj.area.bpp = NBGL_BPP_1;
702 image->obj.alignment = TOP_MIDDLE;
703 image->obj.alignTo = NULL;
704
705 fullHeight += image->buffer->height;
706 container->children[container->nbChildren] = (nbgl_obj_t *) image;
707 container->nbChildren++;
708 }
709 if (info->text1 != NULL) {
710 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
711 textArea->textColor = WHITE;
712 textArea->text = PIC(info->text1);
713 textArea->textAlignment = CENTER;
714 textArea->fontId = (info->style == REGULAR_INFO) ? BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp
716 textArea->obj.area.width = AVAILABLE_WIDTH;
717 textArea->wrapping = true;
718 uint16_t nbLines
719 = nbgl_getTextNbLinesInWidth(textArea->fontId, textArea->text, AVAILABLE_WIDTH, true);
720 // if more than available lines on screen
721 if (nbLines > NB_MAX_LINES) {
722 uint16_t len;
723 nbLines = NB_MAX_LINES;
724 textArea->nbMaxLines = NB_MAX_LINES;
726 textArea->fontId, textArea->text, AVAILABLE_WIDTH, nbLines, &len, true);
727 textArea->len = len;
728 }
729 const nbgl_font_t *font = nbgl_getFont(textArea->fontId);
730 textArea->obj.area.height = nbLines * font->line_height;
731 textArea->style = NO_STYLE;
732 if (info->icon != NULL) {
733 textArea->obj.alignment = BOTTOM_MIDDLE; // under icon
734 textArea->obj.alignTo = (nbgl_obj_t *) container->children[container->nbChildren - 1];
735 textArea->obj.alignmentMarginY = (nbLines < 3) ? 4 : 0;
736 }
737 else if (info->text2 == NULL) {
738 textArea->obj.alignment = CENTER;
739 textArea->obj.alignTo = NULL;
740 }
741 else {
742 textArea->obj.alignment = TOP_MIDDLE;
743 textArea->obj.alignTo = NULL;
744 }
745
746 fullHeight += textArea->obj.area.height + textArea->obj.alignmentMarginY;
747
748 container->children[container->nbChildren] = (nbgl_obj_t *) textArea;
749 container->nbChildren++;
750 }
751 if (info->text2 != NULL) {
752 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
753 textArea->textColor = WHITE;
754 textArea->text = PIC(info->text2);
755 textArea->textAlignment = CENTER;
756 textArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
757 textArea->obj.area.width = AVAILABLE_WIDTH;
758 textArea->wrapping = true;
759 uint16_t nbLines
760 = nbgl_getTextNbLinesInWidth(textArea->fontId, textArea->text, AVAILABLE_WIDTH, true);
761 // if more than available lines on screen
762 if (nbLines > (NB_MAX_LINES - 1)) {
763 uint16_t len;
764 nbLines = NB_MAX_LINES - 1;
765 textArea->nbMaxLines = nbLines;
767 textArea->fontId, textArea->text, AVAILABLE_WIDTH, nbLines, &len, true);
768 textArea->len = len;
769 }
770 const nbgl_font_t *font = nbgl_getFont(textArea->fontId);
771 textArea->obj.area.height = nbLines * font->line_height;
772
773 textArea->style = NO_STYLE;
774 textArea->obj.alignment = BOTTOM_MIDDLE;
775 textArea->obj.alignTo = (nbgl_obj_t *) container->children[container->nbChildren - 1];
776 textArea->obj.alignmentMarginY = 2;
777
778 fullHeight += textArea->obj.area.height + textArea->obj.alignmentMarginY;
779
780 container->children[container->nbChildren] = (nbgl_obj_t *) textArea;
781 container->nbChildren++;
782 }
783 container->obj.area.height = fullHeight;
784 container->layout = VERTICAL;
785 container->obj.alignmentMarginY = 0;
786 if (info->onTop) {
787 container->obj.alignment = TOP_MIDDLE;
788 }
789 else {
790 container->obj.alignment = CENTER;
791 }
792
793 container->obj.area.width = AVAILABLE_WIDTH;
794
795 // set this new container as child of main container
796 layoutAddObject(layoutInt, (nbgl_obj_t *) container);
797
798 return 0;
799}
800
810{
811 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
812 nbgl_progress_bar_t *progress;
813
814 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddProgressBar():\n");
815 if (layout == NULL) {
816 return -1;
817 }
818 if (barLayout->text != NULL) {
819 nbgl_text_area_t *textArea;
820
822 ((nbgl_layoutInternal_t *) layout)->layer);
823 textArea->textColor = WHITE;
824 textArea->text = PIC(barLayout->text);
825 textArea->textAlignment = CENTER;
826 textArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
827 textArea->obj.area.width = AVAILABLE_WIDTH;
828 textArea->obj.area.height = nbgl_getTextHeight(textArea->fontId, textArea->text);
829 textArea->obj.alignment = TOP_MIDDLE;
830 textArea->obj.alignmentMarginX = 0;
831 textArea->obj.alignmentMarginY = 16; // 16 px from top
832 layoutAddObject(layoutInt, (nbgl_obj_t *) textArea);
833 }
835 ((nbgl_layoutInternal_t *) layout)->layer);
836 progress->foregroundColor = WHITE;
837 progress->withBorder = true;
838 progress->state = barLayout->percentage;
839 progress->obj.area.width = 102;
840 progress->obj.area.height = 14;
841 progress->obj.alignment = TOP_MIDDLE;
842 progress->obj.alignmentMarginX = 0;
843 progress->obj.alignmentMarginY = 33; // 33px from top
844 layoutAddObject(layoutInt, (nbgl_obj_t *) progress);
845
846 if (barLayout->subText != NULL) {
847 nbgl_text_area_t *subTextArea;
848
849 subTextArea = (nbgl_text_area_t *) nbgl_objPoolGet(
850 TEXT_AREA, ((nbgl_layoutInternal_t *) layout)->layer);
851 subTextArea->textColor = WHITE;
852 subTextArea->text = PIC(barLayout->subText);
853 subTextArea->textAlignment = CENTER;
854 subTextArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
855 subTextArea->obj.area.width = AVAILABLE_WIDTH;
856 subTextArea->obj.area.height = nbgl_getTextHeight(subTextArea->fontId, subTextArea->text);
857 subTextArea->obj.alignment = BOTTOM_MIDDLE;
858 subTextArea->obj.alignTo = (nbgl_obj_t *) progress;
859 subTextArea->obj.alignmentMarginX = 0;
860 subTextArea->obj.alignmentMarginY = 4;
861 layoutAddObject(layoutInt, (nbgl_obj_t *) subTextArea);
862 }
863
864 return 0;
865}
866
875{
876 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
877 nbgl_button_t *button;
878
879 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddButton():\n");
880 if (layout == NULL) {
881 return -1;
882 }
883 button = (nbgl_button_t *) nbgl_objPoolGet(BUTTON, ((nbgl_layoutInternal_t *) layout)->layer);
884 button->foregroundColor = BLACK;
885 button->innerColor = WHITE;
886 button->borderColor = WHITE;
887 button->radius = RADIUS_3_PIXELS;
888 button->text = (const char *) PIC(buttonInfo->text);
890 button->icon = (const nbgl_icon_details_t *) PIC(buttonInfo->icon);
891 button->obj.area.width = nbgl_getTextWidth(button->fontId, button->text) + BUTTON_MARGIN_Y;
892 button->obj.area.height = 14;
893 layoutAddObject(layoutInt, (nbgl_obj_t *) button);
894
895 return 0;
896}
897
906{
907 nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
908 nbgl_button_t *button;
909 nbgl_text_area_t *textArea;
910
911 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddSwitch():\n");
912 if (layout == NULL) {
913 return -1;
914 }
915 // add switch name as title
916 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
917 textArea->textColor = WHITE;
918 textArea->text = PIC(switchLayout->text);
919 textArea->textAlignment = CENTER;
921 textArea->obj.area.width = AVAILABLE_WIDTH;
922 textArea->wrapping = true;
923 uint16_t nbLines
924 = nbgl_getTextNbLinesInWidth(textArea->fontId, textArea->text, AVAILABLE_WIDTH, true);
925 // if more than 1 line on screen
926 if (nbLines > 1) {
927 LOG_WARN(
928 LAYOUT_LOGGER, "nbgl_layoutAddSwitch(): item text too long (%d lines > 1)\n", nbLines);
929 return -1;
930 }
931 textArea->obj.area.height = nbgl_getFontLineHeight(textArea->fontId);
932 textArea->obj.alignment = TOP_MIDDLE;
933 textArea->obj.alignmentMarginY = 3;
934 layoutAddObject(layoutInt, (nbgl_obj_t *) textArea);
935
936 if (switchLayout->subText != NULL) {
937 // add switch description
938 textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
939 textArea->textColor = WHITE;
940 textArea->text = PIC(switchLayout->subText);
941 textArea->textAlignment = CENTER;
942 textArea->fontId = BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp;
943 textArea->obj.area.width = AVAILABLE_WIDTH;
944 textArea->obj.area.height = 2 * nbgl_getFontLineHeight(textArea->fontId);
945 textArea->wrapping = true;
946 nbLines
947 = nbgl_getTextNbLinesInWidth(textArea->fontId, textArea->text, AVAILABLE_WIDTH, true);
948 // if more than 2 lines on screen
949 if (nbLines > 2) {
951 "nbgl_layoutAddSwitch(): sub text too long (%d lines > 2)\n",
952 nbLines);
953 return -1;
954 }
955 textArea->obj.alignment = CENTER;
956 textArea->obj.alignmentMarginY = 1; // not exactly centered
957 layoutAddObject(layoutInt, (nbgl_obj_t *) textArea);
958 }
959
960 button = (nbgl_button_t *) nbgl_objPoolGet(BUTTON, ((nbgl_layoutInternal_t *) layout)->layer);
961 button->foregroundColor = BLACK;
962 button->innerColor = WHITE;
963 button->borderColor = WHITE;
964 button->radius = RADIUS_3_PIXELS;
965 button->text = (switchLayout->initState == ON_STATE) ? "Enabled" : "Disabled";
967 button->icon = (switchLayout->initState == ON_STATE) ? &C_Switch_On_8px : &C_Switch_Off_8px;
968 // 2 pixels between icon & text, and 4 pixels on each side
969 button->obj.area.width
970 = nbgl_getTextWidth(button->fontId, button->text) + 2 + C_Switch_Off_8px.width + 8;
971 button->obj.area.height = 12;
972 button->obj.alignment = BOTTOM_MIDDLE;
973 button->obj.alignmentMarginY = 3;
974 layoutAddObject(layoutInt, (nbgl_obj_t *) button);
975
976 return 0;
977}
978
986{
987 nbgl_layoutInternal_t *layout = (nbgl_layoutInternal_t *) layoutParam;
988
989 if (layout == NULL) {
990 LOG_WARN(LAYOUT_LOGGER, "nbgl_layoutDraw(): layout is NULL\n");
991 return -1;
992 }
993 LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutDraw(): layout->nbChildren = %d\n", layout->nbChildren);
995
996 return 0;
997}
998
1006{
1007 nbgl_layoutInternal_t *layout = (nbgl_layoutInternal_t *) layoutParam;
1008 LOG_DEBUG(PAGE_LOGGER, "nbgl_layoutRelease(): \n");
1009 if (layout == NULL) {
1010 return -1;
1011 }
1012 // if modal
1013 if (layout->modal) {
1014 nbgl_screenPop(layout->layer);
1015 }
1016 layout->nbChildren = 0;
1017 return 0;
1018}
1019#endif // HAVE_SE_TOUCH
Random Number Generation.
nbgl_contentCenteredInfoStyle_t
possible styles for Centered Info Area
@ BOLD_TEXT1_INFO
bold is used for text1 (but '\b' can switch to regular)
@ BUTTON_INFO
bold is used for text1 and text2 as a white button
@ REGULAR_INFO
both texts regular (but '\b' can switch to bold)
debug traces management
#define LOG_WARN(__logger,...)
Definition nbgl_debug.h:89
@ LAYOUT_LOGGER
Definition nbgl_debug.h:35
@ PAGE_LOGGER
Definition nbgl_debug.h:36
#define LOG_DEBUG(__logger,...)
Definition nbgl_debug.h:88
#define LOG_FATAL(__logger,...)
Definition nbgl_debug.h:90
Middle Level API of the new BOLOS Graphical Library.
void nbgl_getTextMaxLenAndWidth(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, uint16_t *len, uint16_t *width, bool wrapping)
bool nbgl_getTextMaxLenInNbLines(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, uint16_t maxNbLines, uint16_t *len, bool wrapping)
@ BAGL_FONT_OPEN_SANS_REGULAR_11px_1bpp
Definition nbgl_fonts.h:152
@ BAGL_FONT_OPEN_SANS_EXTRABOLD_11px_1bpp
Definition nbgl_fonts.h:150
uint16_t nbgl_getTextWidth(nbgl_font_id_e fontId, const char *text)
uint16_t nbgl_getTextNbLinesInWidth(nbgl_font_id_e fontId, const char *text, uint16_t maxWidth, bool wrapping)
const nbgl_font_t * nbgl_getFont(nbgl_font_id_e fontId)
uint16_t nbgl_getTextHeight(nbgl_font_id_e fontId, const char *text)
uint8_t nbgl_getFontLineHeight(nbgl_font_id_e fontId)
uint16_t nbgl_getSingleLineTextWidthInLen(nbgl_font_id_e fontId, const char *text, uint16_t maxLen)
Font screen low-Level driver API, to draw elementary forms.
#define NB_MAX_LINES
@ HORIZONTAL_NAV
'<' and '>' are displayed, to navigate between pages and steps
@ LEFT_ARROW
left arrow is used
@ RIGHT_ARROW
right arrow is used
nbgl_contentSwitch_t nbgl_layoutSwitch_t
Deprecated, kept for retro compatibility.
#define AVAILABLE_WIDTH
void * nbgl_layout_t
type shared externally
struct nbgl_layoutDescription_s nbgl_layoutDescription_t
Structure containing all information when creating a layout. This structure must be passed as argumen...
void(* nbgl_layoutButtonCallback_t)(nbgl_layout_t *layout, nbgl_buttonEvent_t event)
prototype of function to be called when buttons are touched on a screen
nbgl_contentCenteredInfo_t nbgl_layoutCenteredInfo_t
Deprecated, kept for retro compatibility.
struct nbgl_layoutInternal_s nbgl_layoutInternal_t
Structure containing all information about the current layout.
Internal functions/constants of NBGL layout layer.
#define NB_MAX_BUTTON_ROWS_PUBLIC
Maximum number of buttons used to display the sub text in BUTTON_INFO style with nbgl_layoutAddText()...
#define NB_MAX_CHAR_IN_BUTTON_ROW
Maximum number of bytes (including '\0') of the text of a button in BUTTON_INFO style....
#define NB_MAX_BUTTON_ROWS
Maximum number of buttons used to display the sub text in BUTTON_INFO style.
#define BUTTON_MARGIN_Y
Horizontal margin (sum of left and right) between the text of a button and its border.
int nbgl_layoutDraw(nbgl_layout_t *layoutParam)
Applies given layout. The screen will be redrawn.
int nbgl_layoutAddMenuList(nbgl_layout_t *layout, nbgl_layoutMenuList_t *list)
Creates a menu list (only for nanos) with the given parameters. The navigation (and selection) must b...
static void getButtonTextLen(const char *text, uint16_t maxWidth, uint16_t maxLen, uint16_t *len, uint16_t *width)
computes the part of the given text fitting in the given width and in the given number of bytes,...
int nbgl_layoutAddNavigation(nbgl_layout_t *layout, nbgl_layoutNavigation_t *info)
Creates navigation arrows on side(s) of the screen.
#define ELLIPSIS
int layoutAddText(nbgl_layout_t *layout, const char *text, const char *subText, nbgl_contentCenteredInfoStyle_t style, uint8_t nbMaxLines, bool ellipsis)
Same as nbgl_layoutAddText(), with the choice of the max number of lines (or buttons in BUTTON_INFO s...
int nbgl_layoutAddCenteredInfo(nbgl_layout_t *layout, const nbgl_layoutCenteredInfo_t *info)
Creates an area on the center of the main panel, with a possible icon/image, a possible text in black...
int nbgl_layoutAddText(nbgl_layout_t *layout, const char *text, const char *subText, nbgl_contentCenteredInfoStyle_t style)
Creates an area with given text and sub text, using the given style.
static void addTextWithEllipsis(nbgl_layoutInternal_t *layoutInt, nbgl_container_t *container, nbgl_text_area_t *textArea, uint8_t nbLines, uint16_t areaHeight)
displays the given number of lines of the text of the given text area, the last line ending with an e...
static void buttonCallback(nbgl_screen_t *screen, nbgl_buttonEvent_t buttonEvent)
int nbgl_layoutAddProgressBar(nbgl_layout_t *layout, const nbgl_layoutProgressBar_t *barLayout)
Creates an area in main panel to display a progress bar, with a title text and a description under th...
#define NB_MAX_SCREEN_CHILDREN
int nbgl_layoutAddSwitch(nbgl_layout_t *layout, const nbgl_layoutSwitch_t *switchLayout)
Creates an area in main panel to display a switch.
int nbgl_layoutAddButton(nbgl_layout_t *layout, const nbgl_layoutButton_t *buttonInfo)
Creates an area in main panel to display a button, with the given style.
nbgl_layout_t * nbgl_layoutGet(const nbgl_layoutDescription_t *description)
returns a layout of the given type. The layout is reset
#define NB_MAX_LAYOUTS
int nbgl_layoutRelease(nbgl_layout_t *layoutParam)
Release the layout obtained with nbgl_layoutGet().
void layoutAddObject(nbgl_layoutInternal_t *layout, nbgl_obj_t *obj)
adds the given obj to the layout
static nbgl_layoutInternal_t gLayout[NB_MAX_LAYOUTS]
array of layouts, if used by modal
uint16_t layoutGetButtonRowLen(const char *text, uint16_t *len, uint16_t *width)
computes the part of the given text displayed in a single button in BUTTON_INFO style....
#define ELLIPSIS_SIZE
API to draw all basic graphic objects.
struct PACKED__ nbgl_text_area_s nbgl_text_area_t
struct to represent a text area (TEXT_AREA type)
struct PACKED__ nbgl_progress_bar_s nbgl_progress_bar_t
struct to represent a progress bar (PROGRESS_BAR type)
nbgl_obj_t ** nbgl_containerPoolGet(uint8_t nbObjs, uint8_t layer)
void nbgl_keyboardCallback(nbgl_obj_t *obj, nbgl_buttonEvent_t buttonEvent)
nbgl_obj_t * nbgl_objPoolGet(nbgl_obj_type_t type, uint8_t layer)
nbgl_buttonEvent_t
Definition nbgl_obj.h:196
struct PACKED__ nbgl_image_s nbgl_image_t
struct to represent an image (IMAGE type)
void nbgl_keypadCallback(nbgl_obj_t *obj, nbgl_buttonEvent_t buttonEvent)
struct PACKED__ nbgl_button_s nbgl_button_t
struct to represent a button (BUTTON type) that can contain a text and/or an icon
void(* nbgl_buttonCallback_t)(void *obj, nbgl_buttonEvent_t buttonEvent)
prototype of function to be called when a button event is received by an object (TODO: change to scre...
Definition nbgl_obj.h:214
struct PACKED__ nbgl_container_s nbgl_container_t
struct to represent a container (CONTAINER type)
struct PACKED__ nbgl_obj_s nbgl_obj_t
Common structure for all graphical objects.
API to manage screens.
int nbgl_screenPush(nbgl_obj_t ***elements, uint8_t nbElements, const nbgl_screenTickerConfiguration_t *ticker, nbgl_buttonCallback_t buttonCallback)
int nbgl_screenSet(nbgl_obj_t ***elements, uint8_t nbElements, const nbgl_screenTickerConfiguration_t *ticker, nbgl_buttonCallback_t buttonCallback)
struct PACKED__ nbgl_screen_s nbgl_screen_t
struct to represent a screen (SCREEN type)
int nbgl_screenPop(uint8_t screenIndex)
nbgl_obj_t * nbgl_screenContainsObjType(nbgl_screen_t *screen, nbgl_obj_type_t type)
void nbgl_screenRedraw(void)
@ WHITE
Definition nbgl_types.h:157
@ BLACK
Definition nbgl_types.h:154
@ ON_STATE
Definition nbgl_types.h:214
@ RADIUS_3_PIXELS
3 pixels
Definition nbgl_types.h:385
@ VERTICAL
from top to bottom
Definition nbgl_types.h:222
#define MIN(x, y)
Definition nbgl_types.h:131
struct PACKED__ nbgl_icon_details_s nbgl_icon_details_t
Represents all information about an icon.
@ TOP_MIDDLE
Definition nbgl_types.h:195
@ CENTER
Definition nbgl_types.h:198
@ NO_ALIGNMENT
used when parent container layout is used
Definition nbgl_types.h:193
@ MID_RIGHT
Definition nbgl_types.h:199
@ MID_LEFT
Definition nbgl_types.h:197
@ BOTTOM_MIDDLE
Definition nbgl_types.h:201
@ KEYPAD
Keypad.
Definition nbgl_types.h:180
@ IMAGE
Bitmap (y and height must be multiple of 4 on Stax).
Definition nbgl_types.h:170
@ BUTTON
Rounded rectangle button with icon and/or text.
Definition nbgl_types.h:173
@ PROGRESS_BAR
horizontal bar to indicate progression of something (between 0% and 100%)
Definition nbgl_types.h:176
@ KEYBOARD
Keyboard.
Definition nbgl_types.h:179
@ CONTAINER
Empty container.
Definition nbgl_types.h:169
@ TEXT_AREA
Area to contain text line(s).
Definition nbgl_types.h:172
@ NBGL_BPP_1
1 bit per pixel
Definition nbgl_types.h:296
@ NO_STYLE
no border
Definition nbgl_types.h:231
const char * text2
second text (can be null)
const char * text1
first text (can be null)
bool onTop
if set to true, align only horizontally
nbgl_contentCenteredInfoStyle_t style
style to apply to this info
const nbgl_icon_details_t * icon
a buffer containing the 1BPP icon
const char * text
main text for the switch
nbgl_state_t initState
initial state of the switch
const char * subText
description under main text (NULL terminated, single line, may be null)
structure defining an ASCII font
Definition nbgl_fonts.h:88
uint8_t line_height
height of a line for all characters in pixels
Definition nbgl_fonts.h:93
This structure contains info to build a single button.
const char * text
button text
const nbgl_icon_details_t * icon
a buffer containing the 1BPP icon for button
nbgl_screenTickerConfiguration_t ticker
nbgl_layoutButtonCallback_t onActionCallback
the callback to be called on any action on the layout
uint8_t layer
layer in screen stack
nbgl_layoutTouchCallback_t callback
user callback for all controls
uint8_t nbChildren
number of children in above array
char buttonTexts[NB_MAX_BUTTON_ROWS][NB_MAX_CHAR_IN_BUTTON_ROW]
uint8_t modal
if true, means the screen is a modal
nbgl_obj_t ** children
children for main screen
This structure contains a list of names to build a menu list on Nanos, with for each item a descripti...
nbgl_menuListCallback_t callback
function to call to retrieve a menu list item text
uint8_t nbChoices
total number of choices in the menu list
uint8_t selectedChoice
index of the selected choice (centered, in bold)
This structure contains info to build a navigation bar at the bottom of the screen.
nbgl_layoutNavDirection_t direction
vertical or horizontal navigation
nbgl_layoutNavIndication_t indication
specifies which arrows to use (left or right)
This structure contains info to build a progress bar with info. The progress bar itself is 120px widt...
uint8_t percentage
percentage of completion, from 0 to 100.
const char * text
text in black, on top of progress bar
const char * subText
text in gray, under progress bar