1 | /*
|
---|
2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Display server window
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <gfx/bitmap.h>
|
---|
37 | #include <gfx/color.h>
|
---|
38 | #include <gfx/coord.h>
|
---|
39 | #include <gfx/context.h>
|
---|
40 | #include <gfx/render.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <io/pixelmap.h>
|
---|
43 | #include <macros.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include "client.h"
|
---|
46 | #include "display.h"
|
---|
47 | #include "window.h"
|
---|
48 |
|
---|
49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
51 | static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
52 | gfx_bitmap_alloc_t *, void **);
|
---|
53 | static errno_t ds_window_bitmap_destroy(void *);
|
---|
54 | static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
55 | static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
56 |
|
---|
57 | gfx_context_ops_t ds_window_ops = {
|
---|
58 | .set_color = ds_window_set_color,
|
---|
59 | .fill_rect = ds_window_fill_rect,
|
---|
60 | .bitmap_create = ds_window_bitmap_create,
|
---|
61 | .bitmap_destroy = ds_window_bitmap_destroy,
|
---|
62 | .bitmap_render = ds_window_bitmap_render,
|
---|
63 | .bitmap_get_alloc = ds_window_bitmap_get_alloc
|
---|
64 | };
|
---|
65 |
|
---|
66 | /** Set color on window GC.
|
---|
67 | *
|
---|
68 | * Set drawing color on window GC.
|
---|
69 | *
|
---|
70 | * @param arg Console GC
|
---|
71 | * @param color Color
|
---|
72 | *
|
---|
73 | * @return EOK on success or an error code
|
---|
74 | */
|
---|
75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
76 | {
|
---|
77 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
78 | uint16_t r, g, b;
|
---|
79 |
|
---|
80 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p",
|
---|
81 | ds_display_get_gc(wnd->display));
|
---|
82 |
|
---|
83 | gfx_color_get_rgb_i16(color, &r, &g, &b);
|
---|
84 | wnd->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
|
---|
85 |
|
---|
86 | return gfx_set_color(ds_display_get_gc(wnd->display), color);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Fill rectangle on window GC.
|
---|
90 | *
|
---|
91 | * @param arg Window GC
|
---|
92 | * @param rect Rectangle
|
---|
93 | *
|
---|
94 | * @return EOK on success or an error code
|
---|
95 | */
|
---|
96 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
97 | {
|
---|
98 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
99 | gfx_rect_t crect;
|
---|
100 | gfx_rect_t drect;
|
---|
101 | gfx_coord_t x, y;
|
---|
102 |
|
---|
103 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
104 |
|
---|
105 | gfx_rect_clip(rect, &wnd->rect, &crect);
|
---|
106 | gfx_rect_translate(&wnd->dpos, &crect, &drect);
|
---|
107 |
|
---|
108 | /* Render into the backbuffer */
|
---|
109 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
110 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
111 | pixelmap_put_pixel(&wnd->pixelmap, x - wnd->rect.p0.x,
|
---|
112 | y - wnd->rect.p0.y, wnd->color);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /* Repaint this area of the display */
|
---|
117 | return ds_display_paint(wnd->display, &drect);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Create bitmap in window GC.
|
---|
121 | *
|
---|
122 | * @param arg Window GC
|
---|
123 | * @param params Bitmap params
|
---|
124 | * @param alloc Bitmap allocation info or @c NULL
|
---|
125 | * @param rbm Place to store pointer to new bitmap
|
---|
126 | * @return EOK on success or an error code
|
---|
127 | */
|
---|
128 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
129 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
130 | {
|
---|
131 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
132 | ds_window_bitmap_t *cbm = NULL;
|
---|
133 | errno_t rc;
|
---|
134 |
|
---|
135 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
---|
136 | if (cbm == NULL)
|
---|
137 | return ENOMEM;
|
---|
138 |
|
---|
139 | rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
|
---|
140 | &cbm->bitmap);
|
---|
141 | if (rc != EOK)
|
---|
142 | goto error;
|
---|
143 |
|
---|
144 | cbm->wnd = wnd;
|
---|
145 | cbm->rect = params->rect;
|
---|
146 | *rbm = (void *)cbm;
|
---|
147 | return EOK;
|
---|
148 | error:
|
---|
149 | if (cbm != NULL)
|
---|
150 | free(cbm);
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Destroy bitmap in window GC.
|
---|
155 | *
|
---|
156 | * @param bm Bitmap
|
---|
157 | * @return EOK on success or an error code
|
---|
158 | */
|
---|
159 | static errno_t ds_window_bitmap_destroy(void *bm)
|
---|
160 | {
|
---|
161 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
162 |
|
---|
163 | gfx_bitmap_destroy(cbm->bitmap);
|
---|
164 | free(cbm);
|
---|
165 | return EOK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Render bitmap in window GC.
|
---|
169 | *
|
---|
170 | * @param bm Bitmap
|
---|
171 | * @param srect0 Source rectangle or @c NULL
|
---|
172 | * @param offs0 Offset or @c NULL
|
---|
173 | * @return EOK on success or an error code
|
---|
174 | */
|
---|
175 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
176 | gfx_coord2_t *offs0)
|
---|
177 | {
|
---|
178 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
179 | gfx_coord2_t doffs;
|
---|
180 | gfx_coord2_t offs;
|
---|
181 | gfx_rect_t srect;
|
---|
182 | gfx_rect_t swrect;
|
---|
183 | gfx_rect_t crect;
|
---|
184 | gfx_rect_t drect;
|
---|
185 | gfx_coord_t x, y;
|
---|
186 | pixelmap_t pixelmap;
|
---|
187 | gfx_bitmap_alloc_t alloc;
|
---|
188 | pixel_t pixel;
|
---|
189 | errno_t rc;
|
---|
190 |
|
---|
191 | if (srect0 != NULL) {
|
---|
192 | /* Clip source rectangle to bitmap rectangle */
|
---|
193 | gfx_rect_clip(srect0, &cbm->rect, &srect);
|
---|
194 | } else {
|
---|
195 | /* Source is entire bitmap rectangle */
|
---|
196 | srect = cbm->rect;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (offs0 != NULL) {
|
---|
200 | offs = *offs0;
|
---|
201 | } else {
|
---|
202 | offs.x = 0;
|
---|
203 | offs.y = 0;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Transform window rectangle back to bitmap coordinate system */
|
---|
207 | gfx_rect_rtranslate(&offs, &cbm->wnd->rect, &swrect);
|
---|
208 |
|
---|
209 | /* Clip so that transformed rectangle will be inside the window */
|
---|
210 | gfx_rect_clip(&srect, &swrect, &crect);
|
---|
211 |
|
---|
212 | /* Offset for rendering on screen = window pos + offs */
|
---|
213 | gfx_coord2_add(&cbm->wnd->dpos, &offs, &doffs);
|
---|
214 |
|
---|
215 | /* Resulting rectangle on the screen we are drawing into */
|
---|
216 | gfx_rect_translate(&doffs, &crect, &drect);
|
---|
217 |
|
---|
218 | rc = gfx_bitmap_get_alloc(cbm->bitmap, &alloc);
|
---|
219 | if (rc != EOK)
|
---|
220 | return rc;
|
---|
221 |
|
---|
222 | pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
|
---|
223 | pixelmap.height = cbm->rect.p1.y - cbm->rect.p0.y;
|
---|
224 | pixelmap.data = alloc.pixels;
|
---|
225 |
|
---|
226 | /* Render a copy to the backbuffer */
|
---|
227 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
228 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
229 | pixel = pixelmap_get_pixel(&pixelmap,
|
---|
230 | x - cbm->rect.p0.x, y - cbm->rect.p0.y);
|
---|
231 | pixelmap_put_pixel(&cbm->wnd->pixelmap,
|
---|
232 | x + offs.x - cbm->rect.p0.x + cbm->wnd->rect.p0.x,
|
---|
233 | y + offs.y - cbm->rect.p0.y + cbm->wnd->rect.p0.y,
|
---|
234 | pixel);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* Repaint this area of the display */
|
---|
239 | return ds_display_paint(cbm->wnd->display, &drect);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** Get allocation info for bitmap in window GC.
|
---|
243 | *
|
---|
244 | * @param bm Bitmap
|
---|
245 | * @param alloc Place to store allocation info
|
---|
246 | * @return EOK on success or an error code
|
---|
247 | */
|
---|
248 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
249 | {
|
---|
250 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
251 |
|
---|
252 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Create window.
|
---|
256 | *
|
---|
257 | * Create graphics context for rendering into a window.
|
---|
258 | *
|
---|
259 | * @param client Client owning the window
|
---|
260 | * @param params Window parameters
|
---|
261 | * @param rgc Place to store pointer to new GC.
|
---|
262 | *
|
---|
263 | * @return EOK on success or an error code
|
---|
264 | */
|
---|
265 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
---|
266 | ds_window_t **rgc)
|
---|
267 | {
|
---|
268 | ds_window_t *wnd = NULL;
|
---|
269 | gfx_context_t *gc = NULL;
|
---|
270 | gfx_context_t *dgc;
|
---|
271 | gfx_coord2_t dims;
|
---|
272 | gfx_bitmap_params_t bparams;
|
---|
273 | gfx_bitmap_alloc_t alloc;
|
---|
274 | errno_t rc;
|
---|
275 |
|
---|
276 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
277 | if (wnd == NULL) {
|
---|
278 | rc = ENOMEM;
|
---|
279 | goto error;
|
---|
280 | }
|
---|
281 |
|
---|
282 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
283 | if (rc != EOK)
|
---|
284 | goto error;
|
---|
285 |
|
---|
286 | ds_client_add_window(client, wnd);
|
---|
287 | ds_display_add_window(client->display, wnd);
|
---|
288 |
|
---|
289 | gfx_bitmap_params_init(&bparams);
|
---|
290 | bparams.rect = params->rect;
|
---|
291 |
|
---|
292 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
293 | if (dgc != NULL) {
|
---|
294 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
|
---|
295 | if (rc != EOK)
|
---|
296 | goto error;
|
---|
297 |
|
---|
298 | rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
|
---|
299 | if (rc != EOK)
|
---|
300 | goto error;
|
---|
301 |
|
---|
302 | gfx_rect_dims(¶ms->rect, &dims);
|
---|
303 | wnd->pixelmap.width = dims.x;
|
---|
304 | wnd->pixelmap.height = dims.y;
|
---|
305 | wnd->pixelmap.data = alloc.pixels;
|
---|
306 | }
|
---|
307 |
|
---|
308 | wnd->rect = params->rect;
|
---|
309 | wnd->min_size = params->min_size;
|
---|
310 | wnd->gc = gc;
|
---|
311 | wnd->cursor = wnd->display->cursor[dcurs_arrow];
|
---|
312 | *rgc = wnd;
|
---|
313 | return EOK;
|
---|
314 | error:
|
---|
315 | if (wnd != NULL) {
|
---|
316 | if (wnd->bitmap != NULL)
|
---|
317 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
318 | free(wnd);
|
---|
319 | }
|
---|
320 |
|
---|
321 | gfx_context_delete(gc);
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /** Destroy window.
|
---|
326 | *
|
---|
327 | * @param wnd Window
|
---|
328 | */
|
---|
329 | void ds_window_destroy(ds_window_t *wnd)
|
---|
330 | {
|
---|
331 | ds_display_t *disp;
|
---|
332 |
|
---|
333 | disp = wnd->display;
|
---|
334 |
|
---|
335 | ds_client_remove_window(wnd);
|
---|
336 | ds_display_remove_window(wnd);
|
---|
337 |
|
---|
338 | (void) gfx_context_delete(wnd->gc);
|
---|
339 | if (wnd->bitmap != NULL)
|
---|
340 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
341 |
|
---|
342 | free(wnd);
|
---|
343 |
|
---|
344 | (void) ds_display_paint(disp, NULL);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** Bring window to top.
|
---|
348 | *
|
---|
349 | * @param wnd Window
|
---|
350 | */
|
---|
351 | void ds_window_bring_to_top(ds_window_t *wnd)
|
---|
352 | {
|
---|
353 | ds_display_t *disp = wnd->display;
|
---|
354 |
|
---|
355 | ds_display_remove_window(wnd);
|
---|
356 | ds_display_add_window(disp, wnd);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /** Get generic graphic context from window.
|
---|
360 | *
|
---|
361 | * @param wnd Window
|
---|
362 | * @return Graphic context
|
---|
363 | */
|
---|
364 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
365 | {
|
---|
366 | return wnd->gc;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /** Paint a window using its backing bitmap.
|
---|
370 | *
|
---|
371 | * @param wnd Window to paint
|
---|
372 | * @param rect Display rectangle to paint to
|
---|
373 | * @return EOK on success or an error code
|
---|
374 | */
|
---|
375 | errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
|
---|
376 | {
|
---|
377 | gfx_rect_t srect;
|
---|
378 | gfx_rect_t *brect;
|
---|
379 | gfx_rect_t crect;
|
---|
380 |
|
---|
381 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
|
---|
382 |
|
---|
383 | if (rect != NULL) {
|
---|
384 | gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
|
---|
385 |
|
---|
386 | /* Determine if we have anything to do */
|
---|
387 | gfx_rect_clip(&srect, &wnd->rect, &crect);
|
---|
388 | if (gfx_rect_is_empty(&crect))
|
---|
389 | return EOK;
|
---|
390 |
|
---|
391 | brect = &srect;
|
---|
392 | } else {
|
---|
393 | brect = NULL;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /* This can happen in unit tests */
|
---|
397 | if (wnd->bitmap == NULL)
|
---|
398 | return EOK;
|
---|
399 |
|
---|
400 | return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
|
---|
401 | }
|
---|
402 |
|
---|
403 | /** Start moving a window by mouse drag.
|
---|
404 | *
|
---|
405 | * @param wnd Window
|
---|
406 | * @param pos Position where mouse button was pressed
|
---|
407 | */
|
---|
408 | static void ds_window_start_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
409 | {
|
---|
410 | gfx_color_t *color;
|
---|
411 | gfx_context_t *gc;
|
---|
412 | gfx_rect_t drect;
|
---|
413 | errno_t rc;
|
---|
414 |
|
---|
415 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
|
---|
416 | (int) pos->x, (int) pos->y);
|
---|
417 |
|
---|
418 | if (wnd->state != dsw_idle)
|
---|
419 | return;
|
---|
420 |
|
---|
421 | wnd->orig_pos = *pos;
|
---|
422 | wnd->state = dsw_moving;
|
---|
423 | wnd->preview_pos = wnd->dpos;
|
---|
424 |
|
---|
425 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
426 | if (rc != EOK)
|
---|
427 | return;
|
---|
428 |
|
---|
429 | gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
|
---|
430 |
|
---|
431 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
432 | if (gc != NULL) {
|
---|
433 | gfx_set_color(gc, color);
|
---|
434 | gfx_fill_rect(gc, &drect);
|
---|
435 | }
|
---|
436 |
|
---|
437 | gfx_color_delete(color);
|
---|
438 | }
|
---|
439 |
|
---|
440 | /** Finish moving a window by mouse drag.
|
---|
441 | *
|
---|
442 | * @param wnd Window
|
---|
443 | * @param pos Position where mouse button was released
|
---|
444 | */
|
---|
445 | static void ds_window_finish_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
446 | {
|
---|
447 | gfx_coord2_t dmove;
|
---|
448 | gfx_coord2_t nwpos;
|
---|
449 |
|
---|
450 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
|
---|
451 | (int) pos->x, (int) pos->y);
|
---|
452 |
|
---|
453 | if (wnd->state != dsw_moving)
|
---|
454 | return;
|
---|
455 |
|
---|
456 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
|
---|
457 |
|
---|
458 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
459 | wnd->dpos = nwpos;
|
---|
460 | wnd->state = dsw_idle;
|
---|
461 |
|
---|
462 | (void) ds_display_paint(wnd->display, NULL);
|
---|
463 | }
|
---|
464 |
|
---|
465 | /** Update window position when moving by mouse drag.
|
---|
466 | *
|
---|
467 | * @param wnd Window
|
---|
468 | * @param pos Current mouse position
|
---|
469 | */
|
---|
470 | static void ds_window_update_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
471 | {
|
---|
472 | gfx_coord2_t dmove;
|
---|
473 | gfx_coord2_t nwpos;
|
---|
474 | gfx_rect_t drect;
|
---|
475 | gfx_color_t *color;
|
---|
476 | gfx_context_t *gc;
|
---|
477 | errno_t rc;
|
---|
478 |
|
---|
479 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
|
---|
480 | (int) pos->x, (int) pos->y);
|
---|
481 |
|
---|
482 | if (wnd->state != dsw_moving)
|
---|
483 | return;
|
---|
484 |
|
---|
485 | gfx_rect_translate(&wnd->preview_pos, &wnd->rect, &drect);
|
---|
486 | ds_display_paint(wnd->display, &drect);
|
---|
487 |
|
---|
488 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
|
---|
489 |
|
---|
490 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
491 | wnd->preview_pos = nwpos;
|
---|
492 |
|
---|
493 | gfx_rect_translate(&nwpos, &wnd->rect, &drect);
|
---|
494 |
|
---|
495 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
496 | if (rc != EOK)
|
---|
497 | return;
|
---|
498 |
|
---|
499 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
500 | if (gc != NULL) {
|
---|
501 | gfx_set_color(gc, color);
|
---|
502 | gfx_fill_rect(gc, &drect);
|
---|
503 | }
|
---|
504 |
|
---|
505 | gfx_color_delete(color);
|
---|
506 | }
|
---|
507 |
|
---|
508 | /** Start resizing a window by mouse drag.
|
---|
509 | *
|
---|
510 | * @param wnd Window
|
---|
511 | * @param rsztype Resize type (which part of window is being dragged)
|
---|
512 | * @param pos Position where mouse button was pressed
|
---|
513 | */
|
---|
514 | static void ds_window_start_resize(ds_window_t *wnd,
|
---|
515 | display_wnd_rsztype_t rsztype, gfx_coord2_t *pos)
|
---|
516 | {
|
---|
517 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_resize (%d, %d)",
|
---|
518 | (int) pos->x, (int) pos->y);
|
---|
519 |
|
---|
520 | if (wnd->state != dsw_idle)
|
---|
521 | return;
|
---|
522 |
|
---|
523 | wnd->orig_pos = *pos;
|
---|
524 | wnd->state = dsw_resizing;
|
---|
525 | wnd->rsztype = rsztype;
|
---|
526 | wnd->preview_rect = wnd->rect;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Finish resizing a window by mouse drag.
|
---|
530 | *
|
---|
531 | * @param wnd Window
|
---|
532 | * @param pos Position where mouse button was released
|
---|
533 | */
|
---|
534 | static void ds_window_finish_resize(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
535 | {
|
---|
536 | gfx_coord2_t dresize;
|
---|
537 | gfx_rect_t nrect;
|
---|
538 |
|
---|
539 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_resize (%d, %d)",
|
---|
540 | (int) pos->x, (int) pos->y);
|
---|
541 |
|
---|
542 | if (wnd->state != dsw_resizing)
|
---|
543 | return;
|
---|
544 |
|
---|
545 | (void) ds_display_paint(wnd->display, NULL);
|
---|
546 |
|
---|
547 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
|
---|
548 |
|
---|
549 | /* Compute new rectangle */
|
---|
550 | ds_window_calc_resize(wnd, &dresize, &nrect);
|
---|
551 |
|
---|
552 | wnd->state = dsw_idle;
|
---|
553 | ds_client_post_resize_event(wnd->client, wnd, &nrect);
|
---|
554 | }
|
---|
555 |
|
---|
556 | /** Update window position when resizing by mouse drag.
|
---|
557 | *
|
---|
558 | * @param wnd Window
|
---|
559 | * @param pos Current mouse position
|
---|
560 | */
|
---|
561 | static void ds_window_update_resize(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
562 | {
|
---|
563 | gfx_coord2_t dresize;
|
---|
564 | gfx_rect_t nrect;
|
---|
565 | gfx_rect_t drect;
|
---|
566 | gfx_color_t *color;
|
---|
567 | gfx_context_t *gc;
|
---|
568 | errno_t rc;
|
---|
569 |
|
---|
570 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_resize (%d, %d)",
|
---|
571 | (int) pos->x, (int) pos->y);
|
---|
572 |
|
---|
573 | if (wnd->state != dsw_resizing)
|
---|
574 | return;
|
---|
575 |
|
---|
576 | gfx_rect_translate(&wnd->dpos, &wnd->preview_rect, &drect);
|
---|
577 | (void) ds_display_paint(wnd->display, &drect);
|
---|
578 |
|
---|
579 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
|
---|
580 |
|
---|
581 | ds_window_calc_resize(wnd, &dresize, &nrect);
|
---|
582 | gfx_rect_translate(&wnd->dpos, &nrect, &drect);
|
---|
583 | wnd->preview_rect = nrect;
|
---|
584 |
|
---|
585 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
586 | if (rc != EOK)
|
---|
587 | return;
|
---|
588 |
|
---|
589 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
590 | if (gc != NULL) {
|
---|
591 | gfx_set_color(gc, color);
|
---|
592 | gfx_fill_rect(gc, &drect);
|
---|
593 | }
|
---|
594 |
|
---|
595 | gfx_color_delete(color);
|
---|
596 | }
|
---|
597 |
|
---|
598 | /** Post keyboard event to window.
|
---|
599 | *
|
---|
600 | * @param wnd Window
|
---|
601 | * @param event Event
|
---|
602 | *
|
---|
603 | * @return EOK on success or an error code
|
---|
604 | */
|
---|
605 | errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
|
---|
606 | {
|
---|
607 | bool alt_or_shift;
|
---|
608 |
|
---|
609 | alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
|
---|
610 |
|
---|
611 | if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
|
---|
612 | /* On Alt-F4 or Shift-F4 send close event to the window */
|
---|
613 | ds_client_post_close_event(wnd->client, wnd);
|
---|
614 | return EOK;
|
---|
615 | }
|
---|
616 |
|
---|
617 | return ds_client_post_kbd_event(wnd->client, wnd, event);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /** Post position event to window.
|
---|
621 | *
|
---|
622 | * @param wnd Window
|
---|
623 | * @param event Position event
|
---|
624 | */
|
---|
625 | errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
|
---|
626 | {
|
---|
627 | pos_event_t tevent;
|
---|
628 | gfx_coord2_t pos;
|
---|
629 | gfx_rect_t drect;
|
---|
630 | bool inside;
|
---|
631 |
|
---|
632 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
633 | "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
|
---|
634 | (int) event->hpos, (int) event->vpos);
|
---|
635 |
|
---|
636 | pos.x = event->hpos;
|
---|
637 | pos.y = event->vpos;
|
---|
638 | gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
|
---|
639 | inside = gfx_pix_inside_rect(&pos, &drect);
|
---|
640 |
|
---|
641 | if (event->type == POS_PRESS && event->btn_num == 2 && inside)
|
---|
642 | ds_window_start_move(wnd, &pos);
|
---|
643 |
|
---|
644 | if (event->type == POS_RELEASE) {
|
---|
645 | ds_window_finish_move(wnd, &pos);
|
---|
646 | ds_window_finish_resize(wnd, &pos);
|
---|
647 | }
|
---|
648 |
|
---|
649 | if (event->type == POS_UPDATE) {
|
---|
650 | ds_window_update_move(wnd, &pos);
|
---|
651 | ds_window_update_resize(wnd, &pos);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /* Transform event coordinates to window-local */
|
---|
655 | tevent = *event;
|
---|
656 | tevent.hpos -= wnd->dpos.x;
|
---|
657 | tevent.vpos -= wnd->dpos.y;
|
---|
658 |
|
---|
659 | return ds_client_post_pos_event(wnd->client, wnd, &tevent);
|
---|
660 | }
|
---|
661 |
|
---|
662 | /** Post focus event to window.
|
---|
663 | *
|
---|
664 | * @param wnd Window
|
---|
665 | */
|
---|
666 | errno_t ds_window_post_focus_event(ds_window_t *wnd)
|
---|
667 | {
|
---|
668 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event\n");
|
---|
669 |
|
---|
670 | return ds_client_post_focus_event(wnd->client, wnd);
|
---|
671 | }
|
---|
672 |
|
---|
673 | /** Post unfocus event to window.
|
---|
674 | *
|
---|
675 | * @param wnd Window
|
---|
676 | */
|
---|
677 | errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
|
---|
678 | {
|
---|
679 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event\n");
|
---|
680 |
|
---|
681 | return ds_client_post_unfocus_event(wnd->client, wnd);
|
---|
682 | }
|
---|
683 |
|
---|
684 | /** Start moving a window, detected by client.
|
---|
685 | *
|
---|
686 | * @param wnd Window
|
---|
687 | * @param pos Position where the pointer was when the move started
|
---|
688 | * relative to the window
|
---|
689 | * @param event Button press event
|
---|
690 | */
|
---|
691 | void ds_window_move_req(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
692 | {
|
---|
693 | gfx_coord2_t orig_pos;
|
---|
694 |
|
---|
695 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_move_req (%d, %d)",
|
---|
696 | (int) pos->x, (int) pos->y);
|
---|
697 |
|
---|
698 | gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
|
---|
699 | ds_window_start_move(wnd, &orig_pos);
|
---|
700 | }
|
---|
701 |
|
---|
702 | /** Move window.
|
---|
703 | *
|
---|
704 | * @param wnd Window
|
---|
705 | */
|
---|
706 | void ds_window_move(ds_window_t *wnd, gfx_coord2_t *dpos)
|
---|
707 | {
|
---|
708 | wnd->dpos = *dpos;
|
---|
709 | (void) ds_display_paint(wnd->display, NULL);
|
---|
710 | }
|
---|
711 |
|
---|
712 | /** Start resizing a window, detected by client.
|
---|
713 | *
|
---|
714 | * @param wnd Window
|
---|
715 | * @param rsztype Resize type (which part of window is being dragged)
|
---|
716 | * @param pos Position where the pointer was when the resize started
|
---|
717 | * relative to the window
|
---|
718 | * @param event Button press event
|
---|
719 | */
|
---|
720 | void ds_window_resize_req(ds_window_t *wnd, display_wnd_rsztype_t rsztype,
|
---|
721 | gfx_coord2_t *pos)
|
---|
722 | {
|
---|
723 | gfx_coord2_t orig_pos;
|
---|
724 |
|
---|
725 | log_msg(LOG_DEFAULT, LVL_NOTE, "ds_window_resize_req (%d, %d, %d)",
|
---|
726 | (int) rsztype, (int) pos->x, (int) pos->y);
|
---|
727 |
|
---|
728 | gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
|
---|
729 | ds_window_start_resize(wnd, rsztype, &orig_pos);
|
---|
730 | }
|
---|
731 |
|
---|
732 | /** Resize window.
|
---|
733 | *
|
---|
734 | * @param wnd Window
|
---|
735 | */
|
---|
736 | errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
|
---|
737 | gfx_rect_t *nrect)
|
---|
738 | {
|
---|
739 | gfx_context_t *dgc;
|
---|
740 | gfx_bitmap_params_t bparams;
|
---|
741 | gfx_bitmap_t *nbitmap;
|
---|
742 | pixelmap_t npixelmap;
|
---|
743 | gfx_coord2_t dims;
|
---|
744 | gfx_bitmap_alloc_t alloc;
|
---|
745 | gfx_coord2_t ndpos;
|
---|
746 | errno_t rc;
|
---|
747 |
|
---|
748 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
749 | if (dgc != NULL) {
|
---|
750 | gfx_bitmap_params_init(&bparams);
|
---|
751 | bparams.rect = *nrect;
|
---|
752 |
|
---|
753 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
|
---|
754 | if (rc != EOK)
|
---|
755 | return ENOMEM;
|
---|
756 |
|
---|
757 | rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
|
---|
758 | if (rc != EOK) {
|
---|
759 | gfx_bitmap_destroy(nbitmap);
|
---|
760 | return ENOMEM;
|
---|
761 | }
|
---|
762 |
|
---|
763 | gfx_rect_dims(nrect, &dims);
|
---|
764 | npixelmap.width = dims.x;
|
---|
765 | npixelmap.height = dims.y;
|
---|
766 | npixelmap.data = alloc.pixels;
|
---|
767 |
|
---|
768 | /* TODO: Transfer contents within overlap */
|
---|
769 |
|
---|
770 | if (wnd->bitmap != NULL)
|
---|
771 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
772 |
|
---|
773 | wnd->bitmap = nbitmap;
|
---|
774 | wnd->pixelmap = npixelmap;
|
---|
775 | }
|
---|
776 |
|
---|
777 | gfx_coord2_add(&wnd->dpos, offs, &ndpos);
|
---|
778 |
|
---|
779 | wnd->dpos = ndpos;
|
---|
780 | wnd->rect = *nrect;
|
---|
781 |
|
---|
782 | (void) ds_display_paint(wnd->display, NULL);
|
---|
783 | return EOK;
|
---|
784 | }
|
---|
785 |
|
---|
786 | /** Compute new window rectangle after resize operation.
|
---|
787 | *
|
---|
788 | * @param wnd Window which is being resized (in dsw_resizing state and thus
|
---|
789 | * has rsztype set)
|
---|
790 | * @param dresize Amount by which to resize
|
---|
791 | * @param nrect Place to store new rectangle
|
---|
792 | */
|
---|
793 | void ds_window_calc_resize(ds_window_t *wnd, gfx_coord2_t *dresize,
|
---|
794 | gfx_rect_t *nrect)
|
---|
795 | {
|
---|
796 | if ((wnd->rsztype & display_wr_top) != 0) {
|
---|
797 | nrect->p0.y = min(wnd->rect.p0.y + dresize->y,
|
---|
798 | wnd->rect.p1.y - wnd->min_size.y);
|
---|
799 | } else {
|
---|
800 | nrect->p0.y = wnd->rect.p0.y;
|
---|
801 | }
|
---|
802 |
|
---|
803 | if ((wnd->rsztype & display_wr_left) != 0) {
|
---|
804 | nrect->p0.x = min(wnd->rect.p0.x + dresize->x,
|
---|
805 | wnd->rect.p1.x - wnd->min_size.x);
|
---|
806 | } else {
|
---|
807 | nrect->p0.x = wnd->rect.p0.x;
|
---|
808 | }
|
---|
809 |
|
---|
810 | if ((wnd->rsztype & display_wr_bottom) != 0) {
|
---|
811 | nrect->p1.y = max(wnd->rect.p1.y + dresize->y,
|
---|
812 | wnd->rect.p0.y + wnd->min_size.y);
|
---|
813 | } else {
|
---|
814 | nrect->p1.y = wnd->rect.p1.y;
|
---|
815 | }
|
---|
816 |
|
---|
817 | if ((wnd->rsztype & display_wr_right) != 0) {
|
---|
818 | nrect->p1.x = max(wnd->rect.p1.x + dresize->x,
|
---|
819 | wnd->rect.p0.x + wnd->min_size.x);
|
---|
820 | } else {
|
---|
821 | nrect->p1.x = wnd->rect.p1.x;
|
---|
822 | }
|
---|
823 | }
|
---|
824 |
|
---|
825 | /** Set window cursor.
|
---|
826 | *
|
---|
827 | * @param wnd Window
|
---|
828 | * @return EOK on success, EINVAL if @a cursor is invalid
|
---|
829 | */
|
---|
830 | errno_t ds_window_set_cursor(ds_window_t *wnd, display_stock_cursor_t cursor)
|
---|
831 | {
|
---|
832 | if (cursor >= dcurs_arrow &&
|
---|
833 | cursor < (display_stock_cursor_t) dcurs_limit) {
|
---|
834 | wnd->cursor = wnd->display->cursor[cursor];
|
---|
835 | return EOK;
|
---|
836 | } else {
|
---|
837 | return EINVAL;
|
---|
838 | }
|
---|
839 | }
|
---|
840 |
|
---|
841 | /** @}
|
---|
842 | */
|
---|