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
|
2018-02-05 10:52:32 +00:00
|
|
|
* how you can just draw something; all you have to do is set a function
|
|
|
|
* via gtk_drawing_area_set_draw_func(), as shown here.
|
2001-04-18 18:09:18 +00:00
|
|
|
*
|
|
|
|
* 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 */
|
2017-10-07 00:41:30 +00:00
|
|
|
static void
|
|
|
|
create_surface (GtkWidget *widget)
|
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
|
|
|
|
2017-10-07 00:41:30 +00:00
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2017-11-05 14:01:30 +00:00
|
|
|
gtk_widget_get_width (widget),
|
|
|
|
gtk_widget_get_height (widget));
|
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);
|
2017-10-07 00:41:30 +00:00
|
|
|
}
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2017-10-07 00:41:30 +00:00
|
|
|
static void
|
|
|
|
scribble_size_allocate (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
create_surface (widget);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-12 21:38:17 +00:00
|
|
|
/* Redraw the screen from the surface */
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
scribble_draw (GtkDrawingArea *da,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2010-08-12 21:38:17 +00:00
|
|
|
cairo_set_source_surface (cr, surface, 0, 0);
|
2010-09-08 19:50:16 +00:00
|
|
|
cairo_paint (cr);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2017-10-07 00:41:30 +00:00
|
|
|
if (surface == NULL ||
|
2017-11-05 14:01:30 +00:00
|
|
|
cairo_image_surface_get_width (surface) != gtk_widget_get_width (widget) ||
|
|
|
|
cairo_image_surface_get_height (surface) != gtk_widget_get_height (widget))
|
2017-10-07 00:41:30 +00:00
|
|
|
create_surface (widget);
|
|
|
|
|
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);
|
|
|
|
|
2018-03-28 15:09:49 +00:00
|
|
|
gtk_widget_queue_draw (widget);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
static double start_x;
|
|
|
|
static double start_y;
|
2017-08-28 22:47:03 +00:00
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
static void
|
|
|
|
drag_begin (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
|
|
|
{
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
draw_brush (area, x, y);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
static void
|
|
|
|
drag_update (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2018-01-01 15:09:31 +00:00
|
|
|
draw_brush (area, start_x + x, start_y + y);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
static void
|
|
|
|
drag_end (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
|
|
|
{
|
|
|
|
draw_brush (area, start_x + x, start_y + y);
|
|
|
|
}
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
checkerboard_draw (GtkDrawingArea *da,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
2016-11-18 05:16:38 +00:00
|
|
|
gint i, j, xcount, ycount;
|
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
|
|
|
|
2010-09-08 19:50:16 +00:00
|
|
|
/* At the start of a draw handler, a clip region has been set on
|
|
|
|
* the Cairo context, and the contents have been cleared to the
|
2001-04-18 18:09:18 +00:00
|
|
|
* widget's background color. The docs for
|
2018-03-20 10:40:08 +00:00
|
|
|
* gdk_surface_begin_paint_region() give more details on how this
|
2001-04-18 18:09:18 +00:00
|
|
|
* works.
|
|
|
|
*/
|
|
|
|
|
|
|
|
xcount = 0;
|
|
|
|
i = SPACING;
|
2010-09-08 19:50:16 +00:00
|
|
|
while (i < width)
|
2001-04-18 18:09:18 +00:00
|
|
|
{
|
|
|
|
j = SPACING;
|
|
|
|
ycount = xcount % 2; /* start with even/odd depending on row */
|
2010-09-08 19:50:16 +00:00
|
|
|
while (j < 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
|
|
|
|
2010-09-08 19:50:16 +00:00
|
|
|
/* If we're outside the clip, this will do nothing.
|
2008-07-06 07:24:02 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2010-10-16 18:16:19 +00:00
|
|
|
cairo_surface_destroy (surface);
|
2010-08-12 21:38:17 +00:00
|
|
|
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;
|
2018-01-01 15:09:31 +00:00
|
|
|
GtkGesture *drag;
|
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);
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
|
|
|
|
|
2015-06-28 13:23:28 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK (close_window), NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
|
2016-10-26 19:11:18 +00:00
|
|
|
g_object_set (vbox, "margin", 16, NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
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>");
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label);
|
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);
|
2017-10-07 00:41:30 +00:00
|
|
|
gtk_widget_set_vexpand (frame, TRUE);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 100);
|
|
|
|
gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), 100);
|
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), checkerboard_draw, NULL, NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (frame), da);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>");
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
frame = gtk_frame_new (NULL);
|
2017-10-07 00:41:30 +00:00
|
|
|
gtk_widget_set_vexpand (frame, TRUE);
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
2017-04-21 20:34:36 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), frame);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2001-04-18 18:09:18 +00:00
|
|
|
da = gtk_drawing_area_new ();
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 100);
|
|
|
|
gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), 100);
|
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), scribble_draw, NULL, NULL);
|
2001-04-18 18:09:18 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (frame), da);
|
|
|
|
|
2017-10-07 00:41:30 +00:00
|
|
|
g_signal_connect (da, "size-allocate",
|
|
|
|
G_CALLBACK (scribble_size_allocate), NULL);
|
2008-08-11 19:12:58 +00:00
|
|
|
|
2018-01-01 15:09:31 +00:00
|
|
|
drag = gtk_gesture_drag_new (da);
|
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY);
|
|
|
|
g_object_set_data_full (G_OBJECT (da), "drag", drag, g_object_unref);
|
|
|
|
|
|
|
|
g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), da);
|
|
|
|
g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), da);
|
2018-01-03 08:59:19 +00:00
|
|
|
g_signal_connect (drag, "drag-end", G_CALLBACK (drag_end), da);
|
2001-04-18 18:09:18 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2001-04-18 18:09:18 +00:00
|
|
|
else
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|