text_box.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "text_box.h"
  2. #include "gui/canvas.h"
  3. #include <furi.h>
  4. #include <gui/elements.h>
  5. #include <stdint.h>
  6. struct TextBox {
  7. View* view;
  8. };
  9. typedef struct {
  10. const char* text;
  11. char* text_pos;
  12. FuriString* text_formatted;
  13. int32_t scroll_pos;
  14. int32_t scroll_num;
  15. TextBoxFont font;
  16. TextBoxFocus focus;
  17. bool formatted;
  18. } TextBoxModel;
  19. static void text_box_process_down(TextBox* text_box) {
  20. with_view_model(
  21. text_box->view, (TextBoxModel * model) {
  22. if(model->scroll_pos < model->scroll_num - 1) {
  23. model->scroll_pos++;
  24. // Search next line start
  25. while(*model->text_pos++ != '\n')
  26. ;
  27. }
  28. return true;
  29. });
  30. }
  31. static void text_box_process_up(TextBox* text_box) {
  32. with_view_model(
  33. text_box->view, (TextBoxModel * model) {
  34. if(model->scroll_pos > 0) {
  35. model->scroll_pos--;
  36. // Reach last symbol of previous line
  37. model->text_pos--;
  38. // Search prevous line start
  39. while((model->text_pos != model->text) && (*(--model->text_pos) != '\n'))
  40. ;
  41. if(*model->text_pos == '\n') {
  42. model->text_pos++;
  43. }
  44. }
  45. return true;
  46. });
  47. }
  48. static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
  49. size_t i = 0;
  50. size_t line_width = 0;
  51. const char* str = model->text;
  52. size_t line_num = 0;
  53. const size_t text_width = 120;
  54. while(str[i] != '\0') {
  55. char symb = str[i++];
  56. if(symb != '\n') {
  57. size_t glyph_width = canvas_glyph_width(canvas, symb);
  58. if(line_width + glyph_width > text_width) {
  59. line_num++;
  60. line_width = 0;
  61. furi_string_push_back(model->text_formatted, '\n');
  62. }
  63. line_width += glyph_width;
  64. } else {
  65. line_num++;
  66. line_width = 0;
  67. }
  68. furi_string_push_back(model->text_formatted, symb);
  69. }
  70. line_num++;
  71. model->text = furi_string_get_cstr(model->text_formatted);
  72. model->text_pos = (char*)model->text;
  73. if(model->focus == TextBoxFocusEnd && line_num > 5) {
  74. // Set text position to 5th line from the end
  75. for(uint8_t i = 0; i < line_num - 5; i++) {
  76. while(*model->text_pos++ != '\n') {
  77. };
  78. }
  79. model->scroll_num = line_num - 4;
  80. model->scroll_pos = line_num - 5;
  81. } else {
  82. model->scroll_num = MAX(line_num - 4, 0u);
  83. model->scroll_pos = 0;
  84. }
  85. }
  86. static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
  87. TextBoxModel* model = _model;
  88. canvas_clear(canvas);
  89. if(model->font == TextBoxFontText) {
  90. canvas_set_font(canvas, FontSecondary);
  91. } else if(model->font == TextBoxFontHex) {
  92. canvas_set_font(canvas, FontKeyboard);
  93. }
  94. if(!model->formatted) {
  95. text_box_insert_endline(canvas, model);
  96. model->formatted = true;
  97. }
  98. elements_slightly_rounded_frame(canvas, 0, 0, 124, 64);
  99. elements_multiline_text(canvas, 3, 11, model->text_pos);
  100. elements_scrollbar(canvas, model->scroll_pos, model->scroll_num);
  101. }
  102. static bool text_box_view_input_callback(InputEvent* event, void* context) {
  103. furi_assert(context);
  104. TextBox* text_box = context;
  105. bool consumed = false;
  106. if(event->type == InputTypeShort) {
  107. if(event->key == InputKeyDown) {
  108. text_box_process_down(text_box);
  109. consumed = true;
  110. } else if(event->key == InputKeyUp) {
  111. text_box_process_up(text_box);
  112. consumed = true;
  113. }
  114. }
  115. return consumed;
  116. }
  117. TextBox* text_box_alloc() {
  118. TextBox* text_box = malloc(sizeof(TextBox));
  119. text_box->view = view_alloc();
  120. view_set_context(text_box->view, text_box);
  121. view_allocate_model(text_box->view, ViewModelTypeLocking, sizeof(TextBoxModel));
  122. view_set_draw_callback(text_box->view, text_box_view_draw_callback);
  123. view_set_input_callback(text_box->view, text_box_view_input_callback);
  124. with_view_model(
  125. text_box->view, (TextBoxModel * model) {
  126. model->text = NULL;
  127. model->text_formatted = furi_string_alloc_set("");
  128. model->formatted = false;
  129. model->font = TextBoxFontText;
  130. return true;
  131. });
  132. return text_box;
  133. }
  134. void text_box_free(TextBox* text_box) {
  135. furi_assert(text_box);
  136. with_view_model(
  137. text_box->view, (TextBoxModel * model) {
  138. furi_string_free(model->text_formatted);
  139. return true;
  140. });
  141. view_free(text_box->view);
  142. free(text_box);
  143. }
  144. View* text_box_get_view(TextBox* text_box) {
  145. furi_assert(text_box);
  146. return text_box->view;
  147. }
  148. void text_box_reset(TextBox* text_box) {
  149. furi_assert(text_box);
  150. with_view_model(
  151. text_box->view, (TextBoxModel * model) {
  152. model->text = NULL;
  153. furi_string_set(model->text_formatted, "");
  154. model->font = TextBoxFontText;
  155. model->focus = TextBoxFocusStart;
  156. return true;
  157. });
  158. }
  159. void text_box_set_text(TextBox* text_box, const char* text) {
  160. furi_assert(text_box);
  161. furi_assert(text);
  162. with_view_model(
  163. text_box->view, (TextBoxModel * model) {
  164. model->text = text;
  165. furi_string_reset(model->text_formatted);
  166. furi_string_reserve(model->text_formatted, strlen(text));
  167. model->formatted = false;
  168. return true;
  169. });
  170. }
  171. void text_box_set_font(TextBox* text_box, TextBoxFont font) {
  172. furi_assert(text_box);
  173. with_view_model(
  174. text_box->view, (TextBoxModel * model) {
  175. model->font = font;
  176. return true;
  177. });
  178. }
  179. void text_box_set_focus(TextBox* text_box, TextBoxFocus focus) {
  180. furi_assert(text_box);
  181. with_view_model(
  182. text_box->view, (TextBoxModel * model) {
  183. model->focus = focus;
  184. return true;
  185. });
  186. }