Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/frames.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: 7 июл. 2022 г.
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 static const uint32_t colors[9] =
27 {
28 0xff0000,
29 0x00ff00,
30 0x0000ff,
31 0xffff00,
32 0xff00ff,
33 0x00ffff,
34 0xffcc00,
35 0xcc00ff,
36 0x00ccff
37 };
38
39 MTEST_BEGIN("ws.display", frames)
40
41 class Handler: public ws::IEventHandler
42 {
43 private:
44 test_type_t *pTest;
45 ws::IWindow *pWnd;
46 bool bAntiAlias;
47
48 public:
49 inline Handler(test_type_t *test, ws::IWindow *wnd)
50 {
51 pTest = test;
52 pWnd = wnd;
53 bAntiAlias = true;
54 }
55
56 virtual status_t handle_event(const ws::event_t *ev)
57 {
58 switch (ev->nType)
59 {
60 case ws::UIE_REDRAW:
61 {
62 Color c(0.0f, 0.0f, 0.0f);
63 ws::ISurface *s = pWnd->get_surface();
64 if (s == NULL)
65 return STATUS_OK;
66
67 // Perform drawing
68 s->begin();
69 s->clear(c);
70 s->set_antialiasing(bAntiAlias);
71
72 ws::rectangle_t r1, r2;
73 r1.nWidth = 104;
74 r1.nHeight = 64;
75 r2.nWidth = r1.nWidth / 2;
76 r2.nHeight = r1.nHeight / 2;
77
78 // Method 1: fill frame using coordinates
79 for (size_t y=0; y<3; ++y)
80 for (size_t x=0; x<3; ++x)
81 {
82 c.set_rgb24(colors[y*3 + x]);
83 r1.nLeft = 4 + x * r1.nWidth;
84 r1.nTop = 4 + y * r1.nHeight;
85 r2.nLeft = r1.nLeft + (x * r2.nWidth) / 2;
86 r2.nTop = r1.nTop + (y * r2.nHeight) / 2;
87
88 s->fill_frame(
89 c, SURFMASK_NONE, 0.0f,
90 r1.nLeft, r1.nTop, r1.nWidth, r1.nHeight,
91 r2.nLeft, r2.nTop, r2.nWidth, r2.nHeight);
92 }
93
94 // Method 2: fill frame using rectangles
95 for (size_t y=0; y<3; ++y)
96 for (size_t x=0; x<3; ++x)
97 {
98 c.set_rgb24(colors[y*3 + x]);
99 r1.nLeft = 324 + x * r1.nWidth;
100 r1.nTop = 4 + y * r1.nHeight;
101 r2.nLeft = r1.nLeft + (x * r2.nWidth) / 2;
102 r2.nTop = r1.nTop + (y * r2.nHeight) / 2;
103
104 s->fill_frame(c, SURFMASK_NONE, 0.0f, &r1, &r2);
105 }
106
107 // Method 3: fill round frame using coordinates
108 size_t mask = 0;
109 for (size_t y=0; y<3; ++y)
110 for (size_t x=0; x<3; ++x)
111 {
112 c.set_rgb24(colors[y*3 + x]);
113 r1.nLeft = 4 + x * r1.nWidth;
114 r1.nTop = 204 + y * r1.nHeight;
115 r2.nLeft = r1.nLeft + (x * r2.nWidth) / 2;
116 r2.nTop = r1.nTop + (y * r2.nHeight) / 2;
117
118 s->fill_frame(
119 c, mask, 12,
120 r1.nLeft, r1.nTop, r1.nWidth, r1.nHeight,
121 r2.nLeft, r2.nTop, r2.nWidth, r2.nHeight);
122
123 ++mask;
124 }
125
126 // Method 4: fill round frame using coordinates
127 for (size_t y=0; y<3; ++y)
128 for (size_t x=0; x<3; ++x)
129 {
130 c.set_rgb24(colors[y*3 + x]);
131 r1.nLeft = 324 + x * r1.nWidth;
132 r1.nTop = 204 + y * r1.nHeight;
133 r2.nLeft = r1.nLeft + (x * r2.nWidth) / 2;
134 r2.nTop = r1.nTop + (y * r2.nHeight) / 2;
135
136 s->fill_frame(c, mask, 12, &r1, &r2);
137
138 ++mask;
139 }
140
141 s->end();
142
143 return STATUS_OK;
144 }
145
146 case ws::UIE_MOUSE_CLICK:
147 {
148 // Left mouse button toggles anti-aliasing
149 if (ev->nCode == ws::MCB_LEFT)
150 {
151 bAntiAlias = !bAntiAlias;
152 pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
153 pWnd->invalidate();
154 }
155 break;
156 }
157
158 case ws::UIE_CLOSE:
159 {
160 pWnd->hide();
161 pWnd->display()->quit_main();
162 break;
163 }
164
165 default:
166 return IEventHandler::handle_event(ev);
167 }
168
169 return STATUS_OK;
170 }
171 };
172
173 MTEST_MAIN
174 {
175 ws::IDisplay *dpy = ws::create_display(0, NULL);
176 MTEST_ASSERT(dpy != NULL);
177 lsp_finally { ws::free_display(dpy); };
178
179 ws::IWindow *wnd = dpy->create_window();
180 MTEST_ASSERT(wnd != NULL);
181 lsp_finally {
182 wnd->destroy();
183 delete wnd;
184 };
185
186 MTEST_ASSERT(wnd->init() == STATUS_OK);
187 MTEST_ASSERT(wnd->set_caption("Test frames") == STATUS_OK);
188 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
189 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
190 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
191
192 Handler h(this, wnd);
193 wnd->set_handler(&h);
194
195 MTEST_ASSERT(wnd->show() == STATUS_OK);
196
197 MTEST_ASSERT(dpy->main() == STATUS_OK);
198 }
199
200 MTEST_END
201
202
203