return unit_type from verify_function, so that it can modify the context
[ajla.git] / stdlib / ui / widget / display.ajla
blobaadbe8d76afe45096308887361bb2b0c1be96362
1 {*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
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
9  * version.
10  *
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.
14  *
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/>.
17  *}
19 private unit ui.widget.display;
21 uses ui.widget.common;
23 type display_state;
25 fn display_init(display_prop : bytes, color_scheme : bytes, w : world, app : appstate, id : wid) : (world, appstate, display_state);
26 fn display_get_width(app : appstate, com : widget_common, st : display_state, x : int) : int;
27 fn display_get_height(app : appstate, com : widget_common, st : display_state, x : int) : int;
28 fn display_redraw(app : appstate, curs : curses, com : widget_common, st : display_state) : curses;
29 fn display_process_event(w : world, app : appstate, com : widget_common, st : display_state, wev : wevent) : (world, appstate, widget_common, display_state);
31 const display_class~flat := widget_class.[
32         t : display_state,
33         name : "display",
34         is_selectable : false,
35         get_width : display_get_width,
36         get_height : display_get_height,
37         redraw : display_redraw,
38         process_event : display_process_event,
41 implementation
43 record display_state [
44         display_prop : bytes;
45         color_scheme : bytes;
48 fn display_init(display_prop : bytes, color_scheme : bytes, implicit w : world, implicit app : appstate, id : wid) : (world, appstate, display_state)
50         property_observe(id, display_prop);
51         return display_state.[
52                 display_prop : display_prop,
53                 color_scheme : color_scheme,
54         ];
57 fn display_get_width(app : appstate, com : widget_common, st : display_state, x : int) : int
59         return max(x, 6);
62 fn display_get_height(app : appstate, com : widget_common, st : display_state, x : int) : int
64         return 1;
67 fn display_redraw(implicit app : appstate, implicit curs : curses, com : widget_common, st : display_state) : curses
69         var disp := property_get(st.display_prop).s;
70         if len(disp) > com.size_x then [
71                 if com.size_x >= 6 then
72                         disp := `...` + disp[len(disp) - (com.size_x - 3) ..];
73         ]
74         curses_set_pos(0, 0);
75         property_set_attrib(property_get_attrib(st.color_scheme + "display", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
76         curses_print(disp);
79 fn display_process_event(implicit w : world, implicit app : appstate, implicit com : widget_common, implicit st : display_state, wev : wevent) : (world, appstate, widget_common, display_state)
81         if wev is property_changed then [
82                 widget_enqueue_event(com.self, wevent.redraw.(event_redraw.[
83                         x1 : 0,
84                         x2 : com.size_x,
85                         y1 : 0,
86                         y2 : com.size_y,
87                 ]));
88         ]