| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-ws-lib |
| 6 |
* Created on: 5 мая 2020 г. |
| 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 |
#include <lsp-plug.in/ws/factory.h> |
| 23 |
#include <lsp-plug.in/common/debug.h> |
| 24 |
#include <lsp-plug.in/ws/IEventHandler.h> |
| 25 |
#include <lsp-plug.in/test-fw/mtest.h> |
| 26 |
|
| 27 |
MTEST_BEGIN("ws.display", font) |
| 28 |
|
| 29 |
class Handler: public ws::IEventHandler |
| 30 |
{ |
| 31 |
private: |
| 32 |
test_type_t *pTest; |
| 33 |
ws::IWindow *pWnd; |
| 34 |
|
| 35 |
public: |
| 36 |
inline Handler(test_type_t *test, ws::IWindow *wnd) |
| 37 |
{ |
| 38 |
pTest = test; |
| 39 |
pWnd = wnd; |
| 40 |
} |
| 41 |
|
| 42 |
virtual status_t handle_event(const ws::event_t *ev) |
| 43 |
{ |
| 44 |
switch (ev->nType) |
| 45 |
{ |
| 46 |
case ws::UIE_REDRAW: |
| 47 |
{ |
| 48 |
Color c(0.0f, 0.5f, 0.75f); |
| 49 |
ws::ISurface *s = pWnd->get_surface(); |
| 50 |
if (s == NULL) |
| 51 |
return STATUS_OK; |
| 52 |
|
| 53 |
// Perform drawing |
| 54 |
s->begin(); |
| 55 |
s->clear(c); |
| 56 |
|
| 57 |
ws::Font f; |
| 58 |
ws::font_parameters_t fp; |
| 59 |
ws::text_parameters_t tp; |
| 60 |
f.set_name("example"); |
| 61 |
f.set_size(64); |
| 62 |
|
| 63 |
s->get_font_parameters(f, &fp); |
| 64 |
s->get_text_parameters(f, &tp, "A"); |
| 65 |
|
| 66 |
ssize_t x = (pWnd->width() - ssize_t(tp.Width)*2) / 2; |
| 67 |
ssize_t y = (pWnd->height() - ssize_t(fp.Height)) / 2; |
| 68 |
|
| 69 |
float tx, ty; |
| 70 |
|
| 71 |
c.set_rgb24(0xffff00); |
| 72 |
f.set_antialiasing(ws::FA_ENABLED); |
| 73 |
tx = x + tp.XBearing; |
| 74 |
ty = y + fp.Ascent; |
| 75 |
s->out_text(f, c, tx, ty, "A"); |
| 76 |
x += tp.Width; |
| 77 |
|
| 78 |
// c.set_rgb24(0xcccccc); |
| 79 |
// s->line(c, tx - 8, ty, tx + 8, ty, 1.0f); |
| 80 |
// s->line(c, tx, ty - 8, tx, ty + 8, 1.0f); |
| 81 |
|
| 82 |
c.set_rgb24(0x00ffff); |
| 83 |
f.set_name("alias"); |
| 84 |
f.set_antialiasing(ws::FA_DISABLED); |
| 85 |
tx = x + tp.XBearing; |
| 86 |
ty = y + fp.Ascent; |
| 87 |
s->out_text(f, c, tx, ty, "A"); |
| 88 |
|
| 89 |
// c.set_rgb24(0xcccccc); |
| 90 |
// s->line(c, tx - 8, ty, tx + 8, ty, 1.0f); |
| 91 |
// s->line(c, tx, ty - 8, tx, ty + 8, 1.0f); |
| 92 |
|
| 93 |
c.set_rgb24(0xffffff); |
| 94 |
s->wire_rect(c, SURFMASK_NONE, 0.0f, 8, 8, pWnd->width() - 16, pWnd->height() - 16, 1.0f); |
| 95 |
|
| 96 |
s->end(); |
| 97 |
|
| 98 |
return STATUS_OK; |
| 99 |
} |
| 100 |
|
| 101 |
case ws::UIE_CLOSE: |
| 102 |
{ |
| 103 |
pWnd->hide(); |
| 104 |
pWnd->display()->quit_main(); |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
default: |
| 109 |
return IEventHandler::handle_event(ev); |
| 110 |
} |
| 111 |
|
| 112 |
return STATUS_OK; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
MTEST_MAIN |
| 117 |
{ |
| 118 |
ws::IDisplay *dpy = ws::create_display(0, NULL); |
| 119 |
MTEST_ASSERT(dpy != NULL); |
| 120 |
lsp_finally { ws::free_display(dpy); }; |
| 121 |
|
| 122 |
io::Path font; |
| 123 |
MTEST_ASSERT(font.fmt("%s/font/example.ttf", resources())); |
| 124 |
MTEST_ASSERT(dpy->add_font("example", &font) == STATUS_OK); |
| 125 |
MTEST_ASSERT(dpy->add_font_alias("alias", "example") == STATUS_OK); |
| 126 |
|
| 127 |
ws::IWindow *wnd = dpy->create_window(); |
| 128 |
MTEST_ASSERT(wnd != NULL); |
| 129 |
lsp_finally { |
| 130 |
wnd->destroy(); |
| 131 |
delete wnd; |
| 132 |
}; |
| 133 |
MTEST_ASSERT(wnd->init() == STATUS_OK); |
| 134 |
MTEST_ASSERT(wnd->set_caption("Test font") == STATUS_OK); |
| 135 |
MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK); |
| 136 |
MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_RESIZE | ws::WA_CLOSE) == STATUS_OK); |
| 137 |
MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK); |
| 138 |
|
| 139 |
Handler h(this, wnd); |
| 140 |
wnd->set_handler(&h); |
| 141 |
|
| 142 |
MTEST_ASSERT(wnd->show() == STATUS_OK); |
| 143 |
MTEST_ASSERT(dpy->main() == STATUS_OK); |
| 144 |
} |
| 145 |
|
| 146 |
MTEST_END |
| 147 |
|
| 148 |
|