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

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

Add UI image

  • Property mode set to 100644
File size: 4.6 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 /*
137 * UI image position does not depend on bitmap rectangle p0, so
138 * we need to subtract it.
139 */
140 offs.x = image->rect.p0.x - image->brect.p0.x;
141 offs.y = image->rect.p0.y - image->brect.p0.y;
142
143 /*
144 * Transalte image rectangle back to bitmap coordinate space.
145 * Thus the bitmap will be clipped to the image rectangle.
146 */
147 gfx_rect_rtranslate(&offs, &image->rect, &srect);
148 return gfx_bitmap_render(image->bitmap, &srect, &offs);
149}
150
151/** Destroy image control.
152 *
153 * @param arg Argument (ui_image_t *)
154 */
155void ui_image_ctl_destroy(void *arg)
156{
157 ui_image_t *image = (ui_image_t *) arg;
158
159 ui_image_destroy(image);
160}
161
162/** Paint image control.
163 *
164 * @param arg Argument (ui_image_t *)
165 * @return EOK on success or an error code
166 */
167errno_t ui_image_ctl_paint(void *arg)
168{
169 ui_image_t *image = (ui_image_t *) arg;
170
171 return ui_image_paint(image);
172}
173
174/** Handle image control position event.
175 *
176 * @param arg Argument (ui_image_t *)
177 * @param pos_event Position event
178 * @return @c ui_claimed iff the event is claimed
179 */
180ui_evclaim_t ui_image_ctl_pos_event(void *arg, pos_event_t *event)
181{
182 ui_image_t *image = (ui_image_t *) arg;
183
184 (void) image;
185 return ui_unclaimed;
186}
187
188/** @}
189 */
Note: See TracBrowser for help on using the repository browser.