verify: implement P_Array_Append_One
[ajla.git] / stdlib / ui / widget / msgbox.ajla
blob153c757da62aa6920c9f83be131384a65a813bc0
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.msgbox;
21 uses ui.widget.common;
23 record msgbox_button [
24         label : string;
25         click : fn(world, appstate, wid) : (world, appstate);
26         hotkeys : treeset(char);
29 fn msgbox_new(w : world, app : appstate, caption : string, align : widget_align, text : string, color_scheme : bytes, buttons : list(msgbox_button)) : (world, appstate, wid);
31 implementation
33 uses ui.widget.text;
34 uses ui.widget.button;
35 uses ui.widget.dialog;
37 fn msgbox_dialog_layout(implicit app : appstate, ids : list(wid), offs_x offs_y min_x pref_x max_x : int) : (appstate, int, int)
39         var xs := 0;
40         xs := max(xs, widget_get_width(ids[0], pref_x));
41         xs := max(xs, widgets_get_width(ids[1 .. ], 2, pref_x));
43         xs := min(xs, max_x);
44         xs := max(xs, min_x);
46         var yp := offs_y + 1;
48         yp := widget_place(ids[0], offs_x, xs, yp);
49         yp += 1;
50         yp := widgets_place(ids[1 .. ], widget_align.center, 2, 1, offs_x, xs, yp);
52         return xs, yp;
55 fn msgbox_new(implicit w : world, implicit app : appstate, caption : string, align : widget_align, text : string, color_scheme : bytes, buttons : list(msgbox_button)) : (world, appstate, wid)
57         var entries := [
58                 dialog_entry.[
59                         cls : text_class,
60                         init : text_init(align, text, color_scheme,,,),
61                 ]
62         ];
63         for i := 0 to len(buttons) do [
64                 entries +<= dialog_entry.[
65                         cls : button_class,
66                         init : button_init(buttons[i].label, true, color_scheme, button_no_action, buttons[i].click,,,),
67                         hotkeys : buttons[i].hotkeys,
68                 ];
69         ]
70         var id := widget_new_window(dialog_class, dialog_init(caption, entries, select(len(buttons) > 0, -1, 1), dialog_no_event, msgbox_dialog_layout, color_scheme,,,), false);
71         return id;