2001-04-18 18:09:18 +00:00
|
|
|
/* Drawing Area
|
|
|
|
*
|
|
|
|
* GtkDrawingArea is a blank area where you can draw custom displays
|
|
|
|
* of various kinds.
|
|
|
|
*
|
|
|
|
* This demo has two drawing areas. The checkerboard area shows
|
|
|
|
* how you can just draw something; all you have to do is write
|
|
|
|
* a signal handler for expose_event, as shown here.
|
|
|
|
*
|
|
|
|
* The "scribble" area is a bit more advanced, and shows how to handle
|
|
|
|
* events such as button presses and mouse motion. Click the mouse
|
|
|
|
* and drag in the scribble area to draw squiggles. Resize the window
|
|
|
|
* to clear the area.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
/* Pixmap for scribble area, to store current scribbles */
|
2010-08-12 21:38:17 +00:00
|
|
|
static cairo_surface_t *surface = NULL;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Create a new surface of the appropriate size to store our scribbles */
|
2001-04-18 18:09:18 +00:00
|
|
|
static gboolean
|
2008-07-06 07:24:02 +00:00
|
|
|
scribble_configure_event (GtkWidget *widget,
|
|
|
|
GdkEventConfigure *event,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_t *cr;
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
if (surface)
|
|
|
|
cairo_surface_destroy (surface);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
surface = gdk_window_create_similar_surface (widget->window,
|
|
|
|
CAIRO_CONTENT_COLOR,
|
|
|
|
widget->allocation.width,
|
|
|
|
widget->allocation.height);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Initialize the surface to white */
|
|
|
|
cr = cairo_create (surface);
|
2010-07-16 22:18:35 +00:00
|
|
|
|
|
|
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
|
|
|
cairo_paint (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
/* We've handled the configure event, no need for further processing. */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Redraw the screen from the surface */
|
2001-04-18 18:09:18 +00:00
|
|
|
static gboolean
|
|
|
|
scribble_expose_event (GtkWidget *widget,
|
2008-07-06 07:24:02 +00:00
|
|
|
GdkEventExpose *event,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2010-07-19 10:15:58 +00:00
|
|
|
cairo_t *cr;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2010-07-19 10:15:58 +00:00
|
|
|
cr = gdk_cairo_create (widget->window);
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
cairo_set_source_surface (cr, surface, 0, 0);
|
2010-07-19 10:15:58 +00:00
|
|
|
gdk_cairo_rectangle (cr, &event->area);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw a rectangle on the screen */
|
|
|
|
static void
|
|
|
|
draw_brush (GtkWidget *widget,
|
2008-07-06 07:24:02 +00:00
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
GdkRectangle update_rect;
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_t *cr;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
update_rect.x = x - 3;
|
|
|
|
update_rect.y = y - 3;
|
|
|
|
update_rect.width = 6;
|
|
|
|
update_rect.height = 6;
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Paint to the surface, where we store our state */
|
|
|
|
cr = cairo_create (surface);
|
2010-07-16 22:18:35 +00:00
|
|
|
|
|
|
|
gdk_cairo_rectangle (cr, &update_rect);
|
|
|
|
cairo_fill (cr);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
/* Now invalidate the affected region of the drawing area. */
|
|
|
|
gdk_window_invalidate_rect (widget->window,
|
2008-07-06 07:24:02 +00:00
|
|
|
&update_rect,
|
|
|
|
FALSE);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2008-07-06 07:24:02 +00:00
|
|
|
scribble_button_press_event (GtkWidget *widget,
|
|
|
|
GdkEventButton *event,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2010-08-12 21:38:17 +00:00
|
|
|
if (surface == NULL)
|
2001-04-18 18:09:18 +00:00
|
|
|
return FALSE; /* paranoia check, in case we haven't gotten a configure event */
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
if (event->button == 1)
|
|
|
|
draw_brush (widget, event->x, event->y);
|
|
|
|
|
|
|
|
/* We've handled the event, stop processing */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2008-07-06 07:24:02 +00:00
|
|
|
scribble_motion_notify_event (GtkWidget *widget,
|
|
|
|
GdkEventMotion *event,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
GdkModifierType state;
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
if (surface == NULL)
|
2001-04-18 18:09:18 +00:00
|
|
|
return FALSE; /* paranoia check, in case we haven't gotten a configure event */
|
|
|
|
|
|
|
|
/* This call is very important; it requests the next motion event.
|
|
|
|
* If you don't call gdk_window_get_pointer() you'll only get
|
|
|
|
* a single motion event. The reason is that we specified
|
|
|
|
* GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
|
|
|
|
* If we hadn't specified that, we could just use event->x, event->y
|
|
|
|
* as the pointer location. But we'd also get deluged in events.
|
|
|
|
* By requesting the next event as we handle the current one,
|
|
|
|
* we avoid getting a huge number of events faster than we
|
|
|
|
* can cope.
|
|
|
|
*/
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
gdk_window_get_pointer (event->window, &x, &y, &state);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
if (state & GDK_BUTTON1_MASK)
|
|
|
|
draw_brush (widget, x, y);
|
|
|
|
|
|
|
|
/* We've handled it, stop processing */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2008-07-06 07:24:02 +00:00
|
|
|
checkerboard_expose (GtkWidget *da,
|
|
|
|
GdkEventExpose *event,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
gint i, j, xcount, ycount;
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_t *cr;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
#define CHECK_SIZE 10
|
|
|
|
#define SPACING 2
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
/* At the start of an expose handler, a clip region of event->area
|
|
|
|
* is set on the window, and event->area has been cleared to the
|
|
|
|
* widget's background color. The docs for
|
|
|
|
* gdk_window_begin_paint_region() give more details on how this
|
|
|
|
* works.
|
|
|
|
*/
|
|
|
|
|
2010-07-16 22:18:35 +00:00
|
|
|
cr = gdk_cairo_create (da->window);
|
|
|
|
gdk_cairo_rectangle (cr, &event->area);
|
|
|
|
cairo_clip (cr);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
xcount = 0;
|
|
|
|
i = SPACING;
|
|
|
|
while (i < da->allocation.width)
|
|
|
|
{
|
|
|
|
j = SPACING;
|
|
|
|
ycount = xcount % 2; /* start with even/odd depending on row */
|
|
|
|
while (j < da->allocation.height)
|
2008-07-06 07:24:02 +00:00
|
|
|
{
|
|
|
|
if (ycount % 2)
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
|
2008-07-06 07:24:02 +00:00
|
|
|
else
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
2008-07-06 07:24:02 +00:00
|
|
|
|
|
|
|
/* If we're outside event->area, this will do nothing.
|
|
|
|
* It might be mildly more efficient if we handled
|
|
|
|
* the clipping ourselves, but again we're feeling lazy.
|
|
|
|
*/
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
|
|
|
|
cairo_fill (cr);
|
2008-07-06 07:24:02 +00:00
|
|
|
|
|
|
|
j += CHECK_SIZE + SPACING;
|
|
|
|
++ycount;
|
|
|
|
}
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
i += CHECK_SIZE + SPACING;
|
|
|
|
++xcount;
|
|
|
|
}
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2010-07-16 22:18:35 +00:00
|
|
|
cairo_destroy (cr);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
/* return TRUE because we've handled this event, so no
|
|
|
|
* further processing is required.
|
|
|
|
*/
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-12-23 02:39:45 +00:00
|
|
|
static void
|
|
|
|
close_window (void)
|
|
|
|
{
|
|
|
|
window = NULL;
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
if (surface)
|
|
|
|
g_object_unref (surface);
|
|
|
|
surface = NULL;
|
2006-12-23 02:39:45 +00:00
|
|
|
}
|
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
GtkWidget *
|
2003-11-08 22:08:05 +00:00
|
|
|
do_drawingarea (GtkWidget *do_widget)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
GtkWidget *frame;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *da;
|
|
|
|
GtkWidget *label;
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2003-11-08 22:08:05 +00:00
|
|
|
gtk_window_set_screen (GTK_WINDOW (window),
|
2008-07-06 07:24:02 +00:00
|
|
|
gtk_widget_get_screen (do_widget));
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
|
|
|
|
|
2006-12-23 02:39:45 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
|
|
|
|
|
|
|
|
vbox = gtk_vbox_new (FALSE, 8);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the checkerboard area
|
|
|
|
*/
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label),
|
2008-07-06 07:24:02 +00:00
|
|
|
"<u>Checkerboard pattern</u>");
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
frame = gtk_frame_new (NULL);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
|
|
|
/* set a minimum size */
|
2001-08-17 14:11:36 +00:00
|
|
|
gtk_widget_set_size_request (da, 100, 100);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), da);
|
|
|
|
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (da, "expose-event",
|
2008-07-06 07:24:02 +00:00
|
|
|
G_CALLBACK (checkerboard_expose), NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the scribble area
|
|
|
|
*/
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
label = gtk_label_new (NULL);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (label),
|
2008-07-06 07:24:02 +00:00
|
|
|
"<u>Scribble area</u>");
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
frame = gtk_frame_new (NULL);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
|
|
|
/* set a minimum size */
|
2001-08-17 14:11:36 +00:00
|
|
|
gtk_widget_set_size_request (da, 100, 100);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
gtk_container_add (GTK_CONTAINER (frame), da);
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Signals used to handle backing surface */
|
2008-08-11 19:12:58 +00:00
|
|
|
|
|
|
|
g_signal_connect (da, "expose-event",
|
2008-07-06 07:24:02 +00:00
|
|
|
G_CALLBACK (scribble_expose_event), NULL);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (da,"configure-event",
|
2008-07-06 07:24:02 +00:00
|
|
|
G_CALLBACK (scribble_configure_event), NULL);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
/* Event signals */
|
2008-08-11 19:12:58 +00:00
|
|
|
|
|
|
|
g_signal_connect (da, "motion-notify-event",
|
2008-07-06 07:24:02 +00:00
|
|
|
G_CALLBACK (scribble_motion_notify_event), NULL);
|
2008-08-11 19:12:58 +00:00
|
|
|
g_signal_connect (da, "button-press-event",
|
2008-07-06 07:24:02 +00:00
|
|
|
G_CALLBACK (scribble_button_press_event), NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Ask to receive events the drawing area doesn't normally
|
|
|
|
* subscribe to
|
|
|
|
*/
|
|
|
|
gtk_widget_set_events (da, gtk_widget_get_events (da)
|
2008-07-06 07:24:02 +00:00
|
|
|
| GDK_LEAVE_NOTIFY_MASK
|
|
|
|
| GDK_BUTTON_PRESS_MASK
|
|
|
|
| GDK_POINTER_MOTION_MASK
|
|
|
|
| GDK_POINTER_MOTION_HINT_MASK);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_widget_show_all (window);
|
|
|
|
else
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|