text_box.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "text_box.h"
  2. #include "gui/canvas.h"
  3. #include <m-string.h>
  4. #include <furi.h>
  5. #include <gui/elements.h>
  6. #include <stdint.h>
  7. struct TextBox {
  8. View* view;
  9. };
  10. typedef struct {
  11. const char* text;
  12. char* text_pos;
  13. string_t text_formatted;
  14. int32_t scroll_pos;
  15. int32_t scroll_num;
  16. TextBoxFont font;
  17. TextBoxFocus focus;
  18. bool formatted;
  19. } TextBoxModel;
  20. static void text_box_process_down(TextBox* text_box) {
  21. with_view_model(
  22. text_box->view, (TextBoxModel * model) {
  23. if(model->scroll_pos < model->scroll_num - 1) {
  24. model->scroll_pos++;
  25. // Search next line start
  26. while(*model->text_pos++ != '\n')
  27. ;
  28. }
  29. return true;
  30. });
  31. }
  32. static void text_box_process_up(TextBox* text_box) {
  33. with_view_model(
  34. text_box->view, (TextBoxModel * model) {
  35. if(model->scroll_pos > 0) {
  36. model->scroll_pos--;
  37. // Reach last symbol of previous line
  38. model->text_pos--;
  39. // Search prevous line start
  40. while((model->text_pos != model->text) && (*(--model->text_pos) != '\n'))
  41. ;
  42. if(*model->text_pos == '\n') {
  43. model->text_pos++;
  44. }
  45. }
  46. return true;
  47. });
  48. }
  49. static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
  50. size_t i = 0;
  51. size_t line_width = 0;
  52. const char* str = model->text;
  53. size_t line_num = 0;
  54. const size_t text_width = 140;
  55. while(str[i] != '\0') {
  56. char symb = str[i++];
  57. if(symb != '\n') {
  58. line_width += canvas_glyph_width(canvas, symb) + 1;
  59. if(line_width > text_width) {
  60. line_num++;
  61. line_width = 0;
  62. string_push_back(model->text_formatted, '\n');
  63. }
  64. } else {
  65. line_num++;
  66. line_width = 0;
  67. }
  68. string_push_back(model->text_formatted, symb);
  69. }
  70. line_num++;
  71. model->text = 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, 0);
  83. model->scroll_pos = 0;
  84. }
  85. }
  86. static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
  87. TextBoxModel* model = _model;
  88. if(!model->formatted) {
  89. text_box_insert_endline(canvas, model);
  90. model->formatted = true;
  91. }
  92. canvas_clear(canvas);
  93. elements_slightly_rounded_frame(canvas, 0, 0, 124, 64);
  94. if(model->font == TextBoxFontText) {
  95. canvas_set_font(canvas, FontSecondary);
  96. } else if(model->font == TextBoxFontHex) {
  97. canvas_set_font(canvas, FontKeyboard);
  98. }
  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. string_init_set_str(model->text_formatted, "");
  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. string_clear(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. string_set_str(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. string_reset(model->text_formatted);
  166. 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. }