|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 | + * (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 | + * |
| 5 | + * This file is part of lsp-tk-lib |
| 6 | + * Created on: 5 мая 2020 г. |
| 7 | + * |
| 8 | + * lsp-tk-lib is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * any later version. |
| 12 | + * |
| 13 | + * lsp-tk-lib is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <lsp-plug.in/test-fw/mtest.h> |
| 23 | +#include <lsp-plug.in/tk/tk.h> |
| 24 | + |
| 25 | +MTEST_BEGIN("tk.sys", win_actions) |
| 26 | + |
| 27 | + static status_t slot_close(tk::Widget *sender, void *ptr, void *data) |
| 28 | + { |
| 29 | + sender->display()->quit_main(); |
| 30 | + return STATUS_OK; |
| 31 | + } |
| 32 | + |
| 33 | + static status_t slot_key_up(tk::Widget *sender, void *ptr, void *data) |
| 34 | + { |
| 35 | + tk::Window *wnd = tk::widget_cast<tk::Window>(sender); |
| 36 | + ws::event_t *ev = static_cast<ws::event_t *>(data); |
| 37 | + test_type_t *_this = static_cast<test_type_t *>(ptr); |
| 38 | + |
| 39 | + if ((wnd != NULL) && (ev != NULL) && (ev->nType == ws::UIE_KEY_UP)) |
| 40 | + { |
| 41 | + _this->printf("Key up: %c (0x%x)\n", (char)ev->nCode, int(ev->nCode)); |
| 42 | + |
| 43 | + if ((ev->nCode == '+') || (ev->nCode == ws::WSK_KEYPAD_ADD)) |
| 44 | + wnd->style()->schema()->scaling()->add(0.25f); |
| 45 | + else if ((ev->nCode == '-') || (ev->nCode == ws::WSK_KEYPAD_SUBTRACT)) |
| 46 | + wnd->style()->schema()->scaling()->sub(0.25f); |
| 47 | + } |
| 48 | + return STATUS_OK; |
| 49 | + } |
| 50 | + |
| 51 | + static status_t slot_state(tk::Widget *sender, void *ptr, void *data) |
| 52 | + { |
| 53 | + tk::Window *wnd = tk::widget_cast<tk::Window>(sender); |
| 54 | + test_type_t *_this = static_cast<test_type_t *>(ptr); |
| 55 | + |
| 56 | + _this->printf("Window state is now: %d\n", int(wnd->state()->get())); |
| 57 | + return STATUS_OK; |
| 58 | + } |
| 59 | + |
| 60 | + static status_t slot_minimize(tk::Widget *sender, void *ptr, void *data) |
| 61 | + { |
| 62 | + tk::Window *wnd = static_cast<tk::Window *>(ptr); |
| 63 | + wnd->state()->set_minimized(); |
| 64 | + return STATUS_OK; |
| 65 | + } |
| 66 | + |
| 67 | + static status_t slot_maximize(tk::Widget *sender, void *ptr, void *data) |
| 68 | + { |
| 69 | + tk::Window *wnd = static_cast<tk::Window *>(ptr); |
| 70 | + wnd->state()->set_maximized(); |
| 71 | + return STATUS_OK; |
| 72 | + } |
| 73 | + |
| 74 | + static status_t slot_restore(tk::Widget *sender, void *ptr, void *data) |
| 75 | + { |
| 76 | + tk::Window *wnd = static_cast<tk::Window *>(ptr); |
| 77 | + wnd->state()->set_normal(); |
| 78 | + return STATUS_OK; |
| 79 | + } |
| 80 | + |
| 81 | + MTEST_MAIN |
| 82 | + { |
| 83 | + tk::Display *dpy = new tk::Display(); |
| 84 | + MTEST_ASSERT(dpy != NULL); |
| 85 | + lsp_finally { delete dpy; }; |
| 86 | + |
| 87 | + MTEST_ASSERT(dpy->init(0, NULL) == STATUS_OK); |
| 88 | + |
| 89 | + // Create widgets |
| 90 | + tk::Window *wnd = new tk::Window(dpy); |
| 91 | + lsp_finally { |
| 92 | + wnd->destroy(); |
| 93 | + delete wnd; |
| 94 | + }; |
| 95 | + tk::Box *box = new tk::Box(dpy); |
| 96 | + lsp_finally { |
| 97 | + box->destroy(); |
| 98 | + delete box; |
| 99 | + }; |
| 100 | + tk::Button *minimize = new tk::Button(dpy); |
| 101 | + lsp_finally { |
| 102 | + minimize->destroy(); |
| 103 | + delete minimize; |
| 104 | + }; |
| 105 | + tk::Button *maximize = new tk::Button(dpy); |
| 106 | + lsp_finally { |
| 107 | + maximize->destroy(); |
| 108 | + delete maximize; |
| 109 | + }; |
| 110 | + tk::Button *restore = new tk::Button(dpy); |
| 111 | + lsp_finally { |
| 112 | + restore->destroy(); |
| 113 | + delete restore; |
| 114 | + }; |
| 115 | + |
| 116 | + |
| 117 | + // Initialize window |
| 118 | + MTEST_ASSERT(wnd->init() == STATUS_OK); |
| 119 | + MTEST_ASSERT(wnd->title()->set_raw("Test window") == STATUS_OK); |
| 120 | + MTEST_ASSERT(wnd->role()->set_raw("test") == STATUS_OK); |
| 121 | + wnd->bg_color()->set_rgb(0, 0.75, 1.0); |
| 122 | + wnd->actions()->set_actions(ws::WA_MOVE | ws::WA_RESIZE | ws::WA_CLOSE | ws::WA_MINIMIZE | ws::WA_MAXIMIZE); |
| 123 | + wnd->border_style()->set(ws::BS_SINGLE); |
| 124 | + wnd->constraints()->set(160, 100, 640, 400); |
| 125 | + wnd->size()->set(320, 200); |
| 126 | + wnd->slot(tk::SLOT_CLOSE)->bind(slot_close, this); |
| 127 | + wnd->slot(tk::SLOT_KEY_UP)->bind(slot_key_up, this); |
| 128 | + wnd->slot(tk::SLOT_STATE)->bind(slot_state, this); |
| 129 | + wnd->layout()->set(-0.5, 0.5, 0.5, 0.5); |
| 130 | + |
| 131 | + // Initialize void widget |
| 132 | + MTEST_ASSERT(box->init() == STATUS_OK); |
| 133 | + box->orientation()->set_horizontal(); |
| 134 | + MTEST_ASSERT(wnd->add(box) == STATUS_OK); |
| 135 | + |
| 136 | + MTEST_ASSERT(minimize->init() == STATUS_OK); |
| 137 | + minimize->text()->set_raw("Minimize"); |
| 138 | + minimize->slot(tk::SLOT_SUBMIT)->bind(slot_minimize, wnd); |
| 139 | + MTEST_ASSERT(box->add(minimize) == STATUS_OK); |
| 140 | + |
| 141 | + MTEST_ASSERT(maximize->init() == STATUS_OK); |
| 142 | + maximize->text()->set_raw("Maximize"); |
| 143 | + maximize->slot(tk::SLOT_SUBMIT)->bind(slot_maximize, wnd); |
| 144 | + MTEST_ASSERT(box->add(maximize) == STATUS_OK); |
| 145 | + |
| 146 | + MTEST_ASSERT(restore->init() == STATUS_OK); |
| 147 | + restore->text()->set_raw("Restore"); |
| 148 | + restore->slot(tk::SLOT_SUBMIT)->bind(slot_restore, wnd); |
| 149 | + MTEST_ASSERT(box->add(restore) == STATUS_OK); |
| 150 | + |
| 151 | + // Show window |
| 152 | + wnd->visibility()->set(true); |
| 153 | + |
| 154 | + MTEST_ASSERT(dpy->main() == STATUS_OK); |
| 155 | + } |
| 156 | + |
| 157 | +MTEST_END |
| 158 | + |
| 159 | + |
0 commit comments