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
*/
static gboolean
draw_callback (GtkWidget *widget,
cairo_t *cr,
gpointer data)
static void
draw_function (GtkDrawingArea *da,
cairo_t *cr,
int width,
int height,
gpointer data)
{
gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr);
return TRUE;
}
static void
@ -87,11 +87,9 @@ do_colorsel (GtkWidget *do_widget)
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE);
da = gtk_drawing_area_new ();
g_signal_connect (da, "draw", G_CALLBACK (draw_callback), NULL);
/* set a minimum size */
gtk_widget_set_size_request (da, 200, 200);
gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (da), 200);
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);
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 ());
}
static gboolean
drawing_area_draw (GtkWidget *widget,
cairo_t *cr)
static void
drawing_area_draw (GtkDrawingArea *da,
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,
0, 0,
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;
gtk_render_background (context, cr, 0, 0, width, height);
gtk_render_frame (context, cr, 0, 0, width, height);
}
static void
@ -105,8 +100,9 @@ do_css_multiplebgs (GtkWidget *do_widget)
child = gtk_drawing_area_new ();
gtk_widget_set_name (child, "canvas");
g_signal_connect (child, "draw",
G_CALLBACK (drawing_area_draw), NULL);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (child),
drawing_area_draw,
NULL, NULL);
gtk_container_add (GTK_CONTAINER (container), child);
child = gtk_button_new ();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -88,10 +88,12 @@ create_fancy_attr_list_for_layout (PangoLayout *layout)
return attrs;
}
static gboolean
rotated_text_draw (GtkWidget *widget,
cairo_t *cr,
gpointer data)
static void
rotated_text_draw (GtkDrawingArea *da,
cairo_t *cr,
int width,
int height,
gpointer data)
{
#define RADIUS 150
#define N_WORDS 5
@ -106,15 +108,12 @@ rotated_text_draw (GtkWidget *widget,
PangoAttrList *attrs;
double device_radius;
int width, height;
int i;
/* 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],
* [-RADIUS, RADIUS].
* 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.;
cairo_translate (cr,
device_radius + (width - 2 * device_radius) / 2,
@ -128,7 +127,7 @@ rotated_text_draw (GtkWidget *widget,
cairo_set_source (cr, pattern);
/* 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,
fancy_shape_renderer,
NULL, NULL);
@ -164,8 +163,6 @@ rotated_text_draw (GtkWidget *widget,
g_object_unref (layout);
g_object_unref (context);
cairo_pattern_destroy (pattern);
return FALSE;
}
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_CLASS_VIEW);
g_signal_connect (drawing_area, "draw",
G_CALLBACK (rotated_text_draw), NULL);
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area),
rotated_text_draw,
NULL, NULL);
/* And a label */
label = gtk_label_new (text);

View File

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