gtk-demo: Update drawing area usage to new APIs

A few demos are broken because they do input on drawing areas.
This commit is contained in:
Benjamin Otte 2016-11-18 06:16:38 +01:00
parent 5940625e9e
commit 979b56e86f
9 changed files with 102 additions and 127 deletions

View File

@ -15,15 +15,15 @@ static GtkWidget *frame;
/* draw callback for the drawing area /* draw callback for the drawing area
*/ */
static gboolean static void
draw_callback (GtkWidget *widget, draw_function (GtkDrawingArea *da,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
gdk_cairo_set_source_rgba (cr, &color); gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr); cairo_paint (cr);
return TRUE;
} }
static void static void
@ -87,11 +87,9 @@ do_colorsel (GtkWidget *do_widget)
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE); gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 200);
g_signal_connect (da, "draw", G_CALLBACK (draw_callback), NULL); gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), 200);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_function, NULL, NULL);
/* set a minimum size */
gtk_widget_set_size_request (da, 200, 200);
gtk_container_add (GTK_CONTAINER (frame), da); gtk_container_add (GTK_CONTAINER (frame), da);

View File

@ -51,22 +51,17 @@ css_text_changed (GtkTextBuffer *buffer,
gtk_style_context_reset_widgets (gdk_screen_get_default ()); gtk_style_context_reset_widgets (gdk_screen_get_default ());
} }
static gboolean static void
drawing_area_draw (GtkWidget *widget, drawing_area_draw (GtkDrawingArea *da,
cairo_t *cr) cairo_t *cr,
int width,
int height,
gpointer data)
{ {
GtkStyleContext *context = gtk_widget_get_style_context (widget); GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (da));
gtk_render_background (context, cr, gtk_render_background (context, cr, 0, 0, width, height);
0, 0, gtk_render_frame (context, cr, 0, 0, width, height);
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
gtk_render_frame (context, cr,
0, 0,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
return FALSE;
} }
static void static void
@ -105,8 +100,9 @@ do_css_multiplebgs (GtkWidget *do_widget)
child = gtk_drawing_area_new (); child = gtk_drawing_area_new ();
gtk_widget_set_name (child, "canvas"); gtk_widget_set_name (child, "canvas");
g_signal_connect (child, "draw", gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (child),
G_CALLBACK (drawing_area_draw), NULL); drawing_area_draw,
NULL, NULL);
gtk_container_add (GTK_CONTAINER (container), child); gtk_container_add (GTK_CONTAINER (container), child);
child = gtk_button_new (); child = gtk_button_new ();

View File

@ -50,15 +50,15 @@ scribble_configure_event (GtkWidget *widget,
} }
/* Redraw the screen from the surface */ /* Redraw the screen from the surface */
static gboolean static void
scribble_draw (GtkWidget *widget, scribble_draw (GtkDrawingArea *da,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
cairo_set_source_surface (cr, surface, 0, 0); cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr); cairo_paint (cr);
return FALSE;
} }
/* Draw a rectangle on the screen */ /* Draw a rectangle on the screen */
@ -136,12 +136,14 @@ scribble_motion_notify_event (GtkWidget *widget,
} }
static gboolean static void
checkerboard_draw (GtkWidget *da, checkerboard_draw (GtkDrawingArea *da,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
gint i, j, xcount, ycount, width, height; gint i, j, xcount, ycount;
#define CHECK_SIZE 10 #define CHECK_SIZE 10
#define SPACING 2 #define SPACING 2
@ -154,8 +156,6 @@ checkerboard_draw (GtkWidget *da,
*/ */
xcount = 0; xcount = 0;
width = gtk_widget_get_allocated_width (da);
height = gtk_widget_get_allocated_height (da);
i = SPACING; i = SPACING;
while (i < width) while (i < width)
{ {
@ -180,11 +180,6 @@ checkerboard_draw (GtkWidget *da,
i += CHECK_SIZE + SPACING; i += CHECK_SIZE + SPACING;
++xcount; ++xcount;
} }
/* return TRUE because we've handled this event, so no
* further processing is required.
*/
return TRUE;
} }
static void static void
@ -233,14 +228,11 @@ do_drawingarea (GtkWidget *do_widget)
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE); gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
/* set a minimum size */ gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 100);
gtk_widget_set_size_request (da, 100, 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);
gtk_container_add (GTK_CONTAINER (frame), da); gtk_container_add (GTK_CONTAINER (frame), da);
g_signal_connect (da, "draw",
G_CALLBACK (checkerboard_draw), NULL);
/* /*
* Create the scribble area * Create the scribble area
*/ */
@ -255,15 +247,13 @@ do_drawingarea (GtkWidget *do_widget)
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE); gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
/* set a minimum size */ gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 100);
gtk_widget_set_size_request (da, 100, 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);
gtk_container_add (GTK_CONTAINER (frame), da); gtk_container_add (GTK_CONTAINER (frame), da);
/* Signals used to handle backing surface */ /* Signals used to handle backing surface */
g_signal_connect (da, "draw",
G_CALLBACK (scribble_draw), NULL);
g_signal_connect (da,"configure-event", g_signal_connect (da,"configure-event",
G_CALLBACK (scribble_configure_event), NULL); G_CALLBACK (scribble_configure_event), NULL);

