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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 63b35c7 was 12008adf, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Port barber to UI

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