source: mainline/uspace/srv/hid/display/window.c@ 35cffea

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35cffea was 35cffea, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Maximizing/unmaximizing a window

  • Property mode set to 100644
File size: 22.4 KB
Line 
1/*
2 * Copyright (c) 2022 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 <memgfx/memgc.h>
45#include <stdlib.h>
46#include "client.h"
47#include "display.h"
48#include "seat.h"
49#include "window.h"
50
51static void ds_window_invalidate_cb(void *, gfx_rect_t *);
52static void ds_window_update_cb(void *);
53static void ds_window_get_preview_rect(ds_window_t *, gfx_rect_t *);
54
55static mem_gc_cb_t ds_window_mem_gc_cb = {
56 .invalidate = ds_window_invalidate_cb,
57 .update = ds_window_update_cb
58};
59
60/** Create window.
61 *
62 * Create graphics context for rendering into a window.
63 *
64 * @param client Client owning the window
65 * @param params Window parameters
66 * @param rgc Place to store pointer to new GC.
67 *
68 * @return EOK on success or an error code
69 */
70errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
71 ds_window_t **rgc)
72{
73 ds_window_t *wnd = NULL;
74 ds_seat_t *seat;
75 gfx_context_t *dgc;
76 gfx_coord2_t dims;
77 gfx_bitmap_params_t bparams;
78 gfx_bitmap_alloc_t alloc;
79 errno_t rc;
80
81 wnd = calloc(1, sizeof(ds_window_t));
82 if (wnd == NULL) {
83 rc = ENOMEM;
84 goto error;
85 }
86
87 ds_client_add_window(client, wnd);
88 ds_display_add_window(client->display, wnd);
89
90 gfx_bitmap_params_init(&bparams);
91 bparams.rect = params->rect;
92
93 dgc = ds_display_get_gc(wnd->display);
94 if (dgc != NULL) {
95 rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
96 if (rc != EOK)
97 goto error;
98
99 rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
100 if (rc != EOK)
101 goto error;
102
103 gfx_rect_dims(&params->rect, &dims);
104 wnd->pixelmap.width = dims.x;
105 wnd->pixelmap.height = dims.y;
106 wnd->pixelmap.data = alloc.pixels;
107 } else {
108 /* This is just for unit tests */
109 gfx_rect_dims(&params->rect, &dims);
110 alloc.pitch = dims.x * sizeof(uint32_t);
111 alloc.off0 = 0;
112 alloc.pixels = calloc(1, alloc.pitch * dims.y);
113 }
114
115 rc = mem_gc_create(&params->rect, &alloc, &ds_window_mem_gc_cb,
116 (void *)wnd, &wnd->mgc);
117 if (rc != EOK)
118 goto error;
119
120 wnd->rect = params->rect;
121 wnd->min_size = params->min_size;
122 wnd->gc = mem_gc_get_ctx(wnd->mgc);
123 wnd->cursor = wnd->display->cursor[dcurs_arrow];
124 wnd->flags = params->flags;
125
126 if ((params->flags & wndf_setpos) != 0) {
127 /* Specific window position */
128 wnd->dpos = params->pos;
129 } else {
130 /* Automatic window placement */
131 wnd->dpos.x = ((wnd->id - 1) & 1) * 400;
132 wnd->dpos.y = ((wnd->id - 1) & 2) / 2 * 300;
133 }
134
135 seat = ds_display_first_seat(client->display);
136
137 if ((params->flags & wndf_popup) != 0)
138 ds_seat_set_popup(seat, wnd);
139 else
140 ds_seat_set_focus(seat, wnd);
141
142 (void) ds_display_paint(wnd->display, NULL);
143
144 *rgc = wnd;
145 return EOK;
146error:
147 if (wnd != NULL) {
148 if (wnd->bitmap != NULL)
149 gfx_bitmap_destroy(wnd->bitmap);
150 free(wnd);
151 }
152
153 return rc;
154}
155
156/** Destroy window.
157 *
158 * @param wnd Window
159 */
160void ds_window_destroy(ds_window_t *wnd)
161{
162 ds_display_t *disp;
163
164 disp = wnd->display;
165
166 ds_client_remove_window(wnd);
167 ds_display_remove_window(wnd);
168
169 mem_gc_delete(wnd->mgc);
170
171 if (wnd->bitmap != NULL)
172 gfx_bitmap_destroy(wnd->bitmap);
173
174 free(wnd);
175
176 (void) ds_display_paint(disp, NULL);
177}
178
179/** Bring window to top.
180 *
181 * @param wnd Window
182 */
183void ds_window_bring_to_top(ds_window_t *wnd)
184{
185 ds_display_t *disp = wnd->display;
186
187 ds_display_remove_window(wnd);
188 ds_display_add_window(disp, wnd);
189 (void) ds_display_paint(wnd->display, NULL);
190}
191
192/** Get generic graphic context from window.
193 *
194 * @param wnd Window
195 * @return Graphic context
196 */
197gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
198{
199 return wnd->gc;
200}
201
202/** Paint a window using its backing bitmap.
203 *
204 * @param wnd Window to paint
205 * @param rect Display rectangle to paint to
206 * @return EOK on success or an error code
207 */
208errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
209{
210 gfx_rect_t srect;
211 gfx_rect_t *brect;
212 gfx_rect_t crect;
213
214 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
215
216 if (rect != NULL) {
217 gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
218
219 /* Determine if we have anything to do */
220 gfx_rect_clip(&srect, &wnd->rect, &crect);
221 if (gfx_rect_is_empty(&crect))
222 return EOK;
223
224 brect = &srect;
225 } else {
226 brect = NULL;
227 }
228
229 /* This can happen in unit tests */
230 if (wnd->bitmap == NULL)
231 return EOK;
232
233 return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
234}
235
236/** Get the preview rectangle for a window.
237 *
238 * Get the preview rectangle if the window is being resized or moved.
239 * If the window is not being resized or moved, return an empty rectangle.
240 *
241 * @param wnd Window
242 * @param rect Place to store preview rectangle
243 */
244static void ds_window_get_preview_rect(ds_window_t *wnd, gfx_rect_t *rect)
245{
246 switch (wnd->state) {
247 case dsw_idle:
248 break;
249 case dsw_moving:
250 gfx_rect_translate(&wnd->preview_pos, &wnd->rect, rect);
251 return;
252 case dsw_resizing:
253 gfx_rect_translate(&wnd->dpos, &wnd->preview_rect, rect);
254 return;
255 }
256
257 rect->p0.x = 0;
258 rect->p0.y = 0;
259 rect->p1.x = 0;
260 rect->p1.y = 0;
261}
262
263/** Paint window preview if the window is being moved or resized.
264 *
265 * If the window is not being resized or moved, take no action and return
266 * success.
267 *
268 * @param wnd Window for which to paint preview
269 * @param rect Clipping rectangle
270 * @return EOK on success or an error code
271 */
272errno_t ds_window_paint_preview(ds_window_t *wnd, gfx_rect_t *rect)
273{
274 errno_t rc;
275 gfx_color_t *color;
276 gfx_rect_t prect;
277 gfx_rect_t dr;
278 gfx_rect_t pr;
279 gfx_context_t *gc;
280
281 /*
282 * Get preview rectangle. If the window is not being resized/moved,
283 * we should get an empty rectangle.
284 */
285 ds_window_get_preview_rect(wnd, &prect);
286 if (gfx_rect_is_empty(&prect)) {
287 /* There is nothing to paint */
288 return EOK;
289 }
290
291 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
292 if (rc != EOK)
293 return rc;
294
295 gc = ds_display_get_gc(wnd->display);
296 if (gc != NULL) {
297 gfx_set_color(gc, color);
298
299 /*
300 * TODO: Ideally we'd want XOR operation to make the preview
301 * frame visible on any background. If we wanted to get really
302 * fancy, we'd fill it with a pattern
303 */
304
305 pr.p0.x = prect.p0.x;
306 pr.p0.y = prect.p0.y;
307 pr.p1.x = prect.p1.x;
308 pr.p1.y = prect.p0.y + 1;
309 gfx_rect_clip(&pr, rect, &dr);
310 gfx_fill_rect(gc, &dr);
311
312 pr.p0.x = prect.p0.x;
313 pr.p0.y = prect.p1.y - 1;
314 pr.p1.x = prect.p1.x;
315 pr.p1.y = prect.p1.y;
316 gfx_rect_clip(&pr, rect, &dr);
317 gfx_fill_rect(gc, &dr);
318
319 pr.p0.x = prect.p0.x;
320 pr.p0.y = prect.p0.y;
321 pr.p1.x = prect.p0.x + 1;
322 pr.p1.y = prect.p1.y;
323 gfx_rect_clip(&pr, rect, &dr);
324 gfx_fill_rect(gc, &dr);
325
326 pr.p0.x = prect.p1.x - 1;
327 pr.p0.y = prect.p0.y;
328 pr.p1.x = prect.p1.x;
329 pr.p1.y = prect.p1.y;
330 gfx_rect_clip(&pr, rect, &dr);
331 gfx_fill_rect(gc, &dr);
332
333 }
334
335 gfx_color_delete(color);
336 return EOK;
337}
338
339/** Repaint window preview when resizing or moving.
340 *
341 * Repaint the window preview wich was previously at rectangle @a old_rect.
342 * The current preview rectangle is determined from window state. If
343 * the window did not previously have a preview, @a old_rect should point
344 * to an empty rectangle or be NULL. When window has finished
345 * moving or resizing, the preview will be cleared.
346 *
347 * @param wnd Window for which to paint preview
348 * @param rect Clipping rectangle
349 * @return EOK on success or an error code
350 */
351static errno_t ds_window_repaint_preview(ds_window_t *wnd, gfx_rect_t *old_rect)
352{
353 errno_t rc;
354 gfx_rect_t prect;
355 gfx_rect_t envelope;
356 bool oldr;
357 bool newr;
358
359 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_repaint_preview");
360
361 /*
362 * Get current preview rectangle. If the window is not being resized/moved,
363 * we should get an empty rectangle.
364 */
365 ds_window_get_preview_rect(wnd, &prect);
366
367 oldr = (old_rect != NULL) && !gfx_rect_is_empty(old_rect);
368 newr = !gfx_rect_is_empty(&prect);
369
370 if (oldr && newr && gfx_rect_is_incident(old_rect, &prect)) {
371 /*
372 * As an optimization, repaint both rectangles in a single
373 * operation.
374 */
375
376 gfx_rect_envelope(old_rect, &prect, &envelope);
377
378 rc = ds_display_paint(wnd->display, &envelope);
379 if (rc != EOK)
380 return rc;
381 } else {
382 /* Repaint each rectangle separately */
383 if (oldr) {
384 rc = ds_display_paint(wnd->display, old_rect);
385 if (rc != EOK)
386 return rc;
387 }
388
389 if (newr) {
390 rc = ds_display_paint(wnd->display, &prect);
391 if (rc != EOK)
392 return rc;
393 }
394 }
395
396 return EOK;
397}
398
399/** Start moving a window by mouse drag.
400 *
401 * @param wnd Window
402 * @param pos Position where mouse button was pressed
403 */
404static void ds_window_start_move(ds_window_t *wnd, gfx_coord2_t *pos)
405{
406 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
407 (int) pos->x, (int) pos->y);
408
409 if (wnd->state != dsw_idle)
410 return;
411
412 wnd->orig_pos = *pos;
413 wnd->state = dsw_moving;
414 wnd->preview_pos = wnd->dpos;
415
416 (void) ds_window_repaint_preview(wnd, NULL);
417}
418
419/** Finish moving a window by mouse drag.
420 *
421 * @param wnd Window
422 * @param pos Position where mouse button was released
423 */
424static void ds_window_finish_move(ds_window_t *wnd, gfx_coord2_t *pos)
425{
426 gfx_coord2_t dmove;
427 gfx_coord2_t nwpos;
428 gfx_rect_t old_rect;
429
430 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
431 (int) pos->x, (int) pos->y);
432
433 assert(wnd->state == dsw_moving);
434
435 gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
436 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
437
438 ds_window_get_preview_rect(wnd, &old_rect);
439
440 wnd->dpos = nwpos;
441 wnd->state = dsw_idle;
442
443 (void) ds_display_paint(wnd->display, NULL);
444}
445
446/** Update window position when moving by mouse drag.
447 *
448 * @param wnd Window
449 * @param pos Current mouse position
450 */
451static void ds_window_update_move(ds_window_t *wnd, gfx_coord2_t *pos)
452{
453 gfx_coord2_t dmove;
454 gfx_coord2_t nwpos;
455 gfx_rect_t old_rect;
456
457 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
458 (int) pos->x, (int) pos->y);
459
460 assert(wnd->state == dsw_moving);
461
462 gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
463 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
464
465 ds_window_get_preview_rect(wnd, &old_rect);
466 wnd->preview_pos = nwpos;
467
468 (void) ds_window_repaint_preview(wnd, &old_rect);
469}
470
471/** Start resizing a window by mouse drag.
472 *
473 * @param wnd Window
474 * @param rsztype Resize type (which part of window is being dragged)
475 * @param pos Position where mouse button was pressed
476 */
477static void ds_window_start_resize(ds_window_t *wnd,
478 display_wnd_rsztype_t rsztype, gfx_coord2_t *pos)
479{
480 ds_seat_t *seat;
481 display_stock_cursor_t ctype;
482
483 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_resize (%d, %d)",
484 (int) pos->x, (int) pos->y);
485
486 if (wnd->state != dsw_idle)
487 return;
488
489 wnd->orig_pos = *pos;
490 wnd->state = dsw_resizing;
491 wnd->rsztype = rsztype;
492 wnd->preview_rect = wnd->rect;
493
494 // XXX Need client to tell us which seat started the resize!
495 seat = ds_display_first_seat(wnd->display);
496 ctype = display_cursor_from_wrsz(rsztype);
497 ds_seat_set_wm_cursor(seat, wnd->display->cursor[ctype]);
498
499 (void) ds_window_repaint_preview(wnd, NULL);
500}
501
502/** Finish resizing a window by mouse drag.
503 *
504 * @param wnd Window
505 * @param pos Position where mouse button was released
506 */
507static void ds_window_finish_resize(ds_window_t *wnd, gfx_coord2_t *pos)
508{
509 gfx_coord2_t dresize;
510 gfx_rect_t nrect;
511 ds_seat_t *seat;
512
513 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_resize (%d, %d)",
514 (int) pos->x, (int) pos->y);
515
516 assert(wnd->state == dsw_resizing);
517 gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
518
519 /* Compute new rectangle */
520 ds_window_calc_resize(wnd, &dresize, &nrect);
521
522 wnd->state = dsw_idle;
523 ds_client_post_resize_event(wnd->client, wnd, &nrect);
524
525 // XXX Need to know which seat started the resize!
526 seat = ds_display_first_seat(wnd->display);
527 ds_seat_set_wm_cursor(seat, NULL);
528
529 (void) ds_display_paint(wnd->display, NULL);
530}
531
532/** Update window position when resizing by mouse drag.
533 *
534 * @param wnd Window
535 * @param pos Current mouse position
536 */
537static void ds_window_update_resize(ds_window_t *wnd, gfx_coord2_t *pos)
538{
539 gfx_coord2_t dresize;
540 gfx_rect_t nrect;
541 gfx_rect_t old_rect;
542
543 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_resize (%d, %d)",
544 (int) pos->x, (int) pos->y);
545
546 assert(wnd->state == dsw_resizing);
547
548 gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
549 ds_window_calc_resize(wnd, &dresize, &nrect);
550
551 ds_window_get_preview_rect(wnd, &old_rect);
552 wnd->preview_rect = nrect;
553 (void) ds_window_repaint_preview(wnd, &old_rect);
554}
555
556/** Post keyboard event to window.
557 *
558 * @param wnd Window
559 * @param event Event
560 *
561 * @return EOK on success or an error code
562 */
563errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
564{
565 bool alt_or_shift;
566
567 alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
568
569 if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
570 /* On Alt-F4 or Shift-F4 send close event to the window */
571 ds_client_post_close_event(wnd->client, wnd);
572 return EOK;
573 }
574
575 return ds_client_post_kbd_event(wnd->client, wnd, event);
576}
577
578/** Post position event to window.
579 *
580 * @param wnd Window
581 * @param event Position event
582 *
583 * @return EOK on success or an error code
584 */
585errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
586{
587 pos_event_t tevent;
588 gfx_coord2_t pos;
589 gfx_rect_t drect;
590 bool inside;
591
592 log_msg(LOG_DEFAULT, LVL_DEBUG,
593 "ds_window_post_pos_event type=%d pos=%d,%d", event->type,
594 (int) event->hpos, (int) event->vpos);
595
596 pos.x = event->hpos;
597 pos.y = event->vpos;
598 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
599 inside = gfx_pix_inside_rect(&pos, &drect);
600
601 if (event->type == POS_PRESS && event->btn_num == 2 && inside &&
602 (wnd->flags & wndf_maximized) == 0) {
603 ds_window_start_move(wnd, &pos);
604 return EOK;
605 }
606
607 if (event->type == POS_RELEASE) {
608 if (wnd->state == dsw_moving) {
609 ds_window_finish_move(wnd, &pos);
610 return EOK;
611 }
612
613 if (wnd->state == dsw_resizing) {
614 ds_window_finish_resize(wnd, &pos);
615 return EOK;
616 }
617 }
618
619 if (event->type == POS_UPDATE) {
620 if (wnd->state == dsw_moving) {
621 ds_window_update_move(wnd, &pos);
622 return EOK;
623 }
624
625 if (wnd->state == dsw_resizing) {
626 ds_window_update_resize(wnd, &pos);
627 return EOK;
628 }
629 }
630
631 /* Transform event coordinates to window-local */
632 tevent = *event;
633 tevent.hpos -= wnd->dpos.x;
634 tevent.vpos -= wnd->dpos.y;
635
636 return ds_client_post_pos_event(wnd->client, wnd, &tevent);
637}
638
639/** Post focus event to window.
640 *
641 * @param wnd Window
642 * @return EOK on success or an error code
643 */
644errno_t ds_window_post_focus_event(ds_window_t *wnd)
645{
646 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event");
647
648 return ds_client_post_focus_event(wnd->client, wnd);
649}
650
651/** Post unfocus event to window.
652 *
653 * @param wnd Window
654 * @return EOK on success or an error code
655 */
656errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
657{
658 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event");
659
660 return ds_client_post_unfocus_event(wnd->client, wnd);
661}
662
663/** Start moving a window, detected by client.
664 *
665 * @param wnd Window
666 * @param pos Position where the pointer was when the move started
667 * relative to the window
668 * @param event Button press event
669 */
670void ds_window_move_req(ds_window_t *wnd, gfx_coord2_t *pos)
671{
672 gfx_coord2_t orig_pos;
673
674 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_move_req (%d, %d)",
675 (int) pos->x, (int) pos->y);
676
677 gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
678 ds_window_start_move(wnd, &orig_pos);
679}
680
681/** Move window.
682 *
683 * @param wnd Window
684 */
685void ds_window_move(ds_window_t *wnd, gfx_coord2_t *dpos)
686{
687 wnd->dpos = *dpos;
688 (void) ds_display_paint(wnd->display, NULL);
689}
690
691/** Get window position.
692 *
693 * @param wnd Window
694 */
695void ds_window_get_pos(ds_window_t *wnd, gfx_coord2_t *dpos)
696{
697 *dpos = wnd->dpos;
698}
699
700/** Get maximized window rectangle.
701 *
702 * @param wnd Window
703 */
704void ds_window_get_max_rect(ds_window_t *wnd, gfx_rect_t *rect)
705{
706 *rect = wnd->display->rect;
707}
708
709/** Start resizing a window, detected by client.
710 *
711 * @param wnd Window
712 * @param rsztype Resize type (which part of window is being dragged)
713 * @param pos Position where the pointer was when the resize started
714 * relative to the window
715 * @param event Button press event
716 */
717void ds_window_resize_req(ds_window_t *wnd, display_wnd_rsztype_t rsztype,
718 gfx_coord2_t *pos)
719{
720 gfx_coord2_t orig_pos;
721
722 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_resize_req (%d, %d, %d)",
723 (int) rsztype, (int) pos->x, (int) pos->y);
724
725 gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
726 ds_window_start_resize(wnd, rsztype, &orig_pos);
727}
728
729/** Resize window.
730 *
731 * @param wnd Window
732 * @return EOK on success or an error code
733 */
734errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
735 gfx_rect_t *nrect)
736{
737 gfx_context_t *dgc;
738 gfx_bitmap_params_t bparams;
739 gfx_bitmap_t *nbitmap;
740 pixelmap_t npixelmap;
741 gfx_coord2_t dims;
742 gfx_bitmap_alloc_t alloc;
743 gfx_coord2_t ndpos;
744 errno_t rc;
745
746 dgc = ds_display_get_gc(wnd->display);
747 if (dgc != NULL) {
748 gfx_bitmap_params_init(&bparams);
749 bparams.rect = *nrect;
750
751 rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
752 if (rc != EOK)
753 return ENOMEM;
754
755 rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
756 if (rc != EOK) {
757 gfx_bitmap_destroy(nbitmap);
758 return ENOMEM;
759 }
760
761 gfx_rect_dims(nrect, &dims);
762 npixelmap.width = dims.x;
763 npixelmap.height = dims.y;
764 npixelmap.data = alloc.pixels;
765
766 /* TODO: Transfer contents within overlap */
767
768 if (wnd->bitmap != NULL)
769 gfx_bitmap_destroy(wnd->bitmap);
770
771 wnd->bitmap = nbitmap;
772 wnd->pixelmap = npixelmap;
773
774 /* Point memory GC to the new bitmap */
775 mem_gc_retarget(wnd->mgc, nrect, &alloc);
776 }
777
778 gfx_coord2_add(&wnd->dpos, offs, &ndpos);
779
780 wnd->dpos = ndpos;
781 wnd->rect = *nrect;
782
783 (void) ds_display_paint(wnd->display, NULL);
784 return EOK;
785}
786
787/** Maximize window.
788 *
789 * @param wnd Window
790 * @return EOK on success or an error code
791 */
792errno_t ds_window_maximize(ds_window_t *wnd)
793{
794 gfx_coord2_t old_dpos;
795 gfx_rect_t old_rect;
796 gfx_coord2_t offs;
797 gfx_rect_t max_rect;
798 gfx_rect_t nrect;
799 errno_t rc;
800
801 /* If already maximized, do nothing and return success. */
802 if ((wnd->flags & wndf_maximized) != 0)
803 return EOK;
804
805 /* Remember the old window rectangle and display position */
806 old_rect = wnd->rect;
807 old_dpos = wnd->dpos;
808
809 ds_window_get_max_rect(wnd, &max_rect);
810
811 /* Keep window contents on the same position on the screen */
812 offs.x = max_rect.p0.x - wnd->dpos.x;
813 offs.y = max_rect.p0.y - wnd->dpos.y;
814
815 /* Maximized window's coordinates will start at 0,0 */
816 gfx_rect_rtranslate(&max_rect.p0, &max_rect, &nrect);
817
818 rc = ds_window_resize(wnd, &offs, &nrect);
819 if (rc != EOK)
820 return rc;
821
822 /* Set window flags, remember normal rectangle */
823 wnd->flags |= wndf_maximized;
824 wnd->normal_rect = old_rect;
825 wnd->normal_dpos = old_dpos;
826
827 return EOK;
828}
829
830/** Unmaximize window.
831 *
832 * @param wnd Window
833 * @return EOK on success or an error code
834 */
835errno_t ds_window_unmaximize(ds_window_t *wnd)
836{
837 gfx_coord2_t offs;
838 errno_t rc;
839
840 /* If not maximized, do nothing and return success. */
841 if ((wnd->flags & wndf_maximized) == 0)
842 return EOK;
843
844 /* Keep window contents on the same position on the screen */
845 offs.x = wnd->normal_dpos.x - wnd->dpos.x;
846 offs.y = wnd->normal_dpos.y - wnd->dpos.y;
847
848 rc = ds_window_resize(wnd, &offs, &wnd->normal_rect);
849 if (rc != EOK)
850 return rc;
851
852 /* Set window flags, remember normal rectangle */
853 wnd->flags &= ~wndf_maximized;
854
855 return EOK;
856}
857
858/** Compute new window rectangle after resize operation.
859 *
860 * @param wnd Window which is being resized (in dsw_resizing state and thus
861 * has rsztype set)
862 * @param dresize Amount by which to resize
863 * @param nrect Place to store new rectangle
864 */
865void ds_window_calc_resize(ds_window_t *wnd, gfx_coord2_t *dresize,
866 gfx_rect_t *nrect)
867{
868 if ((wnd->rsztype & display_wr_top) != 0) {
869 nrect->p0.y = min(wnd->rect.p0.y + dresize->y,
870 wnd->rect.p1.y - wnd->min_size.y);
871 } else {
872 nrect->p0.y = wnd->rect.p0.y;
873 }
874
875 if ((wnd->rsztype & display_wr_left) != 0) {
876 nrect->p0.x = min(wnd->rect.p0.x + dresize->x,
877 wnd->rect.p1.x - wnd->min_size.x);
878 } else {
879 nrect->p0.x = wnd->rect.p0.x;
880 }
881
882 if ((wnd->rsztype & display_wr_bottom) != 0) {
883 nrect->p1.y = max(wnd->rect.p1.y + dresize->y,
884 wnd->rect.p0.y + wnd->min_size.y);
885 } else {
886 nrect->p1.y = wnd->rect.p1.y;
887 }
888
889 if ((wnd->rsztype & display_wr_right) != 0) {
890 nrect->p1.x = max(wnd->rect.p1.x + dresize->x,
891 wnd->rect.p0.x + wnd->min_size.x);
892 } else {
893 nrect->p1.x = wnd->rect.p1.x;
894 }
895}
896
897/** Set window cursor.
898 *
899 * @param wnd Window
900 * @return EOK on success, EINVAL if @a cursor is invalid
901 */
902errno_t ds_window_set_cursor(ds_window_t *wnd, display_stock_cursor_t cursor)
903{
904 if (cursor >= dcurs_arrow &&
905 cursor < (display_stock_cursor_t) dcurs_limit) {
906 wnd->cursor = wnd->display->cursor[cursor];
907 return EOK;
908 } else {
909 return EINVAL;
910 }
911}
912
913/** Window memory GC invalidate callback.
914 *
915 * This is called by the window's memory GC when a rectangle is modified.
916 */
917static void ds_window_invalidate_cb(void *arg, gfx_rect_t *rect)
918{
919 ds_window_t *wnd = (ds_window_t *)arg;
920 gfx_rect_t drect;
921
922 /* Repaint the corresponding part of the display */
923
924 gfx_rect_translate(&wnd->dpos, rect, &drect);
925 ds_display_lock(wnd->display);
926 (void) ds_display_paint(wnd->display, &drect);
927 ds_display_unlock(wnd->display);
928}
929
930/** Window memory GC update callback.
931 *
932 * This is called by the window's memory GC when it is to be updated.
933 */
934static void ds_window_update_cb(void *arg)
935{
936 ds_window_t *wnd = (ds_window_t *)arg;
937
938 (void) wnd;
939}
940
941/** @}
942 */
Note: See TracBrowser for help on using the repository browser.