View File

@ -10,11 +10,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static gboolean static void
draw_color (GtkWidget *drawingarea, draw_color (GtkDrawingArea *drawingarea,
cairo_t *cr, cairo_t *cr,
const char *color_name) int width,
int height,
gpointer data)
{ {
const char *color_name = data;
GdkRGBA rgba; GdkRGBA rgba;
if (gdk_rgba_parse (&rgba, color_name)) if (gdk_rgba_parse (&rgba, color_name))
@ -22,8 +25,6 @@ draw_color (GtkWidget *drawingarea,
gdk_cairo_set_source_rgba (cr, &rgba); gdk_cairo_set_source_rgba (cr, &rgba);
cairo_paint (cr); cairo_paint (cr);
} }
return FALSE;
} }
static GtkWidget * static GtkWidget *
@ -33,8 +34,9 @@ color_swatch_new (const gchar *color)
button = gtk_button_new (); button = gtk_button_new ();
area = gtk_drawing_area_new (); area = gtk_drawing_area_new ();
g_signal_connect (area, "draw", G_CALLBACK (draw_color), (gpointer) color); gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 24);
gtk_widget_set_size_request (area, 24, 24); gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 24);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area), draw_color, (gpointer) color, NULL);
gtk_container_add (GTK_CONTAINER (button), area); gtk_container_add (GTK_CONTAINER (button), area);
gtk_widget_show_all (button); gtk_widget_show_all (button);

View File

@ -881,16 +881,18 @@ draw_spinbutton (GtkWidget *widget,
g_object_unref (spin_context); g_object_unref (spin_context);
} }
static gboolean static void
draw_cb (GtkWidget *widget, draw_func (GtkDrawingArea *da,
cairo_t *cr) cairo_t *cr,
int width,
int height,
gpointer data)
{ {
gint panewidth, width, height; GtkWidget *widget = GTK_WIDGET (da);
gint panewidth;
gint x, y; gint x, y;
width = gtk_widget_get_allocated_width (widget);
panewidth = width / 2; panewidth = width / 2;
height = gtk_widget_get_allocated_height (widget);
cairo_rectangle (cr, 0, 0, width, height); cairo_rectangle (cr, 0, 0, width, height);
cairo_set_source_rgb (cr, 0.9, 0.9, 0.9); cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
@ -944,8 +946,6 @@ draw_cb (GtkWidget *widget,
y += height + 10; y += height + 10;
draw_combobox (widget, cr, 10 + panewidth, y, panewidth - 20, TRUE, &height); draw_combobox (widget, cr, 10 + panewidth, y, panewidth - 20, TRUE, &height);
return FALSE;
} }
GtkWidget * GtkWidget *
@ -968,12 +968,12 @@ do_foreigndrawing (GtkWidget *do_widget)
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10); box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_add (GTK_CONTAINER (window), box); gtk_container_add (GTK_CONTAINER (window), box);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
gtk_widget_set_size_request (da, 400, 400); gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 400);
gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), 400);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func, NULL, NULL);
gtk_widget_set_hexpand (da, TRUE); gtk_widget_set_hexpand (da, TRUE);
gtk_widget_set_vexpand (da, TRUE); gtk_widget_set_vexpand (da, TRUE);
gtk_container_add (GTK_CONTAINER (box), da); gtk_container_add (GTK_CONTAINER (box), da);
g_signal_connect (da, "draw", G_CALLBACK (draw_cb), NULL);
} }
if (!gtk_widget_get_visible (window)) if (!gtk_widget_get_visible (window))

View File

