summaryrefslogtreecommitdiffstats
path: root/panels/wacom/cc-drawing-area.c
blob: 6c635977f1d46b849a2b0735e63061e0731ca22f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright © 2016 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */

#include "config.h"
#include <cairo/cairo.h>
#include "cc-drawing-area.h"

typedef struct _CcDrawingArea CcDrawingArea;

struct _CcDrawingArea {
	GtkEventBox parent;
	GdkDevice *current_device;
	cairo_surface_t *surface;
	cairo_t *cr;
};

G_DEFINE_TYPE (CcDrawingArea, cc_drawing_area, GTK_TYPE_EVENT_BOX)

static void
ensure_drawing_surface (CcDrawingArea *area,
			gint           width,
			gint           height)
{
	if (!area->surface ||
	    cairo_image_surface_get_width (area->surface) != width ||
	    cairo_image_surface_get_height (area->surface) != height) {
		cairo_surface_t *surface;

		surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
						      width, height);
		if (area->surface) {
			cairo_t *cr;

			cr = cairo_create (surface);
			cairo_set_source_surface (cr, area->surface, 0, 0);
			cairo_paint (cr);

			cairo_surface_destroy (area->surface);
			cairo_destroy (area->cr);
			cairo_destroy (cr);
		}

		area->surface = surface;
		area->cr = cairo_create (surface);
	}
}

static void
cc_drawing_area_size_allocate (GtkWidget     *widget,
			       GtkAllocation *allocation)
{
	CcDrawingArea *area = CC_DRAWING_AREA (widget);

	ensure_drawing_surface (area, allocation->width, allocation->height);

	GTK_WIDGET_CLASS (cc_drawing_area_parent_class)->size_allocate (widget,
									allocation);
}

static void
cc_drawing_area_map (GtkWidget *widget)
{
	GtkAllocation allocation;

	GTK_WIDGET_CLASS (cc_drawing_area_parent_class)->map (widget);

	gtk_widget_get_allocation (widget, &allocation);
	ensure_drawing_surface (CC_DRAWING_AREA (widget),
				allocation.width, allocation.height);
}

static void
cc_drawing_area_unmap (GtkWidget *widget)
{
	CcDrawingArea *area = CC_DRAWING_AREA (widget);

	if (area->cr) {
		cairo_destroy (area->cr);
		area->cr = NULL;
	}

	if (area->surface) {
		cairo_surface_destroy (area->surface);
		area->surface = NULL;
	}

	GTK_WIDGET_CLASS (cc_drawing_area_parent_class)->unmap (widget);
}

static gboolean
cc_drawing_area_draw (GtkWidget *widget,
		      cairo_t   *cr)
{
	CcDrawingArea *area = CC_DRAWING_AREA (widget);
	GtkAllocation allocation;

	GTK_WIDGET_CLASS (cc_drawing_area_parent_class)->draw (widget, cr);

	gtk_widget_get_allocation (widget, &allocation);
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_paint (cr);

	cairo_set_source_surface (cr, area->surface, 0, 0);
	cairo_paint (cr);

	cairo_set_source_rgb (cr, 0.6, 0.6, 0.6);
	cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
	cairo_stroke (cr);

	return FALSE;
}

static gboolean
cc_drawing_area_event (GtkWidget *widget,
		       GdkEvent  *event)
{
	CcDrawingArea *area = CC_DRAWING_AREA (widget);
	GdkInputSource source;
	GdkDeviceTool *tool;
	GdkDevice *device;

	device = gdk_event_get_source_device (event);

	if (!device)
		return GDK_EVENT_PROPAGATE;

	source = gdk_device_get_source (device);
	tool = gdk_event_get_device_tool (event);

	if (source != GDK_SOURCE_PEN && source != GDK_SOURCE_ERASER)
		return GDK_EVENT_PROPAGATE;

	if (area->current_device && area->current_device != device)
		return GDK_EVENT_PROPAGATE;

	if (event->type == GDK_BUTTON_PRESS &&
	    event->button.button == 1 && !area->current_device) {
		area->current_device = device;
	} else if (event->type == GDK_BUTTON_RELEASE &&
		   event->button.button == 1 && area->current_device) {
		cairo_new_path (area->cr);
		area->current_device = NULL;
	} else if (event->type == GDK_MOTION_NOTIFY &&
		   event->motion.state & GDK_BUTTON1_MASK) {
		gdouble x, y, pressure;

		gdk_event_get_coords (event, &x, &y);
		gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &pressure);

		if (gdk_device_tool_get_tool_type (tool) == GDK_DEVICE_TOOL_TYPE_ERASER) {
			cairo_set_line_width (area->cr, 10 * pressure);
			cairo_set_operator (area->cr, CAIRO_OPERATOR_DEST_OUT);
		} else {
			cairo_set_line_width (area->cr, 4 * pressure);
			cairo_set_operator (area->cr, CAIRO_OPERATOR_SATURATE);
		}

		cairo_set_source_rgba (area->cr, 0, 0, 0, pressure);
		cairo_line_to (area->cr, x, y);
		cairo_stroke (area->cr);

		cairo_move_to (area->cr, x, y);

		gtk_widget_queue_draw (widget);

		return GDK_EVENT_STOP;
	}

	return GDK_EVENT_PROPAGATE;
}

static void
cc_drawing_area_class_init (CcDrawingAreaClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	widget_class->size_allocate = cc_drawing_area_size_allocate;
	widget_class->draw = cc_drawing_area_draw;
	widget_class->event = cc_drawing_area_event;
	widget_class->map = cc_drawing_area_map;
	widget_class->unmap = cc_drawing_area_unmap;
}

static void
cc_drawing_area_init (CcDrawingArea *area)
{
	gtk_event_box_set_above_child (GTK_EVENT_BOX (area), TRUE);
	gtk_widget_add_events (GTK_WIDGET (area),
			       GDK_BUTTON_PRESS_MASK |
			       GDK_BUTTON_RELEASE_MASK |
			       GDK_POINTER_MOTION_MASK);
}

GtkWidget *
cc_drawing_area_new (void)
{
	return g_object_new (CC_TYPE_DRAWING_AREA, NULL);
}