text_box.c 5.8 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. void* context;
  10. TextBoxExitCallback callback;
  11. };
  12. typedef struct {
  13. const char* text;
  14. char* text_pos;
  15. string_t text_formatted;
  16. size_t scroll_pos;
  17. size_t scroll_num;
  18. TextBoxFont font;
  19. bool formatted;
  20. } TextBoxModel;
  21. static void text_box_process_down(TextBox* text_box) {
  22. with_view_model(
  23. text_box->view, (TextBoxModel * model) {
  24. if(model->scroll_pos < model->scroll_num - 1) {
  25. model->scroll_pos++;
  26. // Search next line start
  27. while(*model->text_pos++ != '\n')
  28. ;
  29. }
  30. return true;
  31. });
  32. }
  33. static void text_box_process_up(TextBox* text_box) {
  34. with_view_model(
  35. text_box->view, (TextBoxModel * model) {
  36. if(model->scroll_pos > 0) {
  37. model->scroll_pos--;
  38. // Reach last symbol of previous line
  39. model->text_pos--;
  40. // Search prevous line start
  41. while((model->text_pos != model->text) && (*(--model->text_pos) != '\n'))
  42. ;
  43. if(*model->text_pos == '\n') {
  44. model->text_pos++;
  45. }
  46. }
  47. return true;
  48. });
  49. }
  50. static void text_box_process_back(TextBox* text_box) {
  51. if(text_box->callback) {
  52. text_box->callback(text_box->context);
  53. }
  54. }
  55. static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
  56. size_t i = 0;
  57. size_t line_width = 0;
  58. const char* str = model->text;
  59. size_t line_num = 0;
  60. const size_t text_width = 140;
  61. while(str[i] != '\0') {
  62. char symb = str[i++];
  63. if(symb != '\n') {
  64. line_width += canvas_glyph_width(canvas, symb) + 1;
  65. if(line_width > text_width) {
  66. line_num++;
  67. line_width = 0;
  68. string_push_back(model->text_formatted, '\n');
  69. }
  70. } else {
  71. line_num++;
  72. line_width = 0;
  73. }
  74. string_push_back(model->text_formatted, symb);
  75. }
  76. line_num++;
  77. model->text = string_get_cstr(model->text_formatted);
  78. model->text_pos = (char*)model->text;
  79. model->scroll_num = MAX(line_num - 4, 0);
  80. model->scroll_pos = 0;
  81. }
  82. static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
  83. TextBoxModel* model = _model;
  84. if(!model->formatted) {
  85. text_box_insert_endline(canvas, model);
  86. model->formatted = true;
  87. }
  88. canvas_clear(canvas);
  89. elements_slightly_rounded_frame(canvas, 0, 0, 124, 64);
  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. elements_multiline_text(canvas, 3, 11, model->text_pos);
  96. elements_scrollbar(canvas, model->scroll_pos, model->scroll_num);
  97. }
  98. static bool text_box_view_input_callback(InputEvent* event, void* context) {
  99. furi_assert(context);
  100. TextBox* text_box = context;
  101. bool consumed = false;
  102. if(event->type == InputTypeShort) {
  103. if(event->key == InputKeyDown) {
  104. text_box_process_down(text_box);
  105. consumed = true;
  106. } else if(event->key == InputKeyUp) {
  107. text_box_process_up(text_box);
  108. consumed = true;
  109. } else if(event->key == InputKeyBack) {
  110. text_box_process_back(text_box);
  111. consumed = true;
  112. }
  113. }
  114. return consumed;
  115. }
  116. TextBox* text_box_alloc() {
  117. TextBox* text_box = furi_alloc(sizeof(TextBox));
  118. text_box->view = view_alloc();
  119. view_set_context(text_box->view, text_box);
  120. view_allocate_model(text_box->view, ViewModelTypeLocking, sizeof(TextBoxModel));
  121. view_set_draw_callback(text_box->view, text_box_view_draw_callback);
  122. view_set_input_callback(text_box->view, text_box_view_input_callback);
  123. with_view_model(
  124. text_box->view, (TextBoxModel * model) {
  125. model->text = NULL;
  126. string_init_set_str(model->text_formatted, "");
  127. model->formatted = false;
  128. model->font = TextBoxFontText;
  129. return true;
  130. });
  131. return text_box;
  132. }
  133. void text_box_free(TextBox* text_box) {
  134. furi_assert(text_box);
  135. with_view_model(
  136. text_box->view, (TextBoxModel * model) {
  137. string_clear(model->text_formatted);
  138. return true;
  139. });
  140. view_free(text_box->view);
  141. free(text_box);
  142. }
  143. View* text_box_get_view(TextBox* text_box) {
  144. furi_assert(text_box);
  145. return text_box->view;
  146. }
  147. void text_box_clean(TextBox* text_box) {
  148. furi_assert(text_box);
  149. with_view_model(
  150. text_box->view, (TextBoxModel * model) {
  151. model->text = NULL;
  152. string_set_str(model->text_formatted, "");
  153. model->font = TextBoxFontText;
  154. return true;
  155. });
  156. text_box->context = NULL;
  157. text_box->callback = NULL;
  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_reserve(model->text_formatted, strlen(text));
  166. model->formatted = false;
  167. return true;
  168. });
  169. }
  170. void text_box_set_font(TextBox* text_box, TextBoxFont font) {
  171. furi_assert(text_box);
  172. with_view_model(
  173. text_box->view, (TextBoxModel * model) {
  174. model->font = font;
  175. return true;
  176. });
  177. }
  178. void text_box_set_context(TextBox* text_box, void* context) {
  179. furi_assert(text_box);
  180. text_box->context = context;
  181. }
  182. void text_box_set_exit_callback(TextBox* text_box, TextBoxExitCallback callback) {
  183. furi_assert(text_box);
  184. text_box->callback = callback;
  185. }