Debian Codegraph @

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

raw
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-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/ws/IEventHandler.h>
24 #include <lsp-plug.in/test-fw/mtest.h>
25
26 MTEST_BEGIN("ws.display", window)
27
28 class Handler: public ws::IEventHandler
29 {
30 private:
31 test_type_t *pTest;
32 ws::IWindow *pWnd;
33
34 public:
35 inline Handler(test_type_t *test, ws::IWindow *wnd)
36 {
37 pTest = test;
38 pWnd = wnd;
39 }
40
41 virtual status_t handle_event(const ws::event_t *ev)
42 {
43 switch (ev->nType)
44 {
45 case ws::UIE_MOUSE_CLICK:
46 pTest->printf("CLICK\n");
47 break;
48
49 case ws::UIE_MOUSE_DBL_CLICK:
50 pTest->printf("DBL_CLICK\n");
51 break;
52
53 case ws::UIE_MOUSE_TRI_CLICK:
54 pTest->printf("TRI_CLICK\n");
55 break;
56
57 case ws::UIE_REDRAW:
58 {
59 Color c(0.0f, 0.5f, 0.75f);
60 ws::ISurface *s = pWnd->get_surface();
61 if (s == NULL)
62 return STATUS_OK;
63
64 // Perform drawing
65 s->begin();
66 s->clear(c);
67 s->end();
68
69 return STATUS_OK;
70 }
71
72 case ws::UIE_MOUSE_MOVE:
73 {
74 size_t screen;
75 ssize_t left, top;
76
77 if (pWnd->display()->get_pointer_location(&screen, &left, &top) == STATUS_OK)
78 pTest->printf("Pointer location: local=(%d, %d), screen=(%d, %d, %d)\n",
79 int(ev->nLeft), int(ev->nTop), int(left), int(top), int(screen));
80 return STATUS_OK;
81 };
82
83 case ws::UIE_CLOSE:
84 {
85 pWnd->hide();
86 pWnd->display()->quit_main();
87 break;
88 }
89
90 case ws::UIE_STATE:
91 {
92 pTest->printf("Current window state is: %d\n", int(ev->nCode));
93 break;
94 }
95
96 default:
97 return IEventHandler::handle_event(ev);
98 }
99
100 return STATUS_OK;
101 }
102 };
103
104 MTEST_MAIN
105 {
106 ws::IDisplay *dpy = ws::create_display(0, NULL);
107 MTEST_ASSERT(dpy != NULL);
108 lsp_finally { ws::free_display(dpy); };
109
110 // Enumerate list of displays
111 printf("List of attached displays:\n");
112 printf(" %2s %10s %10s %s %s\n", "id", "coord", "size", "p", "name");
113 size_t count;
114 const ws::MonitorInfo *mi = dpy->enum_monitors(&count);
115 MTEST_ASSERT(count > 0);
116 MTEST_ASSERT(mi != NULL);
117 for (size_t i=0; i < count; ++i, ++mi)
118 {
119 char pos[32], size[32];
120 snprintf(pos, sizeof(pos), "%d,%d", int(mi->rect.nLeft), int(mi->rect.nTop));
121 snprintf(size, sizeof(size), "%dx%d", int(mi->rect.nWidth), int(mi->rect.nHeight));
122 printf(" %2d %10s %10s %c %s\n",
123 int(i), pos, size, (mi->primary) ? '*' : ' ', mi->name.get_native());
124 }
125 printf("\n");
126
127 // Obtain the work area on the primary display
128 ws::rectangle_t r;
129 MTEST_ASSERT(dpy->work_area_geometry(&r) == STATUS_OK);
130 printf("Work area on the primary display:\n");
131 printf(" (%d,%d): %dx%d\n", int(r.nLeft), int(r.nTop), int(r.nWidth), int(r.nHeight));
132
133 // Create window and do the stuff
134 ws::IWindow *wnd = dpy->create_window();
135 MTEST_ASSERT(wnd != NULL);
136 lsp_finally {
137 wnd->destroy();
138 delete wnd;
139 };
140 MTEST_ASSERT(wnd->init() == STATUS_OK);
141 MTEST_ASSERT(wnd->set_mouse_pointer(ws::MP_HAND) == STATUS_OK);
142 MTEST_ASSERT(wnd->get_mouse_pointer() == ws::MP_HAND);
143
144 LSPString dst;
145 MTEST_ASSERT(wnd->set_caption("Test window") == STATUS_OK);
146 MTEST_ASSERT(wnd->get_caption(&dst) == STATUS_OK);
147 MTEST_ASSERT(dst.equals_ascii("Test window"));
148 MTEST_ASSERT(wnd->set_border_style(ws::BS_SIZEABLE) == STATUS_OK);
149 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_RESIZE | ws::WA_CLOSE | ws::WA_MAXIMIZE | ws::WA_MINIMIZE) == STATUS_OK);
150
151 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
152 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
153
154 size_t screen = wnd->screen();
155 ssize_t sw, sh;
156 ws::rectangle_t wr;
157 MTEST_ASSERT(dpy->screen_size(screen, &sw, &sh) == STATUS_OK);
158 MTEST_ASSERT(wnd->get_absolute_geometry(&wr) == STATUS_OK);
159 wnd->move((sw - wr.nWidth) / 2, (sh - wr.nHeight) / 2);
160
161 Handler h(this, wnd);
162 wnd->set_handler(&h);
163
164 MTEST_ASSERT(wnd->show() == STATUS_OK);
165
166 MTEST_ASSERT(dpy->main() == STATUS_OK);
167 }
168
169 MTEST_END
170
171