| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-ws-lib |
| 6 |
* Created on: 30 янв. 2026 г. |
| 7 |
* |
| 8 |
* lsp-ws-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-ws-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-ws-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
#include <lsp-plug.in/common/endian.h> |
| 24 |
#include <lsp-plug.in/stdlib/math.h> |
| 25 |
#include <lsp-plug.in/test-fw/mtest.h> |
| 26 |
#include <lsp-plug.in/ws/factory.h> |
| 27 |
#include <lsp-plug.in/ws/IEventHandler.h> |
| 28 |
#include <lsp-plug.in/mm/Bitmap.h> |
| 29 |
|
| 30 |
MTEST_BEGIN("ws.display", bitmap) |
| 31 |
|
| 32 |
class Handler: public ws::IEventHandler |
| 33 |
{ |
| 34 |
private: |
| 35 |
test_type_t *pTest; |
| 36 |
ws::IWindow *pWnd; |
| 37 |
mm::Bitmap sColor; |
| 38 |
mm::Bitmap sChroma; |
| 39 |
|
| 40 |
public: |
| 41 |
inline Handler(test_type_t *test, ws::IWindow *wnd) |
| 42 |
{ |
| 43 |
pTest = test; |
| 44 |
pWnd = wnd; |
| 45 |
} |
| 46 |
|
| 47 |
status_t load() |
| 48 |
{ |
| 49 |
io::Path path; |
| 50 |
status_t res = path.fmt("%s/img/lsp-logo.xpm", pTest->resources()); |
| 51 |
if (res <= 0) |
| 52 |
return STATUS_NO_MEM; |
| 53 |
|
| 54 |
pTest->printf("Loading icons from %s...\n", path.as_native()); |
| 55 |
|
| 56 |
if ((res = sColor.load(path, mm::PIXFMT_PBGRA8888, NULL)) != STATUS_OK) |
| 57 |
return res; |
| 58 |
if ((res = sChroma.load(path, mm::PIXFMT_G8, NULL)) != STATUS_OK) |
| 59 |
return res; |
| 60 |
if ((res = sChroma.convert(mm::PIXFMT_PBGRA8888)) != STATUS_OK) |
| 61 |
return res; |
| 62 |
|
| 63 |
// Make background fully transparent |
| 64 |
for (size_t r = 0; r<sChroma.rows(); ++r) |
| 65 |
{ |
| 66 |
uint8_t *row = sChroma.row(r); |
| 67 |
for (size_t c = 0; c<sChroma.columns(); ++c) |
| 68 |
{ |
| 69 |
if ((row[0] < 0x10) && (row[1] < 0x10) && (row[2] < 0x10)) |
| 70 |
{ |
| 71 |
row[0] = 0x00; |
| 72 |
row[1] = 0x00; |
| 73 |
row[2] = 0x00; |
| 74 |
row[3] = 0x00; |
| 75 |
} |
| 76 |
row += 4; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return STATUS_OK; |
| 81 |
} |
| 82 |
|
| 83 |
public: |
| 84 |
|
| 85 |
virtual status_t handle_event(const ws::event_t *ev) override |
| 86 |
{ |
| 87 |
switch (ev->nType) |
| 88 |
{ |
| 89 |
case ws::UIE_REDRAW: |
| 90 |
{ |
| 91 |
Color c(1.0f, 1.0f, 1.0f); |
| 92 |
ws::ISurface *s = pWnd->get_surface(); |
| 93 |
if (s == NULL) |
| 94 |
return STATUS_OK; |
| 95 |
|
| 96 |
// Perform drawing |
| 97 |
s->begin(); |
| 98 |
s->clear(c); |
| 99 |
lsp_finally{ s->end(); }; |
| 100 |
|
| 101 |
float x = s->width() * 0.25f; |
| 102 |
float y = s->height() * 0.5f; |
| 103 |
|
| 104 |
s->draw_raw( |
| 105 |
sColor.data(), |
| 106 |
sColor.width(), sColor.height(), sColor.stride(), |
| 107 |
x - sColor.width() * 0.5f, y - sColor.height() * 0.5f, |
| 108 |
1.0f, 1.0f, 0.0f); |
| 109 |
|
| 110 |
x = s->width() * 0.75f; |
| 111 |
|
| 112 |
s->draw_raw( |
| 113 |
sChroma.data(), |
| 114 |
sChroma.width(), sChroma.height(), sChroma.stride(), |
| 115 |
x - sChroma.width() * 0.5f, y - sChroma.height() * 0.5f, |
| 116 |
1.0f, 1.0f, 0.0f); |
| 117 |
|
| 118 |
return STATUS_OK; |
| 119 |
} |
| 120 |
|
| 121 |
case ws::UIE_CLOSE: |
| 122 |
{ |
| 123 |
pWnd->hide(); |
| 124 |
pWnd->display()->quit_main(); |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
default: |
| 129 |
return IEventHandler::handle_event(ev); |
| 130 |
} |
| 131 |
|
| 132 |
return STATUS_OK; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
MTEST_MAIN |
| 137 |
{ |
| 138 |
ws::IDisplay *dpy = ws::create_display(0, NULL); |
| 139 |
MTEST_ASSERT(dpy != NULL); |
| 140 |
lsp_finally { ws::free_display(dpy); }; |
| 141 |
|
| 142 |
ws::IWindow *wnd = dpy->create_window(); |
| 143 |
MTEST_ASSERT(wnd != NULL); |
| 144 |
lsp_finally { |
| 145 |
wnd->destroy(); |
| 146 |
delete wnd; |
| 147 |
}; |
| 148 |
|
| 149 |
MTEST_ASSERT(wnd->init() == STATUS_OK); |
| 150 |
MTEST_ASSERT(wnd->set_caption("Test bitmap drawing") == STATUS_OK); |
| 151 |
MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK); |
| 152 |
MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE | ws::WA_RESIZE) == STATUS_OK); |
| 153 |
MTEST_ASSERT(wnd->set_size_constraints(320, 200, 640, 400) == STATUS_OK); |
| 154 |
|
| 155 |
Handler h(this, wnd); |
| 156 |
wnd->set_handler(&h); |
| 157 |
MTEST_ASSERT(h.load() == STATUS_OK); |
| 158 |
|
| 159 |
MTEST_ASSERT(wnd->show() == STATUS_OK); |
| 160 |
|
| 161 |
MTEST_ASSERT(dpy->main() == STATUS_OK); |
| 162 |
} |
| 163 |
|
| 164 |
MTEST_END |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|