|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Robin E. R. Davies |
| 3 | + * All rights reserved. |
| 4 | +
|
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | +
|
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "tests.hpp" |
| 25 | +#include <term.h> |
| 26 | +#include <memory.h> |
| 27 | + |
| 28 | +class NAltTextElement: public NElement { |
| 29 | +private: |
| 30 | + NAltTextElement(int acs) |
| 31 | + :NElement("AltText"), acs(acs) |
| 32 | + { |
| 33 | + } |
| 34 | +public: |
| 35 | + using self = NAltTextElement; |
| 36 | + using super = NElement; |
| 37 | + using ptr = std::shared_ptr<self>; |
| 38 | + |
| 39 | + static ptr create(int acs) { |
| 40 | + return std::shared_ptr<self>(new NAltTextElement(acs)); |
| 41 | + } |
| 42 | + |
| 43 | +protected: |
| 44 | + virtual NSize measure(const NSize& constraints) override { |
| 45 | + return { 1,1 }; |
| 46 | + } |
| 47 | + virtual void render() override { |
| 48 | + move(0,0); |
| 49 | + print_acs(0,0,this->acs); |
| 50 | + } |
| 51 | +private: |
| 52 | + int acs; |
| 53 | + |
| 54 | +}; |
| 55 | + |
| 56 | +class NCharacterTestWindow: public NElement { |
| 57 | +private: |
| 58 | + NCharacterTestWindow(wchar_t c) |
| 59 | + :NElement("CharacterTest"), c(c) |
| 60 | + { |
| 61 | + } |
| 62 | +public: |
| 63 | + using self = NCharacterTestWindow; |
| 64 | + using super = NElement; |
| 65 | + using ptr = std::shared_ptr<self>; |
| 66 | + |
| 67 | + static ptr create(wchar_t c) { |
| 68 | + return std::shared_ptr<self>(new NCharacterTestWindow(c)); |
| 69 | + } |
| 70 | + |
| 71 | +protected: |
| 72 | + virtual NSize measure(const NSize& constraints) override { |
| 73 | + return { 2,1 }; |
| 74 | + } |
| 75 | + virtual void render() override { |
| 76 | + bool valid = window()->can_display_character(this->c); |
| 77 | + move(0,0); |
| 78 | + wchar_t str[2] = {this->c,0}; |
| 79 | + if (!valid) { |
| 80 | + str[0] = L'X'; |
| 81 | + } |
| 82 | + print(str); |
| 83 | + } |
| 84 | +private: |
| 85 | + wchar_t c; |
| 86 | + |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +static NElement::ptr character_test(NWindow::ptr parentWindow, const std::string& label, char32_t c) |
| 91 | +{ |
| 92 | + bool supported = parentWindow->can_display_character(c); |
| 93 | + return NHorizontalStackElement::create() |
| 94 | + | column_gap(1) |
| 95 | + | add_child(NTextElement::create(label)) |
| 96 | + | add_child(NTextElement::create(u32string_to_utf8(std::u32string(1, c)))) |
| 97 | + | add_child(NCharacterTestWindow::create(c)) |
| 98 | + | add_child(NTextElement::create(supported ? "Supported" : "Not supported")); |
| 99 | +} |
| 100 | + |
| 101 | +static NElement::ptr acs_grid(NWindow::ptr parentWindow) |
| 102 | +{ |
| 103 | + NVerticalStackElement::ptr grid = NVerticalStackElement::create(); |
| 104 | + for (char32_t c = 0x20; c < 0x80; c += 16) |
| 105 | + { |
| 106 | + NHorizontalStackElement::ptr row = NHorizontalStackElement::create(); |
| 107 | + for (int i = 0; i < 16; i++) |
| 108 | + { |
| 109 | + char32_t cc = c+i; |
| 110 | + row->add_child(NAltTextElement::create((int)cc)); |
| 111 | + } |
| 112 | + grid->add_child(row); |
| 113 | + } |
| 114 | + return grid; |
| 115 | +} |
| 116 | + |
| 117 | +static NElement::ptr character_grid(NWindow::ptr parentWindow) |
| 118 | +{ |
| 119 | + NVerticalStackElement::ptr grid = NVerticalStackElement::create(); |
| 120 | + for (char32_t c = 0x20; c < 0x100; c += 16) |
| 121 | + { |
| 122 | + NHorizontalStackElement::ptr row = NHorizontalStackElement::create(); |
| 123 | + for (int i = 0; i < 16; i++) |
| 124 | + { |
| 125 | + char32_t cc = c+i; |
| 126 | + if (cc == 0x7F) cc = 0xFFFD; |
| 127 | + if (wcwidth(cc) == -1) |
| 128 | + { |
| 129 | + cc = 0xFFFD; |
| 130 | + } |
| 131 | + row->add_child(NTextElement::create(u32string_to_utf8(std::u32string(1, cc)))); |
| 132 | + } |
| 133 | + grid->add_child(row); |
| 134 | + } |
| 135 | + return grid; |
| 136 | +} |
| 137 | + |
| 138 | +static NElement::ptr device_grid(NWindow::ptr parentWindow) |
| 139 | +{ |
| 140 | + NVerticalStackElement::ptr grid = NVerticalStackElement::create(); |
| 141 | + for (char32_t c = 0xF000; c < 0xF100; c += 16) // special range for device characters on EGA/VGA devices. |
| 142 | + { |
| 143 | + NHorizontalStackElement::ptr row = NHorizontalStackElement::create(); |
| 144 | + for (int i = 0; i < 16; i++) |
| 145 | + { |
| 146 | + char32_t cc = c+i; |
| 147 | + if (cc == 0x7F) cc = 0xFFFD; |
| 148 | + if (wcwidth(cc) == -1) |
| 149 | + { |
| 150 | + cc = 0xFFFD; |
| 151 | + } |
| 152 | + row->add_child(NTextElement::create(u32string_to_utf8(std::u32string(1, cc)))); |
| 153 | + } |
| 154 | + grid->add_child(row); |
| 155 | + } |
| 156 | + return grid; |
| 157 | +} |
| 158 | + |
| 159 | +void ascii_fallback_test_window(NWindow::ptr parentWindow /* = nullptr */) |
| 160 | +{ |
| 161 | + NWindow::ptr window = NWindow::create(parentWindow, 66, AUTO_SIZE); |
| 162 | + NTextElement::ptr keyIndicator; |
| 163 | + |
| 164 | + std::string oGrave = u32string_to_utf8(U"o\u0300"); |
| 165 | + NColorPair boundsColor; |
| 166 | + if (parentWindow && parentWindow->max_color_pairs() > 8) { |
| 167 | + boundsColor = window->make_color_pair(0xC0C0C0, 0x4040C0); |
| 168 | + } |
| 169 | + else { |
| 170 | + boundsColor = window->make_color_pair(0xE00000, 0x000000); |
| 171 | + } |
| 172 | + window |
| 173 | + | title("ASCII Fallback Test") |
| 174 | + | add_child( |
| 175 | + NVerticalStackElement::create() |
| 176 | + | margin({ 2,1,2,1 }) |
| 177 | + | row_gap(1) |
| 178 | + | add_child( |
| 179 | + NTextElement::create("Fallbacks only occur on non-Unicode terminals. ●○ ☑☐ ⏵⏷ ◉🗹□") |
| 180 | + | wrap_text() |
| 181 | + ) |
| 182 | + | add_child( |
| 183 | + NHorizontalStackElement::create() |
| 184 | + | column_gap(2) |
| 185 | + | add_child( |
| 186 | + NVerticalStackElement::create() |
| 187 | + | add_child( |
| 188 | + character_test(parentWindow, |
| 189 | + "O with Grave: ", |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + 0xF2) |
| 194 | + ) |
| 195 | + | add_child( |
| 196 | + character_test(parentWindow, |
| 197 | + "Composing accent: ", |
| 198 | + 0x301) |
| 199 | + ) |
| 200 | + | add_child( |
| 201 | + character_test(parentWindow, |
| 202 | + "Smile Emjoi: ", |
| 203 | + 0x1F60A |
| 204 | + ) |
| 205 | + ) |
| 206 | + | add_child( |
| 207 | + character_test(parentWindow, |
| 208 | + "Checkbox: ", |
| 209 | + U'☑') |
| 210 | + ) |
| 211 | + | add_child( |
| 212 | + character_test(parentWindow, |
| 213 | + "Checkbox: ", |
| 214 | + U'☐') |
| 215 | + ) |
| 216 | + | add_child( |
| 217 | + character_test(parentWindow, |
| 218 | + "RadioButton: ", |
| 219 | + U'●') |
| 220 | + ) |
| 221 | + ) |
| 222 | + | add_child( |
| 223 | + NVerticalStackElement::create() |
| 224 | + | alignment(NAlignment::Start) |
| 225 | + | add_child(NCheckboxElement::create("Checkbox", true)) |
| 226 | + | add_child(NCheckboxElement::create("Checkbox", false)) |
| 227 | + | add_child(NRadioGroupElement::create( |
| 228 | + NOrientation::Vertical, |
| 229 | + { "Radio 1", "Radio 2", "Radio 3" }, 1)) |
| 230 | + ) |
| 231 | + ) |
| 232 | + | add_child( |
| 233 | + NHorizontalStackElement::create() |
| 234 | + | column_gap(3) |
| 235 | + | add_child(character_grid(parentWindow)) |
| 236 | + | add_child(acs_grid(parentWindow)) |
| 237 | + | add_child(device_grid(parentWindow)) |
| 238 | + ) |
| 239 | + | add_child( |
| 240 | + NHorizontalStackElement::create() |
| 241 | + | alignment(NAlignment::End) |
| 242 | + | add_child( |
| 243 | + NButtonElement::create("OK") |
| 244 | + //| is_default() |
| 245 | + | width(15) |
| 246 | + | on_clicked([windowRef = window->weak_ptr()](NMouseButton button, NClickedEventArgs& args) mutable { |
| 247 | + args.handled = true; |
| 248 | + windowRef.lock()->close(); |
| 249 | + }) |
| 250 | + ) |
| 251 | + ) |
| 252 | + ) |
| 253 | + ; |
| 254 | + |
| 255 | +} |
| 256 | + |
0 commit comments