2011-01-19 04:57:17 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* Surface to store current scribbles */
|
|
|
|
static cairo_surface_t *surface = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_surface (void)
|
|
|
|
{
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
|
|
|
cairo_paint (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new surface of the appropriate size to store our scribbles */
|
2018-01-01 14:15:59 +00:00
|
|
|
static void
|
|
|
|
size_allocate_cb (GtkWidget *widget,
|
|
|
|
GtkAllocation *alloc,
|
|
|
|
int baseline,
|
|
|
|
gpointer data)
|
2011-01-19 04:57:17 +00:00
|
|
|
{
|
|
|
|
if (surface)
|
2018-01-01 14:15:59 +00:00
|
|
|
{
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
surface = NULL;
|
|
|
|
}
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2019-05-20 00:38:08 +00:00
|
|
|
if (gtk_native_get_surface (gtk_widget_get_native (widget)))
|
2018-01-01 14:15:59 +00:00
|
|
|
{
|
2019-05-20 00:38:08 +00:00
|
|
|
surface = gdk_surface_create_similar_surface (gtk_native_get_surface (gtk_widget_get_native (widget)),
|
2018-01-01 14:15:59 +00:00
|
|
|
CAIRO_CONTENT_COLOR,
|
|
|
|
gtk_widget_get_width (widget),
|
|
|
|
gtk_widget_get_height (widget));
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-01-01 14:15:59 +00:00
|
|
|
/* Initialize the surface to white */
|
|
|
|
clear_surface ();
|
|
|
|
}
|
2011-01-19 04:57:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 08:59:19 +00:00
|
|
|
/* Redraw the screen from the surface. Note that the draw
|
|
|
|
* callback receives a ready-to-be-used cairo_t that is already
|
2011-01-19 04:57:17 +00:00
|
|
|
* clipped to only draw the exposed areas of the widget
|
|
|
|
*/
|
2018-01-01 14:15:59 +00:00
|
|
|
static void
|
|
|
|
draw_cb (GtkDrawingArea *drawing_area,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2011-01-19 04:57:17 +00:00
|
|
|
{
|
|
|
|
cairo_set_source_surface (cr, surface, 0, 0);
|
|
|
|
cairo_paint (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw a rectangle on the surface at the given position */
|
|
|
|
static void
|
|
|
|
draw_brush (GtkWidget *widget,
|
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
|
|
|
{
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
/* Paint to the surface, where we store our state */
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
cairo_rectangle (cr, x - 3, y - 3, 6, 6);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2018-03-28 15:09:49 +00:00
|
|
|
/* Now invalidate the drawing area. */
|
|
|
|
gtk_widget_queue_draw (widget);
|
2011-01-19 04:57:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
static double start_x;
|
|
|
|
static double start_y;
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
static void
|
|
|
|
drag_begin (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
|
|
|
{
|
|
|
|
start_x = x;
|
|
|
|
start_y = y;
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
draw_brush (area, x, y);
|
2011-01-19 04:57:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
static void
|
|
|
|
drag_update (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
2011-01-19 04:57:17 +00:00
|
|
|
{
|
2018-01-01 15:00:37 +00:00
|
|
|
draw_brush (area, start_x + x, start_y + y);
|
|
|
|
}
|
2017-08-28 23:05:31 +00:00
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
static void
|
|
|
|
drag_end (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
|
|
|
{
|
|
|
|
draw_brush (area, start_x + x, start_y + y);
|
|
|
|
}
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-01-01 15:00:37 +00:00
|
|
|
static void
|
|
|
|
pressed (GtkGestureMultiPress *gesture,
|
|
|
|
int n_press,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkWidget *area)
|
|
|
|
{
|
|
|
|
clear_surface ();
|
|
|
|
gtk_widget_queue_draw (area);
|
2011-01-19 04:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_window (void)
|
|
|
|
{
|
|
|
|
if (surface)
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
static void
|
|
|
|
activate (GtkApplication *app,
|
|
|
|
gpointer user_data)
|
2011-01-19 04:57:17 +00:00
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *frame;
|
2015-02-19 14:53:35 +00:00
|
|
|
GtkWidget *drawing_area;
|
2018-01-01 15:00:37 +00:00
|
|
|
GtkGesture *drag;
|
|
|
|
GtkGesture *press;
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
window = gtk_application_window_new (app);
|
2011-01-19 04:57:17 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
|
|
|
|
|
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
|
|
|
|
|
|
|
|
frame = gtk_frame_new (NULL);
|
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), frame);
|
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
drawing_area = gtk_drawing_area_new ();
|
2011-01-19 04:57:17 +00:00
|
|
|
/* set a minimum size */
|
2015-02-19 14:53:35 +00:00
|
|
|
gtk_widget_set_size_request (drawing_area, 100, 100);
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (frame), drawing_area);
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-01-01 14:15:59 +00:00
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area), draw_cb, NULL, NULL);
|
|
|
|
|
|
|
|
g_signal_connect_after (drawing_area, "size-allocate",
|
|
|
|
G_CALLBACK (size_allocate_cb), NULL);
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2018-03-10 17:05:57 +00:00
|
|
|
drag = gtk_gesture_drag_new ();
|
2018-01-01 15:00:37 +00:00
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (drag), GDK_BUTTON_PRIMARY);
|
2018-03-10 17:05:57 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (drag));
|
2018-01-01 15:00:37 +00:00
|
|
|
g_signal_connect (drag, "drag-begin", G_CALLBACK (drag_begin), drawing_area);
|
|
|
|
g_signal_connect (drag, "drag-update", G_CALLBACK (drag_update), drawing_area);
|
2018-01-03 08:59:19 +00:00
|
|
|
g_signal_connect (drag, "drag-end", G_CALLBACK (drag_end), drawing_area);
|
2018-01-01 15:00:37 +00:00
|
|
|
|
2018-03-09 05:14:59 +00:00
|
|
|
press = gtk_gesture_multi_press_new ();
|
2018-01-01 15:00:37 +00:00
|
|
|
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (press), GDK_BUTTON_SECONDARY);
|
2018-03-09 05:14:59 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (press));
|
2018-01-01 15:00:37 +00:00
|
|
|
|
|
|
|
g_signal_connect (press, "pressed", G_CALLBACK (pressed), drawing_area);
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2015-02-19 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
GtkApplication *app;
|
|
|
|
int status;
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
|
|
|
|
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
|
|
|
status = g_application_run (G_APPLICATION (app), argc, argv);
|
|
|
|
g_object_unref (app);
|
2011-01-19 04:57:17 +00:00
|
|
|
|
2015-02-19 14:53:35 +00:00
|
|
|
return status;
|
2011-01-19 04:57:17 +00:00
|
|
|
}
|