1 | /*
|
---|
2 | * Copyright (c) 2013 Martin Decky
|
---|
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 gui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <transform.h>
|
---|
38 | #include <draw/source.h>
|
---|
39 | #include <draw/surface.h>
|
---|
40 | #include <draw/drawctx.h>
|
---|
41 | #include "window.h"
|
---|
42 | #include "canvas.h"
|
---|
43 |
|
---|
44 | static void paint_internal(widget_t *widget)
|
---|
45 | {
|
---|
46 | canvas_t *canvas = (canvas_t *) widget;
|
---|
47 |
|
---|
48 | surface_t *surface = window_claim(canvas->widget.window);
|
---|
49 | if (!surface) {
|
---|
50 | window_yield(canvas->widget.window);
|
---|
51 | }
|
---|
52 |
|
---|
53 | transform_t transform;
|
---|
54 | transform_identity(&transform);
|
---|
55 | transform_translate(&transform, widget->hpos, widget->vpos);
|
---|
56 |
|
---|
57 | source_t source;
|
---|
58 | source_init(&source);
|
---|
59 | source_set_transform(&source, transform);
|
---|
60 | source_set_texture(&source, canvas->surface,
|
---|
61 | PIXELMAP_EXTEND_TRANSPARENT_BLACK);
|
---|
62 |
|
---|
63 | drawctx_t drawctx;
|
---|
64 | drawctx_init(&drawctx, surface);
|
---|
65 |
|
---|
66 | drawctx_set_source(&drawctx, &source);
|
---|
67 | drawctx_transfer(&drawctx, widget->hpos, widget->vpos, widget->width,
|
---|
68 | widget->height);
|
---|
69 |
|
---|
70 | window_yield(canvas->widget.window);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void deinit_canvas(canvas_t *canvas)
|
---|
74 | {
|
---|
75 | widget_deinit(&canvas->widget);
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void canvas_destroy(widget_t *widget)
|
---|
79 | {
|
---|
80 | canvas_t *canvas = (canvas_t *) widget;
|
---|
81 |
|
---|
82 | deinit_canvas(canvas);
|
---|
83 | free(canvas);
|
---|
84 | }
|
---|
85 |
|
---|
86 | static void canvas_reconfigure(widget_t *widget)
|
---|
87 | {
|
---|
88 | /* No-op */
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void canvas_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
|
---|
92 | sysarg_t width, sysarg_t height)
|
---|
93 | {
|
---|
94 | canvas_t *canvas = (canvas_t *) widget;
|
---|
95 |
|
---|
96 | widget_modify(widget, hpos, vpos, canvas->width, canvas->height);
|
---|
97 | paint_internal(widget);
|
---|
98 | }
|
---|
99 |
|
---|
100 | static void canvas_repaint(widget_t *widget)
|
---|
101 | {
|
---|
102 | paint_internal(widget);
|
---|
103 | window_damage(widget->window);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void canvas_handle_keyboard_event(widget_t *widget, kbd_event_t event)
|
---|
107 | {
|
---|
108 | canvas_t *canvas = (canvas_t *) widget;
|
---|
109 |
|
---|
110 | sig_send(&canvas->keyboard_event, &event);
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void canvas_handle_position_event(widget_t *widget, pos_event_t event)
|
---|
114 | {
|
---|
115 | canvas_t *canvas = (canvas_t *) widget;
|
---|
116 | pos_event_t tevent;
|
---|
117 |
|
---|
118 | tevent = event;
|
---|
119 | tevent.hpos -= widget->hpos;
|
---|
120 | tevent.vpos -= widget->vpos;
|
---|
121 |
|
---|
122 | sig_send(&canvas->position_event, &tevent);
|
---|
123 | }
|
---|
124 |
|
---|
125 | bool init_canvas(canvas_t *canvas, widget_t *parent, const void *data,
|
---|
126 | sysarg_t width, sysarg_t height, surface_t *surface)
|
---|
127 | {
|
---|
128 | widget_init(&canvas->widget, parent, data);
|
---|
129 |
|
---|
130 | canvas->widget.width = width;
|
---|
131 | canvas->widget.height = height;
|
---|
132 |
|
---|
133 | canvas->widget.width_min = width;
|
---|
134 | canvas->widget.height_min = height;
|
---|
135 | canvas->widget.width_ideal = width;
|
---|
136 | canvas->widget.height_ideal = height;
|
---|
137 | canvas->widget.width_max = width;
|
---|
138 | canvas->widget.height_max = height;
|
---|
139 |
|
---|
140 | canvas->widget.destroy = canvas_destroy;
|
---|
141 | canvas->widget.reconfigure = canvas_reconfigure;
|
---|
142 | canvas->widget.rearrange = canvas_rearrange;
|
---|
143 | canvas->widget.repaint = canvas_repaint;
|
---|
144 | canvas->widget.handle_keyboard_event = canvas_handle_keyboard_event;
|
---|
145 | canvas->widget.handle_position_event = canvas_handle_position_event;
|
---|
146 |
|
---|
147 | canvas->width = width;
|
---|
148 | canvas->height = height;
|
---|
149 | canvas->surface = surface;
|
---|
150 |
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 |
|
---|
154 | bool update_canvas(canvas_t *canvas, surface_t *surface)
|
---|
155 | {
|
---|
156 | if (surface != NULL)
|
---|
157 | canvas->surface = surface;
|
---|
158 |
|
---|
159 | canvas_repaint(&canvas->widget);
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | canvas_t *create_canvas(widget_t *parent, const void *data, sysarg_t width,
|
---|
164 | sysarg_t height, surface_t *surface)
|
---|
165 | {
|
---|
166 | canvas_t *canvas = (canvas_t *) malloc(sizeof(canvas_t));
|
---|
167 | if (!canvas)
|
---|
168 | return NULL;
|
---|
169 |
|
---|
170 | if (init_canvas(canvas, parent, data, width, height, surface))
|
---|
171 | return canvas;
|
---|
172 |
|
---|
173 | free(canvas);
|
---|
174 | return NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** @}
|
---|
178 | */
|
---|