Debian Codegraph @

demos/gtk-demo/paint.c

raw
1 /* Paint
2 *
3 * Demonstrates practical handling of drawing tablets in a real world
4 * usecase.
5 */
6 #include <gtk/gtk.h>
7
8 typedef struct
9 {
10 GtkEventBox parent_instance;
11 cairo_surface_t *surface;
12 cairo_t *cr;
13 GdkRGBA draw_color;
14
15 GtkGesture *stylus_gesture;
16 } DrawingArea;
17
18 typedef struct
19 {
20 GtkEventBoxClass parent_class;
21 } DrawingAreaClass;
22
23 G_DEFINE_TYPE (DrawingArea, drawing_area, GTK_TYPE_EVENT_BOX)
24
25 static void
26 drawing_area_ensure_surface (DrawingArea *area,
27 gint width,
28 gint height)
29 {
30 if (!area->surface ||
31 cairo_image_surface_get_width (area->surface) != width ||
32 cairo_image_surface_get_height (area->surface) != height)
33 {
34 cairo_surface_t *surface;
35
36 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
37 width, height);
38 if (area->surface)
39 {
40 cairo_t *cr;
41
42 cr = cairo_create (surface);
43 cairo_set_source_surface (cr, area->surface, 0, 0);
44 cairo_paint (cr);
45
46 cairo_surface_destroy (area->surface);
47 cairo_destroy (area->cr);
48 cairo_destroy (cr);
49 }
50
51 area->surface = surface;
52 area->cr = cairo_create (surface);
53 }
54 }
55
56 static void
57 drawing_area_size_allocate (GtkWidget *widget,
58 GtkAllocation *allocation)
59 {
60 DrawingArea *area = (DrawingArea *) widget;
61
62 drawing_area_ensure_surface (area, allocation->width, allocation->height);
63
64 GTK_WIDGET_CLASS (drawing_area_parent_class)->size_allocate (widget, allocation);
65 }
66
67 static void
68 drawing_area_map (GtkWidget *widget)
69 {
70 GtkAllocation allocation;
71
72 GTK_WIDGET_CLASS (drawing_area_parent_class)->map (widget);
73
74 gdk_window_set_event_compression (gtk_widget_get_window (widget), TRUE);
75
76 gtk_widget_get_allocation (widget, &allocation);
77 drawing_area_ensure_surface ((DrawingArea *) widget,
78 allocation.width, allocation.height);
79 }
80
81 static void
82 drawing_area_unmap (GtkWidget *widget)
83 {
84 DrawingArea *area = (DrawingArea *) widget;
85
86 g_clear_pointer (&area->cr, cairo_destroy);
87 g_clear_pointer (&area->surface, cairo_surface_destroy);
88
89 GTK_WIDGET_CLASS (drawing_area_parent_class)->unmap (widget);
90 }
91
92 static gboolean
93 drawing_area_draw (GtkWidget *widget,
94 cairo_t *cr)
95 {
96 DrawingArea *area = (DrawingArea *) widget;
97 GtkAllocation allocation;
98
99 gtk_widget_get_allocation (widget, &allocation);
100
101 cairo_set_source_rgb (cr, 1, 1, 1);
102 cairo_paint (cr);
103
104 cairo_set_source_surface (cr, area->surface, 0, 0);
105 cairo_paint (cr);
106
107 cairo_set_source_rgb (cr, 0.6, 0.6, 0.6);
108 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
109 cairo_stroke (cr);
110
111 return TRUE;
112 }
113
114 static void
115 drawing_area_class_init (DrawingAreaClass *klass)
116 {
117 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
118
119 widget_class->size_allocate = drawing_area_size_allocate;
120 widget_class->draw = drawing_area_draw;
121 widget_class->map = drawing_area_map;
122 widget_class->unmap = drawing_area_unmap;
123 }
124
125 static void
126 drawing_area_apply_stroke (DrawingArea *area,
127 GdkDeviceTool *tool,
128 gdouble x,
129 gdouble y,
130 gdouble pressure)
131 {
132 if (gdk_device_tool_get_tool_type (tool) == GDK_DEVICE_TOOL_TYPE_ERASER)
133 {
134 cairo_set_line_width (area->cr, 10 * pressure);
135 cairo_set_operator (area->cr, CAIRO_OPERATOR_DEST_OUT);
136 }
137 else
138 {
139 cairo_set_line_width (area->cr, 4 * pressure);
140 cairo_set_operator (area->cr, CAIRO_OPERATOR_SATURATE);
141 }
142
143 cairo_set_source_rgba (area->cr, area->draw_color.red,
144 area->draw_color.green, area->draw_color.blue,
145 area->draw_color.alpha * pressure);
146
147 //cairo_set_source_rgba (area->cr, 0, 0, 0, pressure);
148
149 cairo_line_to (area->cr, x, y);
150 cairo_stroke (area->cr);
151 cairo_move_to (area->cr, x, y);
152 }
153
154 static void
155 stylus_gesture_down (GtkGestureStylus *gesture,
156 gdouble x,
157 gdouble y,
158 DrawingArea *area)
159 {
160 cairo_new_path (area->cr);
161 }
162
163 static void
164 stylus_gesture_motion (GtkGestureStylus *gesture,
165 gdouble x,
166 gdouble y,
167 DrawingArea *area)
168 {
169 GdkDeviceTool *tool;
170 gdouble pressure;
171
172 tool = gtk_gesture_stylus_get_device_tool (gesture);
173
174 if (!gtk_gesture_stylus_get_axis (gesture, GDK_AXIS_PRESSURE, &pressure))
175 pressure = 1;
176
177 drawing_area_apply_stroke (area, tool, x, y, pressure);
178 gtk_widget_queue_draw (GTK_WIDGET (area));
179 }
180
181 static void
182 drawing_area_init (DrawingArea *area)
183 {
184 const GdkRGBA draw_rgba = { 0, 0, 0, 1 };
185 gtk_event_box_set_visible_window (GTK_EVENT_BOX (area), TRUE);
186
187 area->stylus_gesture = gtk_gesture_stylus_new (GTK_WIDGET (area));
188 g_signal_connect (area->stylus_gesture, "down",
189 G_CALLBACK (stylus_gesture_down), area);
190 g_signal_connect (area->stylus_gesture, "motion",
191 G_CALLBACK (stylus_gesture_motion), area);
192
193 area->draw_color = draw_rgba;
194 }
195
196 GtkWidget *
197 drawing_area_new (void)
198 {
199 return g_object_new (drawing_area_get_type (), NULL);
200 }
201
202 void
203 drawing_area_set_color (DrawingArea *area,
204 GdkRGBA *color)
205 {
206 area->draw_color = *color;
207 }
208
209 static void
210 color_button_color_set (GtkColorButton *button,
211 DrawingArea *draw_area)
212 {
213 GdkRGBA color;
214
215 gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &color);
216 drawing_area_set_color (draw_area, &color);
217 }
218
219 GtkWidget *
220 do_paint (GtkWidget *toplevel)
221 {
222 static GtkWidget *window = NULL;
223
224 if (!window)
225 {
226 GtkWidget *draw_area, *headerbar, *colorbutton;
227 const GdkRGBA draw_rgba = { 0, 0, 0, 1 };
228
229 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
230
231 draw_area = drawing_area_new ();
232 gtk_container_add (GTK_CONTAINER (window), draw_area);
233
234 headerbar = gtk_header_bar_new ();
235 gtk_header_bar_set_title (GTK_HEADER_BAR (headerbar), "Paint");
236 gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (headerbar), TRUE);
237
238 colorbutton = gtk_color_button_new ();
239 g_signal_connect (colorbutton, "color-set",
240 G_CALLBACK (color_button_color_set), draw_area);
241 gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (colorbutton),
242 &draw_rgba);
243
244 gtk_header_bar_pack_end (GTK_HEADER_BAR (headerbar), colorbutton);
245 gtk_window_set_titlebar (GTK_WINDOW (window), headerbar);
246
247 g_signal_connect (window, "destroy",
248 G_CALLBACK (gtk_widget_destroyed), &window);
249 }
250
251 if (!gtk_widget_get_visible (window))
252 gtk_widget_show_all (window);
253 else
254 gtk_widget_destroy (window);
255
256 return window;
257 }