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

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

UI demo should demonstrate image and entry controls

We also add the ability to draw a frame around image control, use
in UI demo and in launcher.

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[f93e4e3]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
[d8ddf7a]126/** Set image flags.
127 *
128 * @param image Image
129 * @param flags Flags
130 */
131void ui_image_set_flags(ui_image_t *image, ui_image_flags_t flags)
132{
133 image->flags = flags;
134}
135
[f93e4e3]136/** Paint image.
137 *
138 * @param image Image
139 * @return EOK on success or an error code
140 */
141errno_t ui_image_paint(ui_image_t *image)
142{
[d8ddf7a]143 gfx_rect_t irect;
[f93e4e3]144 gfx_rect_t srect;
145 gfx_coord2_t offs;
[d8ddf7a]146 errno_t rc;
147
148 if ((image->flags & ui_imgf_frame) != 0) {
149 rc = ui_paint_bevel(image->res->gc, &image->rect,
150 image->res->btn_frame_color, image->res->btn_frame_color,
151 1, NULL);
152 if (rc != EOK)
153 return rc;
154 }
[f93e4e3]155
[12008adf]156 if (image->bitmap == NULL)
157 return EOK;
158
[d8ddf7a]159 irect = image->rect;
160 if ((image->flags & ui_imgf_frame) != 0) {
161 irect.p0.x++;
162 irect.p0.y++;
163 irect.p1.x--;
164 irect.p1.y--;
165 }
166
[f93e4e3]167 /*
168 * UI image position does not depend on bitmap rectangle p0, so
169 * we need to subtract it.
170 */
[d8ddf7a]171 offs.x = irect.p0.x - image->brect.p0.x;
172 offs.y = irect.p0.y - image->brect.p0.y;
[f93e4e3]173
174 /*
[d8ddf7a]175 * Translate image rectangle back to bitmap coordinate space.
[f93e4e3]176 * Thus the bitmap will be clipped to the image rectangle.
177 */
[d8ddf7a]178 gfx_rect_rtranslate(&offs, &irect, &srect);
[f93e4e3]179 return gfx_bitmap_render(image->bitmap, &srect, &offs);
[d8ddf7a]180
[f93e4e3]181}
182
[12008adf]183/** Change image bitmap.
184 *
185 * Note that the caller must have saved the pointer to the previous bitmap
186 * in the image, because this causes it to be unlinked from the image and
187 * not destroyed (the ownership is transferred back to the caller).
188 *
189 * @param image Image
190 * @param bitmap New bitmap (ownership transferred to image) or @c NULL
191 * @param brect New bitmap rectangle
192 */
193void ui_image_set_bmp(ui_image_t *image, gfx_bitmap_t *bitmap,
194 gfx_rect_t *brect)
195{
196 image->bitmap = bitmap;
197 image->brect = *brect;
198}
199
[f93e4e3]200/** Destroy image control.
201 *
202 * @param arg Argument (ui_image_t *)
203 */
204void ui_image_ctl_destroy(void *arg)
205{
206 ui_image_t *image = (ui_image_t *) arg;
207
208 ui_image_destroy(image);
209}
210
211/** Paint image control.
212 *
213 * @param arg Argument (ui_image_t *)
214 * @return EOK on success or an error code
215 */
216errno_t ui_image_ctl_paint(void *arg)
217{
218 ui_image_t *image = (ui_image_t *) arg;
219
220 return ui_image_paint(image);
221}
222
223/** Handle image control position event.
224 *
225 * @param arg Argument (ui_image_t *)
226 * @param pos_event Position event
227 * @return @c ui_claimed iff the event is claimed
228 */
229ui_evclaim_t ui_image_ctl_pos_event(void *arg, pos_event_t *event)
230{
231 ui_image_t *image = (ui_image_t *) arg;
232
233 (void) image;
234 return ui_unclaimed;
235}
236
237/** @}
238 */
Note: See TracBrowser for help on using the repository browser.