@ -60,20 +60,18 @@ zoom_scale_changed (GtkGestureZoom *gesture,
gtk_widget_queue_draw (widget); gtk_widget_queue_draw (widget);
} }
static gboolean static void
drawing_area_draw (GtkWidget *widget, drawing_area_draw (GtkDrawingArea *area,
cairo_t *cr) cairo_t *cr,
int width,
int height,
gpointer data)
{ {
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
if (swipe_x != 0 || swipe_y != 0) if (swipe_x != 0 || swipe_y != 0)
{ {
cairo_save (cr); cairo_save (cr);
cairo_set_line_width (cr, 6); cairo_set_line_width (cr, 6);
cairo_move_to (cr, allocation.width / 2, cairo_move_to (cr, width / 2, height / 2);
allocation.height / 2);
cairo_rel_line_to (cr, swipe_x, swipe_y); cairo_rel_line_to (cr, swipe_x, swipe_y);
cairo_set_source_rgba (cr, 1, 0, 0, 0.5); cairo_set_source_rgba (cr, 1, 0, 0, 0.5);
cairo_stroke (cr); cairo_stroke (cr);
@ -87,9 +85,7 @@ drawing_area_draw (GtkWidget *widget,
gdouble angle, scale; gdouble angle, scale;
cairo_get_matrix (cr, &matrix); cairo_get_matrix (cr, &matrix);
cairo_matrix_translate (&matrix, cairo_matrix_translate (&matrix, width / 2, height / 2);
allocation.width / 2,
allocation.height / 2);
cairo_save (cr); cairo_save (cr);
@ -116,8 +112,8 @@ drawing_area_draw (GtkWidget *widget,
if (long_pressed) if (long_pressed)
{ {
cairo_save (cr); cairo_save (cr);
cairo_arc (cr, allocation.width / 2, cairo_arc (cr,
allocation.height / 2, width / 2, height / 2,
50, 0, 2 * G_PI); 50, 0, 2 * G_PI);
cairo_set_source_rgba (cr, 0, 1, 0, 0.5); cairo_set_source_rgba (cr, 0, 1, 0, 0.5);
@ -125,8 +121,6 @@ drawing_area_draw (GtkWidget *widget,
cairo_restore (cr); cairo_restore (cr);
} }
return TRUE;
} }
GtkWidget * GtkWidget *
@ -150,8 +144,9 @@ do_gestures (GtkWidget *do_widget)
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK | GDK_TOUCH_MASK); GDK_POINTER_MOTION_MASK | GDK_TOUCH_MASK);
g_signal_connect (drawing_area, "draw", gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area),
G_CALLBACK (drawing_area_draw), NULL); drawing_area_draw,
NULL, NULL);
/* Swipe */ /* Swipe */
gesture = gtk_gesture_swipe_new (drawing_area); gesture = gtk_gesture_swipe_new (drawing_area);

View File

@ -75,15 +75,15 @@ load_pixbufs (GError **error)
} }
/* Expose callback for the drawing area */ /* Expose callback for the drawing area */
static gint static void
draw_cb (GtkWidget *widget, draw_func (GtkDrawingArea *area,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
gdk_cairo_set_source_pixbuf (cr, frame, 0, 0); gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
cairo_paint (cr); cairo_paint (cr);
return TRUE;
} }
#define CYCLE_TIME 3000000 /* 3 seconds */ #define CYCLE_TIME 3000000 /* 3 seconds */
@ -204,14 +204,13 @@ do_pixbufs (GtkWidget *do_widget)
} }
else else
{ {
gtk_widget_set_size_request (window, back_width, back_height);
frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height); frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
g_signal_connect (da, "draw", gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), back_width);
G_CALLBACK (draw_cb), NULL); gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (da), back_height);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func, NULL, NULL);
gtk_container_add (GTK_CONTAINER (window), da); gtk_container_add (GTK_CONTAINER (window), da);

View File

