[e31ea20d] | 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 <malloc.h>
|
---|
| 37 | #include <transform.h>
|
---|
| 38 | #include <source.h>
|
---|
| 39 | #include <surface.h>
|
---|
| 40 | #include <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, false);
|
---|
| 61 |
|
---|
| 62 | drawctx_t drawctx;
|
---|
| 63 | drawctx_init(&drawctx, surface);
|
---|
| 64 |
|
---|
| 65 | drawctx_set_source(&drawctx, &source);
|
---|
| 66 | drawctx_transfer(&drawctx, widget->hpos, widget->vpos, widget->width,
|
---|
| 67 | widget->height);
|
---|
| 68 |
|
---|
| 69 | window_yield(canvas->widget.window);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void deinit_canvas(canvas_t *canvas)
|
---|
| 73 | {
|
---|
| 74 | widget_deinit(&canvas->widget);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static void canvas_destroy(widget_t *widget)
|
---|
| 78 | {
|
---|
| 79 | canvas_t *canvas = (canvas_t *) widget;
|
---|
| 80 |
|
---|
| 81 | deinit_canvas(canvas);
|
---|
| 82 | free(canvas);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static void canvas_reconfigure(widget_t *widget)
|
---|
| 86 | {
|
---|
| 87 | /* No-op */
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | static void canvas_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
|
---|
| 91 | sysarg_t width, sysarg_t height)
|
---|
| 92 | {
|
---|
| 93 | canvas_t *canvas = (canvas_t *) widget;
|
---|
| 94 |
|
---|
| 95 | widget_modify(widget, hpos, vpos, canvas->width, canvas->height);
|
---|
| 96 | paint_internal(widget);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | static void canvas_repaint(widget_t *widget)
|
---|
| 100 | {
|
---|
| 101 | paint_internal(widget);
|
---|
| 102 | window_damage(widget->window);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | static void canvas_handle_keyboard_event(widget_t *widget, kbd_event_t event)
|
---|
| 106 | {
|
---|
[d65be39] | 107 | canvas_t *canvas = (canvas_t *) widget;
|
---|
| 108 |
|
---|
| 109 | sig_send(&canvas->keyboard_event, &event);
|
---|
[e31ea20d] | 110 | }
|
---|
| 111 |
|
---|
| 112 | static void canvas_handle_position_event(widget_t *widget, pos_event_t event)
|
---|
| 113 | {
|
---|
[a0ff947] | 114 | canvas_t *canvas = (canvas_t *) widget;
|
---|
| 115 | pos_event_t tevent;
|
---|
| 116 |
|
---|
| 117 | tevent = event;
|
---|
| 118 | tevent.hpos -= widget->hpos;
|
---|
| 119 | tevent.vpos -= widget->vpos;
|
---|
| 120 |
|
---|
| 121 | sig_send(&canvas->position_event, &tevent);
|
---|
[e31ea20d] | 122 | }
|
---|
| 123 |
|
---|
| 124 | bool init_canvas(canvas_t *canvas, widget_t *parent, sysarg_t width,
|
---|
| 125 | sysarg_t height, surface_t *surface)
|
---|
| 126 | {
|
---|
| 127 | widget_init(&canvas->widget, parent);
|
---|
| 128 |
|
---|
| 129 | canvas->widget.width = width;
|
---|
| 130 | canvas->widget.height = height;
|
---|
| 131 |
|
---|
| 132 | canvas->widget.width_min = width;
|
---|
| 133 | canvas->widget.height_min = height;
|
---|
| 134 | canvas->widget.width_ideal = width;
|
---|
| 135 | canvas->widget.height_ideal = height;
|
---|
| 136 | canvas->widget.width_max = width;
|
---|
| 137 | canvas->widget.height_max = height;
|
---|
| 138 |
|
---|
| 139 | canvas->widget.destroy = canvas_destroy;
|
---|
| 140 | canvas->widget.reconfigure = canvas_reconfigure;
|
---|
| 141 | canvas->widget.rearrange = canvas_rearrange;
|
---|
| 142 | canvas->widget.repaint = canvas_repaint;
|
---|
| 143 | canvas->widget.handle_keyboard_event = canvas_handle_keyboard_event;
|
---|
| 144 | canvas->widget.handle_position_event = canvas_handle_position_event;
|
---|
| 145 |
|
---|
| 146 | canvas->width = width;
|
---|
| 147 | canvas->height = height;
|
---|
| 148 | canvas->surface = surface;
|
---|
| 149 |
|
---|
| 150 | return true;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[d65be39] | 153 | bool update_canvas(canvas_t *canvas, surface_t *surface)
|
---|
| 154 | {
|
---|
| 155 | if (surface != NULL)
|
---|
| 156 | canvas->surface = surface;
|
---|
| 157 |
|
---|
| 158 | canvas_repaint(&canvas->widget);
|
---|
| 159 | return true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[e31ea20d] | 162 | canvas_t *create_canvas(widget_t *parent, sysarg_t width, sysarg_t height,
|
---|
| 163 | surface_t *surface)
|
---|
| 164 | {
|
---|
| 165 | canvas_t *canvas = (canvas_t *) malloc(sizeof(canvas_t));
|
---|
| 166 | if (!canvas)
|
---|
| 167 | return NULL;
|
---|
| 168 |
|
---|
| 169 | if (init_canvas(canvas, parent, width, height, surface))
|
---|
| 170 | return canvas;
|
---|
| 171 |
|
---|
| 172 | free(canvas);
|
---|
| 173 | return NULL;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /** @}
|
---|
| 177 | */
|
---|