2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 private unit ui.widget.text;
21 uses ui.widget.common;
25 fn text_init(align : widget_align, label : string, color_scheme : bytes, w : world, app : appstate, id : wid) : (world, appstate, text_state);
26 fn text_get_width(app : appstate, com : widget_common, st : text_state, x : int) : int;
27 fn text_get_height(app : appstate, com : widget_common, st : text_state, x : int) : int;
28 fn text_reflow(app : appstate, com : widget_common, st : text_state) : (appstate, widget_common, text_state);
29 fn text_redraw(app : appstate, curs : curses, com : widget_common, st : text_state) : curses;
31 const text_class~flat := widget_class.[
34 is_selectable : false,
35 get_width : text_get_width,
36 get_height : text_get_height,
50 fn text_init(align : widget_align, label : string, color_scheme : bytes, implicit w : world, implicit app : appstate, id : wid) : (world, appstate, text_state)
55 color_scheme : color_scheme,
60 fn reflow(label : string, x : int, hard_break : bool) : list(string)
63 var tmp_line := empty(char);
65 var result := empty(string);
66 for i := 0 to len(label) do [
70 tmp_line := empty(char);
78 tmp_len += char_length(c);
81 result +<= tmp_line[ .. lbr];
82 tmp_line := tmp_line[lbr + 1 .. ];
83 ] else if hard_break, tmp_len > 1 then [
84 result +<= tmp_line[ .. tmp_len - 1];
85 tmp_line := tmp_line[tmp_len - 1 .. ];
87 tmp_len := string_length(tmp_line);
96 fn text_get_width(app : appstate, com : widget_common, st : text_state, x : int) : int
98 var flow := reflow(st.label, x, false);
100 for i := 0 to len(flow) do
101 x := max(x, len(flow[i]));
105 fn text_get_height(app : appstate, com : widget_common, st : text_state, x : int) : int
107 var flow := reflow(st.label, x, true);
111 fn text_reflow(implicit app : appstate, implicit com : widget_common, implicit st : text_state) : (appstate, widget_common, text_state)
113 var flow := reflow(st.label, com.size_x, true);
117 fn text_redraw(implicit app : appstate, implicit curs : curses, com : widget_common, st : text_state) : curses
119 property_set_attrib(property_get_attrib(st.color_scheme + "text", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
120 curses_fill_rect(0, com.size_x, 0, com.size_y, ' ');
121 for i := 0 to len(st.flow) do [
124 if st.align is center then
125 x := com.size_x - string_length(s) shr 1;
126 else if st.align is right then
127 x := com.size_x - string_length(s);
128 curses_set_pos(x, i);