~jsvoboda/helenos/dnsr

« back to all changes in this revision

Viewing changes to uspace/lib/gui/canvas.c

  • Committer: Jiri Svoboda
  • Date: 2013-04-19 18:38:18 UTC
  • mfrom: (1527.1.284 main-clone)
  • Revision ID: jiri@wiwaxia-20130419183818-nvfibuh4t5qol0e3
MergeĀ mainlineĀ chages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
{
 
107
        /* No-op */
 
108
}
 
109
 
 
110
static void canvas_handle_position_event(widget_t *widget, pos_event_t event)
 
111
{
 
112
        /* No-op */
 
113
}
 
114
 
 
115
bool init_canvas(canvas_t *canvas, widget_t *parent, sysarg_t width,
 
116
    sysarg_t height, surface_t *surface)
 
117
{
 
118
        widget_init(&canvas->widget, parent);
 
119
        
 
120
        canvas->widget.width = width;
 
121
        canvas->widget.height = height;
 
122
        
 
123
        canvas->widget.width_min = width;
 
124
        canvas->widget.height_min = height;
 
125
        canvas->widget.width_ideal = width;
 
126
        canvas->widget.height_ideal = height;
 
127
        canvas->widget.width_max = width;
 
128
        canvas->widget.height_max = height;
 
129
        
 
130
        canvas->widget.destroy = canvas_destroy;
 
131
        canvas->widget.reconfigure = canvas_reconfigure;
 
132
        canvas->widget.rearrange = canvas_rearrange;
 
133
        canvas->widget.repaint = canvas_repaint;
 
134
        canvas->widget.handle_keyboard_event = canvas_handle_keyboard_event;
 
135
        canvas->widget.handle_position_event = canvas_handle_position_event;
 
136
        
 
137
        canvas->width = width;
 
138
        canvas->height = height;
 
139
        canvas->surface = surface;
 
140
        
 
141
        return true;
 
142
}
 
143
 
 
144
canvas_t *create_canvas(widget_t *parent, sysarg_t width, sysarg_t height,
 
145
    surface_t *surface)
 
146
{
 
147
        canvas_t *canvas = (canvas_t *) malloc(sizeof(canvas_t));
 
148
        if (!canvas)
 
149
                return NULL;
 
150
        
 
151
        if (init_canvas(canvas, parent, width, height, surface))
 
152
                return canvas;
 
153
        
 
154
        free(canvas);
 
155
        return NULL;
 
156
}
 
157
 
 
158
/** @}
 
159
 */