text_box.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = 120;
  55. while(str[i] != '\0') {
  56. char symb = str[i++];
  57. if(symb != '\n') {
  58. size_t glyph_width = canvas_glyph_width(canvas, symb);
  59. if(line_width + glyph_width > text_width) {
  60. line_num++;
  61. line_width = 0;
  62. string_push_back(model->text_formatted, '\n');
  63. }
  64. line_width += glyph_width;
  65. } else {
  66. line_num++;
  67. line_width = 0;
  68. }
  69. string_push_back(model->text_formatted, symb);
  70. }
  71. line_num++;
  72. model->text = string_get_cstr(model->text_formatted);
  73. model->text_pos = (char*)model->text;
  74. if(model->focus == TextBoxFocusEnd && line_num > 5) {
  75. // Set text position to 5th line from the end
  76. for(uint8_t i = 0; i < line_num - 5; i++) {
  77. while(*model->text_pos++ != '\n') {
  78. };
  79. }
  80. model->scroll_num = line_num - 4;
  81. model->scroll_pos = line_num - 5;
  82. } else {
  83. model->scroll_num = MAX(line_num - 4, 0);
  84. model->scroll_pos = 0;
  85. }
  86. }
  87. static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
  88. TextBoxModel* model = _model;
  89. canvas_clear(canvas);
  90. if(model->font == TextBoxFontText) {
  91. canvas_set_font(canvas, FontSecondary);
  92. } else if(model->font == TextBoxFontHex) {
  93. canvas_set_font(canvas, FontKeyboard);
  94. }
  95. if(!model->formatted) {
  96. text_box_insert_endline(canvas, model);
  97. model->formatted = true;
  98. }
  99. elements_slightly_rounded_frame(canvas, 0, 0, 124, 64);
  100. elements_multiline_text(canvas, 3, 11, model->text_pos);
  101. elements_scrollbar(canvas, model->scroll_pos, model->scroll_num);
  102. }
  103. static bool text_box_view_input_callback(InputEvent* event, void* context) {
  104. furi_assert(context);
  105. TextBox* text_box = context;
  106. bool consumed = false;
  107. if(event->type == InputTypeShort) {
  108. if(event->key == InputKeyDown) {
  109. text_box_process_down(text_box);
  110. consumed = true;
  111. } else if(event->key == InputKeyUp) {
  112. text_box_process_up(text_box);
  113. consumed = true;
  114. }
  115. }
  116. return consumed;
  117. }
  118. TextBox* text_box_alloc() {
  119. TextBox* text_box = malloc(sizeof(TextBox));
  120. text_box->view = view_alloc();
  121. view_set_context(text_box->view, text_box);
  122. view_allocate_model(text_box->view, ViewModelTypeLocking, sizeof(TextBoxModel));
  123. view_set_draw_callback(text_box->view, text_box_view_draw_callback);
  124. view_set_input_callback(text_box->view, text_box_view_input_callback);
  125. with_view_model(
  126. text_box->view, (TextBoxModel * model) {
  127. model->text = NULL;
  128. string_init_set_str(model->text_formatted, "");
  129. model->formatted = false;
  130. model->font = TextBoxFontText;
  131. return true;
  132. });
  133. return text_box;
  134. }
  135. void text_box_free(TextBox* text_box) {
  136. furi_assert(text_box);
  137. with_view_model(
  138. text_box->view, (TextBoxModel * model) {
  139. string_clear(model->text_formatted);
  140. return true;
  141. });
  142. view_free(text_box->view);
  143. free(text_box);
  144. }
  145. View* text_box_get_view(TextBox* text_box) {
  146. furi_assert(text_box);
  147. return text_box->view;
  148. }
  149. void text_box_reset(TextBox* text_box) {
  150. furi_assert(text_box);
  151. with_view_model(
  152. text_box->view, (TextBoxModel * model) {
  153. model->text = NULL;
  154. string_set_str(model->text_formatted, "");
  155. model->font = TextBoxFontText;
  156. model->focus = TextBoxFocusStart;
  157. return true;
  158. });
  159. }
  160. void text_box_set_text(TextBox* text_box, const char* text) {
  161. furi_assert(text_box);
  162. furi_assert(text);
  163. with_view_model(
  164. text_box->view, (TextBoxModel * model) {
  165. model->text = text;
  166. string_reset(model->text_formatted);
  167. string_reserve(model->text_formatted, strlen(text));
  168. model->formatted = false;
  169. return true;
  170. });
  171. }
  172. void text_box_set_font(TextBox* text_box, TextBoxFont font) {
  173. furi_assert(text_box);
  174. with_view_model(
  175. text_box->view, (TextBoxModel * model) {
  176. model->font = font;
  177. return true;
  178. });
  179. }
  180. void text_box_set_focus(TextBox* text_box, TextBoxFocus focus) {
  181. furi_assert(text_box);
  182. with_view_model(
  183. text_box->view, (TextBoxModel * model) {
  184. model->focus = focus;
  185. return true;
  186. });
  187. }