@ -88,10 +88,12 @@ create_fancy_attr_list_for_layout (PangoLayout *layout)
return attrs; return attrs;
} }
static gboolean static void
rotated_text_draw (GtkWidget *widget, rotated_text_draw (GtkDrawingArea *da,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
#define RADIUS 150 #define RADIUS 150
#define N_WORDS 5 #define N_WORDS 5
@ -106,15 +108,12 @@ rotated_text_draw (GtkWidget *widget,
PangoAttrList *attrs; PangoAttrList *attrs;
double device_radius; double device_radius;
int width, height;
int i; int i;
/* Create a cairo context and set up a transformation matrix so that the user /* Create a cairo context and set up a transformation matrix so that the user
* space coordinates for the centered square where we draw are [-RADIUS, RADIUS], * space coordinates for the centered square where we draw are [-RADIUS, RADIUS],
* [-RADIUS, RADIUS]. * [-RADIUS, RADIUS].
* We first center, then change the scale. */ * We first center, then change the scale. */
width = gtk_widget_get_allocated_width (widget);
height = gtk_widget_get_allocated_height (widget);
device_radius = MIN (width, height) / 2.; device_radius = MIN (width, height) / 2.;
cairo_translate (cr, cairo_translate (cr,
device_radius + (width - 2 * device_radius) / 2, device_radius + (width - 2 * device_radius) / 2,
@ -128,7 +127,7 @@ rotated_text_draw (GtkWidget *widget,
cairo_set_source (cr, pattern); cairo_set_source (cr, pattern);
/* Create a PangoContext and set up our shape renderer */ /* Create a PangoContext and set up our shape renderer */
context = gtk_widget_create_pango_context (widget); context = gtk_widget_create_pango_context (GTK_WIDGET (da));
pango_cairo_context_set_shape_renderer (context, pango_cairo_context_set_shape_renderer (context,
fancy_shape_renderer, fancy_shape_renderer,
NULL, NULL); NULL, NULL);
@ -164,8 +163,6 @@ rotated_text_draw (GtkWidget *widget,
g_object_unref (layout); g_object_unref (layout);
g_object_unref (context); g_object_unref (context);
cairo_pattern_destroy (pattern); cairo_pattern_destroy (pattern);
return FALSE;
} }
GtkWidget * GtkWidget *
@ -199,8 +196,9 @@ do_rotated_text (GtkWidget *do_widget)
gtk_style_context_add_class (gtk_widget_get_style_context (drawing_area), gtk_style_context_add_class (gtk_widget_get_style_context (drawing_area),
GTK_STYLE_CLASS_VIEW); GTK_STYLE_CLASS_VIEW);
g_signal_connect (drawing_area, "draw", gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area),
G_CALLBACK (rotated_text_draw), NULL); rotated_text_draw,
NULL, NULL);
/* And a label */ /* And a label */
label = gtk_label_new (text); label = gtk_label_new (text);

View File

@ -7,10 +7,12 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
static gboolean static void
draw_text (GtkWidget *da, draw_text (GtkDrawingArea *da,
cairo_t *cr, cairo_t *cr,
gpointer data) int width,
int height,
gpointer data)
{ {
cairo_pattern_t *pattern; cairo_pattern_t *pattern;
PangoLayout *layout; PangoLayout *layout;
@ -18,7 +20,7 @@ draw_text (GtkWidget *da,
cairo_save (cr); cairo_save (cr);
layout = gtk_widget_create_pango_layout (da, "Pango power!\nPango power!\nPango power!"); layout = gtk_widget_create_pango_layout (GTK_WIDGET (da), "Pango power!\nPango power!\nPango power!");
desc = pango_font_description_from_string ("sans bold 34"); desc = pango_font_description_from_string ("sans bold 34");
pango_layout_set_font_description (layout, desc); pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc); pango_font_description_free (desc);
@ -27,9 +29,7 @@ draw_text (GtkWidget *da,
pango_cairo_layout_path (cr, layout); pango_cairo_layout_path (cr, layout);
g_object_unref (layout); g_object_unref (layout);
pattern = cairo_pattern_create_linear (0.0, 0.0, pattern = cairo_pattern_create_linear (0.0, 0.0, width, height);
gtk_widget_get_allocated_width (da),
gtk_widget_get_allocated_height (da));
cairo_pattern_add_color_stop_rgb (pattern, 0.0, 1.0, 0.0, 0.0); cairo_pattern_add_color_stop_rgb (pattern, 0.0, 1.0, 0.0, 0.0);
cairo_pattern_add_color_stop_rgb (pattern, 0.2, 1.0, 0.0, 0.0); cairo_pattern_add_color_stop_rgb (pattern, 0.2, 1.0, 0.0, 0.0);
cairo_pattern_add_color_stop_rgb (pattern, 0.3, 1.0, 1.0, 0.0); cairo_pattern_add_color_stop_rgb (pattern, 0.3, 1.0, 1.0, 0.0);
@ -49,8 +49,6 @@ draw_text (GtkWidget *da,
cairo_stroke (cr); cairo_stroke (cr);
cairo_restore (cr); cairo_restore (cr);
return TRUE;
} }
GtkWidget * GtkWidget *
@ -72,8 +70,7 @@ do_textmask (GtkWidget *do_widget)
da = gtk_drawing_area_new (); da = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER (window), da); gtk_container_add (GTK_CONTAINER (window), da);
g_signal_connect (da, "draw", gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_text, NULL, NULL);
G_CALLBACK (draw_text), NULL);
} }
if (!gtk_widget_get_visible (window)) if (!gtk_widget_get_visible (window))