Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/srectangles.cpp

raw
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: 1 апр. 2025 г.
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/ws/IEventHandler.h>
24 #include <lsp-plug.in/test-fw/mtest.h>
25
26 MTEST_BEGIN("ws.display", srectangles)
27
28 class Handler: public ws::IEventHandler
29 {
30 private:
31 test_type_t *pTest;
32 ws::IWindow *pWnd;
33 bool bAntiAlias;
34
35 public:
36 inline Handler(test_type_t *test, ws::IWindow *wnd)
37 {
38 pTest = test;
39 pWnd = wnd;
40 bAntiAlias = true;
41 }
42
43 virtual status_t handle_event(const ws::event_t *ev)
44 {
45 switch (ev->nType)
46 {
47 case ws::UIE_REDRAW:
48 {
49 ws::ISurface *s = pWnd->get_surface();
50 if (s == NULL)
51 return STATUS_OK;
52
53 s->begin();
54 {
55 // Prepare source surface
56 ws::ISurface *src = s->create(64, 64);
57 if (src == NULL)
58 return STATUS_OK;
59 lsp_finally {
60 src->destroy();
61 delete src;
62 };
63
64 // Draw gradient on a source surface
65 {
66 ws::IGradient *g = src->radial_gradient(32.0f, 32.0f, 32.0f, 32.0f, 48.0f);
67 if (g == NULL)
68 return STATUS_OK;
69 lsp_finally { delete g; };
70
71 Color c;
72 g->set_start_rgb(0xff00ff);
73 g->set_stop_rgb(0x00ffff);
74 src->begin();
75 {
76 src->fill_rect(g, SURFMASK_NO_CORNER, 0, 0.0f, 0.0f, 64.0f, 64.0f);
77 }
78 src->end();
79 }
80
81 // Perform drawing
82 Color c(0.0f, 0.5f, 0.75f);
83
84 s->clear(c);
85 s->set_antialiasing(bAntiAlias);
86
87 s->draw(src, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
88
89 for (size_t xi=0; xi<4; ++xi)
90 for (size_t yi=0; yi<4; ++yi)
91 {
92 const size_t mask = (yi << 2) | xi;
93
94 size_t x = 76 * (xi + 1);
95 size_t y = 76 * (yi + 1);
96
97 s->fill_rect(
98 src, xi * 0.125f,
99 mask, 12.0f,
100 x, y,
101 src->width(), src->height());
102 }
103
104 }
105 s->end();
106
107 return STATUS_OK;
108 }
109
110 case ws::UIE_MOUSE_CLICK:
111 {
112 // Left mouse button toggles anti-aliasing
113 if (ev->nCode == ws::MCB_LEFT)
114 {
115 bAntiAlias = !bAntiAlias;
116 pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
117 pWnd->invalidate();
118 }
119 break;
120 }
121
122 case ws::UIE_CLOSE:
123 {
124 pWnd->hide();
125 pWnd->display()->quit_main();
126 break;
127 }
128
129 default:
130 return IEventHandler::handle_event(ev);
131 }
132
133 return STATUS_OK;
134 }
135 };
136
137 MTEST_MAIN
138 {
139 ws::IDisplay *dpy = ws::create_display(0, NULL);
140 MTEST_ASSERT(dpy != NULL);
141 lsp_finally { ws::free_display(dpy); };
142
143 ws::IWindow *wnd = dpy->create_window();
144 MTEST_ASSERT(wnd != NULL);
145 lsp_finally {
146 wnd->destroy();
147 delete wnd;
148 };
149
150 MTEST_ASSERT(wnd->init() == STATUS_OK);
151 MTEST_ASSERT(wnd->set_caption("Test surface rectangles") == STATUS_OK);
152 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
153 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
154 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
155
156 Handler h(this, wnd);
157 wnd->set_handler(&h);
158
159 MTEST_ASSERT(wnd->show() == STATUS_OK);
160
161 MTEST_ASSERT(dpy->main() == STATUS_OK);
162 }
163
164 MTEST_END
165
166
167