| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <gtk/gtk.h> |
| 17 |
|
| 18 |
static GtkWidget *window = NULL; |
| 19 |
|
| 20 |
static cairo_surface_t *surface = NULL; |
| 21 |
|
| 22 |
|
| 23 |
static gboolean |
| 24 |
scribble_configure_event (GtkWidget *widget, |
| 25 |
GdkEventConfigure *event, |
| 26 |
gpointer data) |
| 27 |
{ |
| 28 |
cairo_t *cr; |
| 29 |
GtkAllocation allocation; |
| 30 |
|
| 31 |
gtk_widget_get_allocation (widget, &allocation); |
| 32 |
|
| 33 |
if (surface) |
| 34 |
cairo_surface_destroy (surface); |
| 35 |
|
| 36 |
surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget), |
| 37 |
CAIRO_CONTENT_COLOR, |
| 38 |
allocation.width, |
| 39 |
allocation.height); |
| 40 |
|
| 41 |
|
| 42 |
cr = cairo_create (surface); |
| 43 |
|
| 44 |
cairo_set_source_rgb (cr, 1, 1, 1); |
| 45 |
cairo_paint (cr); |
| 46 |
|
| 47 |
cairo_destroy (cr); |
| 48 |
|
| 49 |
|
| 50 |
return TRUE; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static gboolean |
| 55 |
scribble_expose_event (GtkWidget *widget, |
| 56 |
GdkEventExpose *event, |
| 57 |
gpointer data) |
| 58 |
{ |
| 59 |
cairo_t *cr; |
| 60 |
|
| 61 |
cr = gdk_cairo_create (gtk_widget_get_window (widget)); |
| 62 |
|
| 63 |
cairo_set_source_surface (cr, surface, 0, 0); |
| 64 |
gdk_cairo_rectangle (cr, &event->area); |
| 65 |
cairo_fill (cr); |
| 66 |
|
| 67 |
cairo_destroy (cr); |
| 68 |
|
| 69 |
return FALSE; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static void |
| 74 |
draw_brush (GtkWidget *widget, |
| 75 |
gdouble x, |
| 76 |
gdouble y) |
| 77 |
{ |
| 78 |
GdkRectangle update_rect; |
| 79 |
cairo_t *cr; |
| 80 |
|
| 81 |
update_rect.x = x - 3; |
| 82 |
update_rect.y = y - 3; |
| 83 |
update_rect.width = 6; |
| 84 |
update_rect.height = 6; |
| 85 |
|
| 86 |
|
| 87 |
cr = cairo_create (surface); |
| 88 |
|
| 89 |
gdk_cairo_rectangle (cr, &update_rect); |
| 90 |
cairo_fill (cr); |
| 91 |
|
| 92 |
cairo_destroy (cr); |
| 93 |
|
| 94 |
|
| 95 |
gdk_window_invalidate_rect (gtk_widget_get_window (widget), |
| 96 |
&update_rect, |
| 97 |
FALSE); |
| 98 |
} |
| 99 |
|
| 100 |
static gboolean |
| 101 |
scribble_button_press_event (GtkWidget *widget, |
| 102 |
GdkEventButton *event, |
| 103 |
gpointer data) |
| 104 |
{ |
| 105 |
if (surface == NULL) |
| 106 |
return FALSE; |
| 107 |
|
| 108 |
if (event->button == 1) |
| 109 |
draw_brush (widget, event->x, event->y); |
| 110 |
|
| 111 |
|
| 112 |
return TRUE; |
| 113 |
} |
| 114 |
|
| 115 |
static gboolean |
| 116 |
scribble_motion_notify_event (GtkWidget *widget, |
| 117 |
GdkEventMotion *event, |
| 118 |
gpointer data) |
| 119 |
{ |
| 120 |
int x, y; |
| 121 |
GdkModifierType state; |
| 122 |
|
| 123 |
if (surface == NULL) |
| 124 |
return FALSE; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
gdk_window_get_pointer (event->window, &x, &y, &state); |
| 138 |
|
| 139 |
if (state & GDK_BUTTON1_MASK) |
| 140 |
draw_brush (widget, x, y); |
| 141 |
|
| 142 |
|
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
static gboolean |
| 148 |
checkerboard_expose (GtkWidget *da, |
| 149 |
GdkEventExpose *event, |
| 150 |
gpointer data) |
| 151 |
{ |
| 152 |
gint i, j, xcount, ycount; |
| 153 |
cairo_t *cr; |
| 154 |
GtkAllocation allocation; |
| 155 |
|
| 156 |
gtk_widget_get_allocation (da, &allocation); |
| 157 |
|
| 158 |
#define CHECK_SIZE 10 |
| 159 |
#define SPACING 2 |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
cr = gdk_cairo_create (gtk_widget_get_window (da)); |
| 169 |
gdk_cairo_rectangle (cr, &event->area); |
| 170 |
cairo_clip (cr); |
| 171 |
|
| 172 |
xcount = 0; |
| 173 |
i = SPACING; |
| 174 |
while (i < allocation.width) |
| 175 |
{ |
| 176 |
j = SPACING; |
| 177 |
ycount = xcount % 2; |
| 178 |
while (j < allocation.height) |
| 179 |
{ |
| 180 |
if (ycount % 2) |
| 181 |
cairo_set_source_rgb (cr, 0.45777, 0, 0.45777); |
| 182 |
else |
| 183 |
cairo_set_source_rgb (cr, 1, 1, 1); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE); |
| 190 |
cairo_fill (cr); |
| 191 |
|
| 192 |
j += CHECK_SIZE + SPACING; |
| 193 |
++ycount; |
| 194 |
} |
| 195 |
|
| 196 |
i += CHECK_SIZE + SPACING; |
| 197 |
++xcount; |
| 198 |
} |
| 199 |
|
| 200 |
cairo_destroy (cr); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
return TRUE; |
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
close_window (void) |
| 210 |
{ |
| 211 |
window = NULL; |
| 212 |
|
| 213 |
if (surface) |
| 214 |
cairo_surface_destroy (surface); |
| 215 |
surface = NULL; |
| 216 |
} |
| 217 |
|
| 218 |
GtkWidget * |
| 219 |
do_drawingarea (GtkWidget *do_widget) |
| 220 |
{ |
| 221 |
GtkWidget *frame; |
| 222 |
GtkWidget *vbox; |
| 223 |
GtkWidget *da; |
| 224 |
GtkWidget *label; |
| 225 |
|
| 226 |
if (!window) |
| 227 |
{ |
| 228 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 229 |
gtk_window_set_screen (GTK_WINDOW (window), |
| 230 |
gtk_widget_get_screen (do_widget)); |
| 231 |
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area"); |
| 232 |
|
| 233 |
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL); |
| 234 |
|
| 235 |
gtk_container_set_border_width (GTK_CONTAINER (window), 8); |
| 236 |
|
| 237 |
vbox = gtk_vbox_new (FALSE, 8); |
| 238 |
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8); |
| 239 |
gtk_container_add (GTK_CONTAINER (window), vbox); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
label = gtk_label_new (NULL); |
| 246 |
gtk_label_set_markup (GTK_LABEL (label), |
| 247 |
"<u>Checkerboard pattern</u>"); |
| 248 |
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); |
| 249 |
|
| 250 |
frame = gtk_frame_new (NULL); |
| 251 |
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); |
| 252 |
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0); |
| 253 |
|
| 254 |
da = gtk_drawing_area_new (); |
| 255 |
|
| 256 |
gtk_widget_set_size_request (da, 100, 100); |
| 257 |
|
| 258 |
gtk_container_add (GTK_CONTAINER (frame), da); |
| 259 |
|
| 260 |
g_signal_connect (da, "expose-event", |
| 261 |
G_CALLBACK (checkerboard_expose), NULL); |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
label = gtk_label_new (NULL); |
| 268 |
gtk_label_set_markup (GTK_LABEL (label), |
| 269 |
"<u>Scribble area</u>"); |
| 270 |
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); |
| 271 |
|
| 272 |
frame = gtk_frame_new (NULL); |
| 273 |
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); |
| 274 |
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0); |
| 275 |
|
| 276 |
da = gtk_drawing_area_new (); |
| 277 |
|
| 278 |
gtk_widget_set_size_request (da, 100, 100); |
| 279 |
|
| 280 |
gtk_container_add (GTK_CONTAINER (frame), da); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
g_signal_connect (da, "expose-event", |
| 285 |
G_CALLBACK (scribble_expose_event), NULL); |
| 286 |
g_signal_connect (da,"configure-event", |
| 287 |
G_CALLBACK (scribble_configure_event), NULL); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
g_signal_connect (da, "motion-notify-event", |
| 292 |
G_CALLBACK (scribble_motion_notify_event), NULL); |
| 293 |
g_signal_connect (da, "button-press-event", |
| 294 |
G_CALLBACK (scribble_button_press_event), NULL); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
gtk_widget_set_events (da, gtk_widget_get_events (da) |
| 301 |
| GDK_LEAVE_NOTIFY_MASK |
| 302 |
| GDK_BUTTON_PRESS_MASK |
| 303 |
| GDK_POINTER_MOTION_MASK |
| 304 |
| GDK_POINTER_MOTION_HINT_MASK); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
if (!gtk_widget_get_visible (window)) |
| 309 |
gtk_widget_show_all (window); |
| 310 |
else |
| 311 |
gtk_widget_destroy (window); |
| 312 |
|
| 313 |
return window; |
| 314 |
} |