Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/dnd.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: 22 июл. 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/common/debug.h>
23 #include <lsp-plug.in/io/OutMemoryStream.h>
24 #include <lsp-plug.in/test-fw/mtest.h>
25 #include <lsp-plug.in/ws/factory.h>
26 #include <lsp-plug.in/ws/IEventHandler.h>
27
28 namespace
29 {
30 static const char * const accept_mime[] =
31 {
32 "text/uri-list",
33 "text/x-moz-url",
34 "application/x-kde4-urilist",
35 "UTF8_STRING",
36 "text/plain",
37 "text/plain;charset=utf-8",
38 "application/x-windows-filenamew",
39 "application/x-windows-filename",
40 NULL
41 };
42 }
43
44 MTEST_BEGIN("ws.display", dnd)
45
46 class DragInSink: public ws::IDataSink
47 {
48 protected:
49 enum ctype_t
50 {
51 TEXT_URI_LIST,
52 TEXT_X_MOZ_URL,
53 APPLICATION_X_KDE4_URILIST,
54 TEXT_PLAIN
55 };
56
57 protected:
58 test_type_t *pTest;
59 io::OutMemoryStream sOS;
60 ssize_t nCtype;
61
62 protected:
63 ssize_t get_mime_index(const char *mime);
64
65 public:
66 explicit DragInSink(test_type_t *test)
67 {
68 pTest = test;
69 nCtype = -1;
70 }
71
72 virtual ~DragInSink()
73 {
74 }
75
76 public:
77 ssize_t select_mime_type(const char * const *mime_types)
78 {
79 nCtype = 0;
80 for (const char * const *p = accept_mime; *p != NULL; ++p, ++nCtype)
81 {
82 ssize_t idx = 0;
83 for (const char * const *q = mime_types; *q != NULL; ++q, ++idx)
84 {
85 if (!::strcasecmp(*p, *q))
86 return idx;
87 }
88 }
89 return -1;
90 }
91
92 public:
93 virtual ssize_t open(const char * const *mime_types) override
94 {
95 ssize_t ctype = select_mime_type(mime_types);
96 if (ctype < 0)
97 return -STATUS_UNSUPPORTED_FORMAT;
98
99 sOS.clear();
100 return ctype;
101 }
102
103 virtual status_t write(const void *buf, size_t count) override
104 {
105 wssize_t written = sOS.write(buf, count);
106 if (written < 0)
107 return status_t(-written);
108 return (written == wssize_t(count)) ? STATUS_OK : STATUS_UNKNOWN_ERR;
109 }
110
111 virtual status_t close(status_t code) override
112 {
113 if (code != STATUS_OK)
114 {
115 pTest->printf("Failed drop: code=%d\n", int(code));
116 return code;
117 }
118
119 pTest->printf("Received content type: %s\n", accept_mime[nCtype]);
120 lsp_dumpb("Dump", sOS.data(), sOS.size());
121
122 return STATUS_OK;
123 }
124 };
125
126 class Handler: public ws::IEventHandler
127 {
128 private:
129 test_type_t *pTest;
130 ws::IWindow *pWnd;
131 bool bDragOn;
132 DragInSink *pSink;
133
134 public:
135 inline Handler(test_type_t *test, ws::IWindow *wnd)
136 {
137 pTest = test;
138 pWnd = wnd;
139 bDragOn = false;
140 pSink = new DragInSink(pTest);
141 pSink->acquire();
142 }
143
144 virtual ~Handler()
145 {
146 if (pSink != NULL)
147 {
148 pSink->release();
149 pSink = NULL;
150 }
151 }
152
153 public:
154 virtual status_t handle_event(const ws::event_t *ev) override
155 {
156 switch (ev->nType)
157 {
158 case ws::UIE_DRAG_ENTER:
159 {
160 pTest->printf("DRAG_ENTER\n");
161 bDragOn = true;
162 pWnd->invalidate();
163 break;
164 }
165
166 case ws::UIE_DRAG_REQUEST:
167 {
168 pTest->printf("DRAG_REQUEST x=%d y=%d\n", int(ev->nLeft), int(ev->nTop));
169 ws::IDisplay *dpy = pWnd->display();
170
171 const char * const *ctype = dpy->get_drag_ctypes();
172 for (const char * const *t = ctype; (t != NULL) && (*t != NULL); ++t)
173 pTest->printf(" %s\n", *t);
174
175 ssize_t w = pWnd->width() / 4;
176 ssize_t h = pWnd->height() / 4;
177
178 if ((ev->nLeft >= w) && (ev->nLeft < w*3) &&
179 (ev->nTop >= h) && (ev->nTop < h*3))
180 {
181 ssize_t idx = pSink->select_mime_type(ctype);
182 if (idx >= 0)
183 {
184 ws::rectangle_t r;
185 r.nLeft = w;
186 r.nTop = h;
187 r.nWidth = w*2;
188 r.nHeight = h*2;
189
190 dpy->accept_drag(pSink, ws::DRAG_COPY, &r);
191 pTest->printf("Accepted drag of %s\n", ctype[idx]);
192 break;
193 }
194 }
195
196 dpy->reject_drag();
197 pTest->printf("Rejected drag\n");
198 break;
199 }
200
201 case ws::UIE_DRAG_LEAVE:
202 {
203 pTest->printf("DRAG_LEAVE\n");
204 bDragOn = false;
205 pWnd->invalidate();
206 break;
207 }
208
209 // Redraw event
210 case ws::UIE_REDRAW:
211 {
212 Color c(0.0f, 0.0f, 0.0f);
213 ws::ISurface *s = pWnd->get_surface();
214 if (s == NULL)
215 return STATUS_OK;
216
217 size_t w = pWnd->width() / 4;
218 size_t h = pWnd->height() / 4;
219
220 // Perform drawing
221 s->begin();
222 s->clear(c);
223
224 // Clear with blue
225 c.set_rgb24(0x0088cc);
226 s->clear(c);
227
228 // Draw the Drag & Drop area
229 c.set_rgb24((bDragOn) ? 0xffee00 : 0xcc8800);
230 s->fill_rect(c, SURFMASK_NONE, 0.0f, w, h, w*2, h*2);
231
232 s->end();
233
234 return STATUS_OK;
235 }
236
237 case ws::UIE_CLOSE:
238 {
239 pTest->printf("CLOSE\n");
240
241 pWnd->hide();
242 pWnd->display()->quit_main();
243 break;
244 }
245
246 default:
247 return IEventHandler::handle_event(ev);
248 }
249
250 return STATUS_OK;
251 }
252 };
253
254 MTEST_MAIN
255 {
256 ws::IDisplay *dpy = ws::create_display(0, NULL);
257 MTEST_ASSERT(dpy != NULL);
258 lsp_finally { ws::free_display(dpy); };
259
260 ws::IWindow *wnd = dpy->create_window();
261 MTEST_ASSERT(wnd != NULL);
262 lsp_finally {
263 wnd->destroy();
264 delete wnd;
265 };
266 MTEST_ASSERT(wnd->init() == STATUS_OK);
267 MTEST_ASSERT(wnd->set_caption("Test drag&drop") == STATUS_OK);
268 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
269 MTEST_ASSERT(wnd->set_window_actions(ws::WA_ALL) == STATUS_OK);
270 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
271
272 Handler h(this, wnd);
273 wnd->set_handler(&h);
274
275 MTEST_ASSERT(wnd->show() == STATUS_OK);
276 MTEST_ASSERT(dpy->main() == STATUS_OK);
277 }
278
279 MTEST_END
280
281
282
283
284