summaryrefslogtreecommitdiffstats
path: root/panels/wacom/cc-drawing-area.c
blob: 3f570ad667607a9f92ffc8712fd7f83cb2d8becd (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
/*
 * 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 {
	GtkDrawingArea parent;
	GtkGesture *stylus_gesture;
	cairo_surface_t *surface;
	cairo_t *cr;
};

G_DEFINE_TYPE (CcDrawingArea, cc_drawing_area, GTK_TYPE_DRAWING_AREA)

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_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 void
draw_cb (GtkDrawingArea *drawing_area,
         cairo_t   *cr,
         gint       width,
         gint       height,
         gpointer   user_data)
{
	CcDrawingArea *area = CC_DRAWING_AREA (drawing_area);

	ensure_drawing_surface (area, width, height);

	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, width, height);
	cairo_stroke (cr);
}

static void
stylus_down_cb (GtkGestureStylus *gesture,
		double            x,
		double            y,
		CcDrawingArea    *area)
{
	cairo_new_path (area->cr);
}

static void
stylus_motion_cb (GtkGestureStylus *gesture,
		  double            x,
		  double            y,
		  CcDrawingArea    *area)
{
	GdkDeviceTool *tool;
	gdouble pressure;

	tool = gtk_gesture_stylus_get_device_tool (gesture);
	gtk_gesture_stylus_get_axis (gesture,
				     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 (GTK_WIDGET (area));
}

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

	widget_class->map = cc_drawing_area_map;
	widget_class->unmap = cc_drawing_area_unmap;
}

static void
cc_drawing_area_init (CcDrawingArea *area)
{
	gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area), draw_cb, NULL, NULL);
	area->stylus_gesture = gtk_gesture_stylus_new ();
	g_signal_connect (area->stylus_gesture, "down",
			  G_CALLBACK (stylus_down_cb), area);
	g_signal_connect (area->stylus_gesture, "motion",
			  G_CALLBACK (stylus_motion_cb), area);
	gtk_widget_add_controller (GTK_WIDGET (area),
				   GTK_EVENT_CONTROLLER (area->stylus_gesture));
}

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