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.button;
21 uses ui.widget.common;
25 fn button_no_action(w : world, app : appstate, id : wid) : (world, appstate);
26 fn button_init(label : string, braces : bool, color_scheme : bytes, change_focus : fn(world, appstate, wid) : (world, appstate), click : fn(world, appstate, wid) : (world, appstate), w : world, app : appstate, id : wid) : (world, appstate, button_state);
27 fn button_get_width(app : appstate, com : widget_common, st : button_state, x : int) : int;
28 fn button_redraw(app : appstate, curs : curses, com : widget_common, st : button_state) : curses;
29 fn button_get_cursor(app : appstate, com : widget_common, st : button_state) : (int, int);
30 fn button_accepts_key(app : appstate, com : widget_common, st : button_state, k : event_keyboard) : bool;
31 fn button_process_event(w : world, app : appstate, com : widget_common, st : button_state, wev : wevent) : (world, appstate, widget_common, button_state);
33 const button_class~flat := widget_class.[
37 get_width : button_get_width,
38 redraw : button_redraw,
39 get_cursor : button_get_cursor,
40 accepts_key : button_accepts_key,
41 process_event : button_process_event,
50 change_focus : fn(world, appstate, wid) : (world, appstate);
51 click : fn(world, appstate, wid) : (world, appstate);
54 fn button_no_action(implicit w : world, implicit app : appstate, id : wid) : (world, appstate)
58 fn button_init(label : string, braces : bool, color_scheme : bytes, change_focus : fn(world, appstate, wid) : (world, appstate), click : fn(world, appstate, wid) : (world, appstate), implicit w : world, implicit app : appstate, id : wid) : (world, appstate, button_state)
63 color_scheme : color_scheme,
64 change_focus : change_focus,
69 fn button_get_width(app : appstate, com : widget_common, st : button_state, x : int) : int
72 return string_length(`[ ` + st.label + ` ]`);
74 return string_length(st.label);
77 fn button_redraw(implicit app : appstate, implicit curs : curses, com : widget_common, st : button_state) : curses
80 if widget_is_top(com.self) then [
81 property_set_attrib(property_get_attrib(st.color_scheme + "button-selected", #ffff, #ffff, #ffff, #0000, #0000, #0000, curses_bold, curses_bold));
83 property_set_attrib(property_get_attrib(st.color_scheme + "button", #0000, #0000, #0000, #aaaa, #aaaa, #aaaa, 0, curses_invert));
86 curses_print(`[ ` + st.label + ` ]`);
88 curses_print(st.label);
91 fn button_get_cursor(implicit app : appstate, com : widget_common, st : button_state) : (int, int)
93 return select(st.braces, 0, 2), 0;
96 fn button_accepts_key(app : appstate, com : widget_common, st : button_state, k : event_keyboard) : bool
98 return k.key = key_enter or k.key = ' ';
101 fn button_process_event(implicit w : world, implicit app : appstate, implicit com : widget_common, implicit st : button_state, wev : wevent) : (world, appstate, widget_common, button_state)
103 if wev is keyboard then [
107 if wev is mouse, wev.mouse.prev_buttons = 1, wev.mouse.buttons = 0 then [
111 if wev is change_focus then [
112 st.change_focus(com.self);
113 widget_enqueue_event(com.self, wevent.redraw.(event_redraw.[