2009-11-14 16:39:22 -05:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:st-drawing-area
|
|
|
|
* @short_description: A dynamically-sized Cairo drawing area
|
|
|
|
*
|
|
|
|
* #StDrawingArea is similar to #ClutterCairoTexture in that
|
|
|
|
* it allows drawing via Cairo; the primary difference is that
|
2010-03-03 18:00:05 -05:00
|
|
|
* it is dynamically sized. To use, connect to the #StDrawingArea::repaint
|
2009-11-14 16:39:22 -05:00
|
|
|
* signal, and inside the signal handler, call
|
2010-03-03 18:00:05 -05:00
|
|
|
* st_drawing_area_get_context() to get the Cairo context to draw to. The
|
|
|
|
* #StDrawingArea::repaint signal will be emitted by default when the area is
|
2009-11-14 16:39:22 -05:00
|
|
|
* resized or the CSS style changes; you can use the
|
2010-03-03 18:00:05 -05:00
|
|
|
* st_drawing_area_queue_repaint() as well.
|
2009-11-14 16:39:22 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "st-drawing-area.h"
|
|
|
|
|
|
|
|
#include <cairo.h>
|
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
/* Cairo stores the data in native byte order as ARGB but Cogl's pixel
|
|
|
|
formats specify the actual byte order. Therefore we need to use a
|
|
|
|
different format depending on the architecture */
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
#define PIXEL_FORMAT COGL_PIXEL_FORMAT_BGRA_8888_PRE
|
|
|
|
#else
|
|
|
|
#define PIXEL_FORMAT COGL_PIXEL_FORMAT_ARGB_8888_PRE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(StDrawingArea, st_drawing_area, ST_TYPE_WIDGET);
|
2009-11-14 16:39:22 -05:00
|
|
|
|
|
|
|
struct _StDrawingAreaPrivate {
|
2010-07-22 12:53:58 -04:00
|
|
|
CoglHandle texture;
|
|
|
|
CoglHandle material;
|
2010-03-03 18:00:05 -05:00
|
|
|
cairo_t *context;
|
|
|
|
guint needs_repaint : 1;
|
|
|
|
guint in_repaint : 1;
|
2009-11-14 16:39:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Signals */
|
|
|
|
enum
|
|
|
|
{
|
2010-03-03 18:00:05 -05:00
|
|
|
REPAINT,
|
2009-11-14 16:39:22 -05:00
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint st_drawing_area_signals [LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
static void
|
2010-03-03 18:00:05 -05:00
|
|
|
st_drawing_area_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
StDrawingArea *area = ST_DRAWING_AREA (object);
|
|
|
|
StDrawingAreaPrivate *priv = area->priv;
|
|
|
|
|
|
|
|
if (priv->material != COGL_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
cogl_handle_unref (priv->material);
|
|
|
|
priv->material = COGL_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->texture != COGL_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
cogl_handle_unref (priv->texture);
|
|
|
|
priv->texture = COGL_INVALID_HANDLE;
|
|
|
|
}
|
2010-03-10 18:22:06 -05:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (st_drawing_area_parent_class)->dispose (object);
|
2010-03-03 18:00:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_drawing_area_paint (ClutterActor *self)
|
2009-11-14 16:39:22 -05:00
|
|
|
{
|
|
|
|
StDrawingArea *area = ST_DRAWING_AREA (self);
|
2010-03-03 18:00:05 -05:00
|
|
|
StDrawingAreaPrivate *priv = area->priv;
|
|
|
|
StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
|
|
|
|
ClutterActorBox allocation_box;
|
|
|
|
ClutterActorBox content_box;
|
|
|
|
int width, height;
|
|
|
|
CoglColor color;
|
|
|
|
guint8 paint_opacity;
|
|
|
|
|
|
|
|
(CLUTTER_ACTOR_CLASS (st_drawing_area_parent_class))->paint (self);
|
|
|
|
|
|
|
|
clutter_actor_get_allocation_box (self, &allocation_box);
|
|
|
|
st_theme_node_get_content_box (theme_node, &allocation_box, &content_box);
|
2009-11-14 16:39:22 -05:00
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
width = (int)(0.5 + content_box.x2 - content_box.x1);
|
|
|
|
height = (int)(0.5 + content_box.y2 - content_box.y1);
|
2009-11-14 16:39:22 -05:00
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
if (priv->material == COGL_INVALID_HANDLE)
|
|
|
|
priv->material = cogl_material_new ();
|
2009-11-14 16:39:22 -05:00
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
if (priv->texture != COGL_INVALID_HANDLE &&
|
|
|
|
(width != cogl_texture_get_width (priv->texture) ||
|
|
|
|
height != cogl_texture_get_height (priv->texture)))
|
|
|
|
{
|
|
|
|
cogl_handle_unref (priv->texture);
|
|
|
|
priv->texture = COGL_INVALID_HANDLE;
|
|
|
|
}
|
2009-11-14 16:39:22 -05:00
|
|
|
|
|
|
|
if (width > 0 && height > 0)
|
|
|
|
{
|
2010-03-03 18:00:05 -05:00
|
|
|
if (priv->texture == COGL_INVALID_HANDLE)
|
|
|
|
{
|
|
|
|
priv->texture = cogl_texture_new_with_size (width, height,
|
|
|
|
COGL_TEXTURE_NONE,
|
|
|
|
PIXEL_FORMAT);
|
|
|
|
priv->needs_repaint = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->needs_repaint)
|
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
|
|
|
|
priv->context = cairo_create (surface);
|
|
|
|
priv->in_repaint = TRUE;
|
2010-10-15 03:18:37 -04:00
|
|
|
priv->needs_repaint = FALSE;
|
2010-03-03 18:00:05 -05:00
|
|
|
|
|
|
|
g_signal_emit ((GObject*)area, st_drawing_area_signals[REPAINT], 0);
|
|
|
|
|
|
|
|
priv->in_repaint = FALSE;
|
|
|
|
cairo_destroy (priv->context);
|
|
|
|
priv->context = NULL;
|
|
|
|
|
|
|
|
cogl_texture_set_region (priv->texture, 0, 0, 0, 0, width, height, width, height,
|
|
|
|
PIXEL_FORMAT,
|
|
|
|
cairo_image_surface_get_stride (surface),
|
|
|
|
cairo_image_surface_get_data (surface));
|
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cogl_material_set_layer (priv->material, 0, priv->texture);
|
|
|
|
|
|
|
|
if (priv->texture)
|
|
|
|
{
|
|
|
|
paint_opacity = clutter_actor_get_paint_opacity (self);
|
|
|
|
cogl_color_set_from_4ub (&color,
|
|
|
|
paint_opacity, paint_opacity, paint_opacity, paint_opacity);
|
|
|
|
cogl_material_set_color (priv->material, &color);
|
|
|
|
|
|
|
|
cogl_set_source (priv->material);
|
|
|
|
cogl_rectangle_with_texture_coords (content_box.x1, content_box.y1,
|
|
|
|
width, height,
|
|
|
|
0.0f, 0.0f, 1.0f, 1.0f);
|
2009-11-14 16:39:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_drawing_area_style_changed (StWidget *self)
|
|
|
|
{
|
2010-03-03 18:00:05 -05:00
|
|
|
StDrawingArea *area = ST_DRAWING_AREA (self);
|
|
|
|
StDrawingAreaPrivate *priv = area->priv;
|
|
|
|
|
2009-11-14 16:39:22 -05:00
|
|
|
(ST_WIDGET_CLASS (st_drawing_area_parent_class))->style_changed (self);
|
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
priv->needs_repaint = TRUE;
|
2009-11-14 16:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_drawing_area_class_init (StDrawingAreaClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
|
|
|
|
StWidgetClass *widget_class = ST_WIDGET_CLASS (klass);
|
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
gobject_class->dispose = st_drawing_area_dispose;
|
|
|
|
actor_class->paint = st_drawing_area_paint;
|
2009-11-14 16:39:22 -05:00
|
|
|
widget_class->style_changed = st_drawing_area_style_changed;
|
|
|
|
|
2010-03-03 18:00:05 -05:00
|
|
|
st_drawing_area_signals[REPAINT] =
|
|
|
|
g_signal_new ("repaint",
|
2009-11-14 16:39:22 -05:00
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
2010-03-03 18:00:05 -05:00
|
|
|
G_STRUCT_OFFSET (StDrawingAreaClass, repaint),
|
2009-11-14 16:39:22 -05:00
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__OBJECT,
|
2010-03-03 18:00:05 -05:00
|
|
|
G_TYPE_NONE, 0);
|
2009-11-14 16:39:22 -05:00
|
|
|
|
|
|
|
g_type_class_add_private (gobject_class, sizeof (StDrawingAreaPrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_drawing_area_init (StDrawingArea *area)
|
|
|
|
{
|
|
|
|
area->priv = G_TYPE_INSTANCE_GET_PRIVATE (area, ST_TYPE_DRAWING_AREA,
|
|
|
|
StDrawingAreaPrivate);
|
2010-03-03 18:00:05 -05:00
|
|
|
area->priv->texture = COGL_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_drawing_area_queue_repaint:
|
|
|
|
* @area: the #StDrawingArea
|
|
|
|
*
|
|
|
|
* Will cause the actor to emit a ::repaint signal before it is next
|
|
|
|
* drawn to the scene. Useful if some parameters for the area being
|
|
|
|
* drawn other than the size or style have changed. Note that
|
|
|
|
* clutter_actor_queue_redraw() will simply result in the same
|
|
|
|
* contents being drawn to the scene again.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
st_drawing_area_queue_repaint (StDrawingArea *area)
|
|
|
|
{
|
|
|
|
StDrawingAreaPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (ST_IS_DRAWING_AREA (area));
|
|
|
|
|
|
|
|
priv = area->priv;
|
|
|
|
|
|
|
|
priv->needs_repaint = TRUE;
|
|
|
|
clutter_actor_queue_redraw (CLUTTER_ACTOR (area));
|
2009-11-14 16:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-03 18:00:05 -05:00
|
|
|
* st_drawing_area_get_context:
|
|
|
|
* @area: the #StDrawingArea
|
2009-11-14 16:39:22 -05:00
|
|
|
*
|
2010-03-03 18:00:05 -05:00
|
|
|
* Gets the Cairo context to paint to. This function must only be called
|
|
|
|
* from a signal hander for the ::repaint signal.
|
|
|
|
*
|
|
|
|
* Return Value: (transfer none): the Cairo context for the paint operation
|
2009-11-14 16:39:22 -05:00
|
|
|
*/
|
2010-03-03 18:00:05 -05:00
|
|
|
cairo_t *
|
|
|
|
st_drawing_area_get_context (StDrawingArea *area)
|
2009-11-14 16:39:22 -05:00
|
|
|
{
|
2010-03-03 18:00:05 -05:00
|
|
|
g_return_val_if_fail (ST_IS_DRAWING_AREA (area), NULL);
|
|
|
|
g_return_val_if_fail (area->priv->in_repaint, NULL);
|
|
|
|
|
|
|
|
return area->priv->context;
|
2009-11-14 16:39:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-03 18:00:05 -05:00
|
|
|
* st_drawing_area_get_surface_size:
|
|
|
|
* @area: the #StDrawingArea
|
|
|
|
* @width: (out): location to store the width of the painted area
|
|
|
|
* @height: (out): location to store the height of the painted area
|
2009-11-14 16:39:22 -05:00
|
|
|
*
|
2010-03-03 18:00:05 -05:00
|
|
|
* Gets the size of the cairo surface being painted to, which is equal
|
|
|
|
* to the size of the content area of the widget. This function must
|
|
|
|
* only be called from a signal hander for the ::repaint signal.
|
2009-11-14 16:39:22 -05:00
|
|
|
*/
|
|
|
|
void
|
2010-03-03 18:00:05 -05:00
|
|
|
st_drawing_area_get_surface_size (StDrawingArea *area,
|
|
|
|
guint *width,
|
|
|
|
guint *height)
|
2009-11-14 16:39:22 -05:00
|
|
|
{
|
2010-03-03 18:00:05 -05:00
|
|
|
StDrawingAreaPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (ST_IS_DRAWING_AREA (area));
|
|
|
|
g_return_if_fail (area->priv->in_repaint);
|
|
|
|
|
|
|
|
priv = area->priv;
|
|
|
|
|
|
|
|
if (width)
|
|
|
|
*width = cogl_texture_get_width (priv->texture);
|
|
|
|
if (height)
|
|
|
|
*height = cogl_texture_get_height (priv->texture);
|
2009-11-14 16:39:22 -05:00
|
|
|
}
|