source: mainline/uspace/lib/ui/src/image.c@ bfc0fc6

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

Client-side UI rendering

It is possible to turn on and off and if turned on, one can also
enable or disable window double buffering (currently both options
are build-time).

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2021 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 libui
30 * @{
31 */
32/**
33 * @file Image
34 */
35
36#include <errno.h>
37#include <gfx/bitmap.h>
38#include <gfx/context.h>
39#include <gfx/render.h>
40#include <gfx/text.h>
41#include <stdlib.h>
42#include <str.h>
43#include <ui/control.h>
44#include <ui/paint.h>
45#include <ui/image.h>
46#include "../private/image.h"
47#include "../private/resource.h"
48#include "../private/ui.h"
49
50static void ui_image_ctl_destroy(void *);
51static errno_t ui_image_ctl_paint(void *);
52static ui_evclaim_t ui_image_ctl_pos_event(void *, pos_event_t *);
53
54/** Image control ops */
55ui_control_ops_t ui_image_ops = {
56 .destroy = ui_image_ctl_destroy,
57 .paint = ui_image_ctl_paint,
58 .pos_event = ui_image_ctl_pos_event
59};
60
61/** Create new image.
62 *
63 * @param resource UI resource
64 * @param bitmap Bitmap
65 * @param brect Bitmap rectangle
66 * @param rimage Place to store pointer to new image
67 * @return EOK on success, ENOMEM if out of memory
68 */
69errno_t ui_image_create(ui_resource_t *resource, gfx_bitmap_t *bitmap,
70 gfx_rect_t *brect, ui_image_t **rimage)
71{
72 ui_image_t *image;
73 errno_t rc;
74
75 image = calloc(1, sizeof(ui_image_t));
76 if (image == NULL)
77 return ENOMEM;
78
79 rc = ui_control_new(&ui_image_ops, (void *) image, &image->control);
80 if (rc != EOK) {
81 free(image);
82 return rc;
83 }
84
85 image->bitmap = bitmap;
86 image->brect = *brect;
87 image->res = resource;
88 *rimage = image;
89 return EOK;
90}
91
92/** Destroy image.
93 *
94 * @param image Image or @c NULL
95 */
96void ui_image_destroy(ui_image_t *image)
97{
98 if (image == NULL)
99 return;
100
101 ui_control_delete(image->control);
102 if (image->bitmap != NULL)
103 gfx_bitmap_destroy(image->bitmap);
104 free(image);
105}
106
107/** Get base control from image.
108 *
109 * @param image Image
110 * @return Control
111 */
112ui_control_t *ui_image_ctl(ui_image_t *image)
113{
114 return image->control;
115}
116
117/** Set image rectangle.
118 *
119 * @param image Image
120 * @param rect New image rectangle
121 */
122void ui_image_set_rect(ui_image_t *image, gfx_rect_t *rect)
123{
124 image->rect = *rect;
125}
126
127/** Set image flags.
128 *
129 * @param image Image
130 * @param flags Flags
131 */
132void ui_image_set_flags(ui_image_t *image, ui_image_flags_t flags)
133{
134 image->flags = flags;
135}
136
137/** Paint image.
138 *
139 * @param image Image
140 * @return EOK on success or an error code
141 */
142errno_t ui_image_paint(ui_image_t *image)
143{
144 gfx_rect_t irect;
145 gfx_rect_t srect;
146 gfx_coord2_t offs;
147 errno_t rc;
148
149 if ((image->flags & ui_imgf_frame) != 0) {
150 rc = ui_paint_bevel(image->res->gc, &image->rect,
151 image->res->btn_frame_color, image->res->btn_frame_color,
152 1, NULL);
153 if (rc != EOK)
154 return rc;
155 }
156
157 if (image->bitmap == NULL)
158 return EOK;
159
160 irect = image->rect;
161 if ((image->flags & ui_imgf_frame) != 0) {
162 irect.p0.x++;
163 irect.p0.y++;
164 irect.p1.x--;
165 irect.p1.y--;
166 }
167
168 /*
169 * UI image position does not depend on bitmap rectangle p0, so
170 * we need to subtract it.
171 */
172 offs.x = irect.p0.x - image->brect.p0.x;
173 offs.y = irect.p0.y - image->brect.p0.y;
174
175 /*
176 * Translate image rectangle back to bitmap coordinate space.
177 * Thus the bitmap will be clipped to the image rectangle.
178 */
179 gfx_rect_rtranslate(&offs, &irect, &srect);
180 rc = gfx_bitmap_render(image->bitmap, &srect, &offs);
181 if (rc != EOK)
182 return rc;
183
184 return gfx_update(image->res->gc);
185}
186
187/** Change image bitmap.
188 *
189 * Note that the caller must have saved the pointer to the previous bitmap
190 * in the image, because this causes it to be unlinked from the image and
191 * not destroyed (the ownership is transferred back to the caller).
192 *
193 * @param image Image
194 * @param bitmap New bitmap (ownership transferred to image) or @c NULL
195 * @param brect New bitmap rectangle
196 */
197void ui_image_set_bmp(ui_image_t *image, gfx_bitmap_t *bitmap,
198 gfx_rect_t *brect)
199{
200 image->bitmap = bitmap;
201 image->brect = *brect;
202}
203
204/** Destroy image control.
205 *
206 * @param arg Argument (ui_image_t *)
207 */
208void ui_image_ctl_destroy(void *arg)
209{
210 ui_image_t *image = (ui_image_t *) arg;
211
212 ui_image_destroy(image);
213}
214
215/** Paint image control.
216 *
217 * @param arg Argument (ui_image_t *)
218 * @return EOK on success or an error code
219 */
220errno_t ui_image_ctl_paint(void *arg)
221{
222 ui_image_t *image = (ui_image_t *) arg;
223
224 return ui_image_paint(image);
225}
226
227/** Handle image control position event.
228 *
229 * @param arg Argument (ui_image_t *)
230 * @param pos_event Position event
231 * @return @c ui_claimed iff the event is claimed
232 */
233ui_evclaim_t ui_image_ctl_pos_event(void *arg, pos_event_t *event)
234{
235 ui_image_t *image = (ui_image_t *) arg;
236
237 (void) image;
238 return ui_unclaimed;
239}
240
241/** @}
242 */
Note: See TracBrowser for help on using the repository browser.