2013-01-17 20:12:39 +00:00
|
|
|
/* Gestures
|
|
|
|
*
|
|
|
|
* Perform gestures on touchscreens and other input devices. This
|
|
|
|
* demo reacts to long presses and swipes from all devices, plus
|
|
|
|
* multi-touch rotate and zoom gestures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkGesture *rotate = NULL;
|
|
|
|
static GtkGesture *zoom = NULL;
|
2020-07-24 20:32:16 +00:00
|
|
|
static double swipe_x = 0;
|
|
|
|
static double swipe_y = 0;
|
2013-01-17 20:12:39 +00:00
|
|
|
static gboolean long_pressed = FALSE;
|
|
|
|
|
2017-03-02 17:18:31 +00:00
|
|
|
static gboolean
|
|
|
|
touchpad_swipe_gesture_begin (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
/* Disallow touchscreen events here */
|
|
|
|
if (sequence != NULL)
|
|
|
|
gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_DENIED);
|
|
|
|
return sequence == NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-17 20:12:39 +00:00
|
|
|
static void
|
|
|
|
swipe_gesture_swept (GtkGestureSwipe *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double velocity_x,
|
|
|
|
double velocity_y,
|
2013-01-17 20:12:39 +00:00
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
swipe_x = velocity_x / 10;
|
|
|
|
swipe_y = velocity_y / 10;
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
long_press_gesture_pressed (GtkGestureLongPress *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2013-01-17 20:12:39 +00:00
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
long_pressed = TRUE;
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
long_press_gesture_end (GtkGesture *gesture,
|
|
|
|
GdkEventSequence *sequence,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
long_pressed = FALSE;
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rotation_angle_changed (GtkGestureRotate *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double angle,
|
|
|
|
double delta,
|
2013-01-17 20:12:39 +00:00
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
zoom_scale_changed (GtkGestureZoom *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double scale,
|
2013-01-17 20:12:39 +00:00
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
gtk_widget_queue_draw (widget);
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
drawing_area_draw (GtkDrawingArea *area,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2013-01-17 20:12:39 +00:00
|
|
|
{
|
|
|
|
if (swipe_x != 0 || swipe_y != 0)
|
|
|
|
{
|
|
|
|
cairo_save (cr);
|
|
|
|
cairo_set_line_width (cr, 6);
|
2016-11-18 05:16:38 +00:00
|
|
|
cairo_move_to (cr, width / 2, height / 2);
|
2013-01-17 20:12:39 +00:00
|
|
|
cairo_rel_line_to (cr, swipe_x, swipe_y);
|
|
|
|
cairo_set_source_rgba (cr, 1, 0, 0, 0.5);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gtk_gesture_is_recognized (rotate) || gtk_gesture_is_recognized (zoom))
|
|
|
|
{
|
|
|
|
cairo_pattern_t *pat;
|
|
|
|
cairo_matrix_t matrix;
|
2020-07-24 20:32:16 +00:00
|
|
|
double angle, scale;
|
|
|
|
double x_center, y_center;
|
2019-12-14 09:27:44 +00:00
|
|
|
|
|
|
|
gtk_gesture_get_bounding_box_center (GTK_GESTURE (zoom), &x_center, &y_center);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
2015-09-15 11:19:31 +00:00
|
|
|
cairo_get_matrix (cr, &matrix);
|
2019-12-14 09:27:44 +00:00
|
|
|
cairo_matrix_translate (&matrix, x_center, y_center);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
cairo_save (cr);
|
|
|
|
|
2014-05-26 12:29:17 +00:00
|
|
|
angle = gtk_gesture_rotate_get_angle_delta (GTK_GESTURE_ROTATE (rotate));
|
|
|
|
cairo_matrix_rotate (&matrix, angle);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
2014-05-26 12:32:59 +00:00
|
|
|
scale = gtk_gesture_zoom_get_scale_delta (GTK_GESTURE_ZOOM (zoom));
|
|
|
|
cairo_matrix_scale (&matrix, scale, scale);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
cairo_set_matrix (cr, &matrix);
|
|
|
|
cairo_rectangle (cr, -100, -100, 200, 200);
|
|
|
|
|
|
|
|
pat = cairo_pattern_create_linear (-100, 0, 200, 0);
|
|
|
|
cairo_pattern_add_color_stop_rgb (pat, 0, 0, 0, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgb (pat, 1, 1, 0, 0);
|
|
|
|
cairo_set_source (cr, pat);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
|
|
|
|
|
|
|
cairo_pattern_destroy (pat);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (long_pressed)
|
|
|
|
{
|
|
|
|
cairo_save (cr);
|
2016-11-18 05:16:38 +00:00
|
|
|
cairo_arc (cr,
|
|
|
|
width / 2, height / 2,
|
2013-01-17 20:12:39 +00:00
|
|
|
50, 0, 2 * G_PI);
|
|
|
|
|
|
|
|
cairo_set_source_rgba (cr, 0, 1, 0, 0.5);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_gestures (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
2013-01-17 20:12:39 +00:00
|
|
|
GtkWidget *drawing_area;
|
|
|
|
GtkGesture *gesture;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2013-01-17 20:12:39 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Gestures");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
drawing_area = gtk_drawing_area_new ();
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), drawing_area);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area),
|
|
|
|
drawing_area_draw,
|
|
|
|
NULL, NULL);
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
/* Swipe */
|
2018-03-08 22:49:41 +00:00
|
|
|
gesture = gtk_gesture_swipe_new ();
|
2013-01-17 20:12:39 +00:00
|
|
|
g_signal_connect (gesture, "swipe",
|
|
|
|
G_CALLBACK (swipe_gesture_swept), drawing_area);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_BUBBLE);
|
2018-03-08 22:49:41 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (gesture));
|
2013-01-17 20:12:39 +00:00
|
|
|
|
2017-03-02 17:18:31 +00:00
|
|
|
/* 3fg swipe for touchpads */
|
|
|
|
gesture = g_object_new (GTK_TYPE_GESTURE_SWIPE,
|
|
|
|
"n-points", 3,
|
|
|
|
NULL);
|
|
|
|
g_signal_connect (gesture, "begin",
|
|
|
|
G_CALLBACK (touchpad_swipe_gesture_begin), drawing_area);
|
|
|
|
g_signal_connect (gesture, "swipe",
|
|
|
|
G_CALLBACK (swipe_gesture_swept), drawing_area);
|
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_BUBBLE);
|
2018-05-10 18:00:17 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
2017-03-02 17:18:31 +00:00
|
|
|
|
2013-01-17 20:12:39 +00:00
|
|
|
/* Long press */
|
2018-03-09 05:36:22 +00:00
|
|
|
gesture = gtk_gesture_long_press_new ();
|
2013-01-17 20:12:39 +00:00
|
|
|
g_signal_connect (gesture, "pressed",
|
|
|
|
G_CALLBACK (long_press_gesture_pressed), drawing_area);
|
|
|
|
g_signal_connect (gesture, "end",
|
|
|
|
G_CALLBACK (long_press_gesture_end), drawing_area);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_BUBBLE);
|
2018-03-09 05:36:22 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (gesture));
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
/* Rotate */
|
2018-03-08 22:36:41 +00:00
|
|
|
rotate = gesture = gtk_gesture_rotate_new ();
|
2013-01-17 20:12:39 +00:00
|
|
|
g_signal_connect (gesture, "angle-changed",
|
|
|
|
G_CALLBACK (rotation_angle_changed), drawing_area);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_BUBBLE);
|
2018-03-08 22:36:41 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (gesture));
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
/* Zoom */
|
2018-03-08 22:40:45 +00:00
|
|
|
zoom = gesture = gtk_gesture_zoom_new ();
|
2013-01-17 20:12:39 +00:00
|
|
|
g_signal_connect (gesture, "scale-changed",
|
|
|
|
G_CALLBACK (zoom_scale_changed), drawing_area);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_BUBBLE);
|
2018-03-08 22:40:45 +00:00
|
|
|
gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (gesture));
|
2013-01-17 20:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2013-01-17 20:12:39 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2013-01-17 20:12:39 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|