sp-ctrlquadr.cpp revision 055bb425d5d2ab1c5c81df150d221a6f9b507cd4
#define __INKSCAPE_CTRLQUADR_C__
/*
* Quadrilateral
*
* Authors:
* bulia byak
*
* Copyright (C) 2005 authors
*
* Released under GNU GPL
*/
#include "display-forward.h"
#include "sp-canvas-util.h"
#include "sp-ctrlquadr.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "display/inkscape-cairo.h"
#include "color.h"
struct SPCtrlQuadr : public SPCanvasItem{
guint32 rgba;
NR::Point p1, p2, p3, p4;
NR::Matrix affine;
};
struct SPCtrlQuadrClass : public SPCanvasItemClass{};
static void sp_ctrlquadr_class_init (SPCtrlQuadrClass *klass);
static void sp_ctrlquadr_init (SPCtrlQuadr *ctrlquadr);
static void sp_ctrlquadr_destroy (GtkObject *object);
static void sp_ctrlquadr_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
static void sp_ctrlquadr_render (SPCanvasItem *item, SPCanvasBuf *buf);
static SPCanvasItemClass *parent_class;
GtkType
sp_ctrlquadr_get_type (void)
{
static GtkType type = 0;
if (!type) {
GtkTypeInfo info = {
"SPCtrlQuadr",
sizeof (SPCtrlQuadr),
sizeof (SPCtrlQuadrClass),
(GtkClassInitFunc) sp_ctrlquadr_class_init,
(GtkObjectInitFunc) sp_ctrlquadr_init,
NULL, NULL, NULL
};
type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
}
return type;
}
static void
sp_ctrlquadr_class_init (SPCtrlQuadrClass *klass)
{
GtkObjectClass *object_class = (GtkObjectClass *) klass;
SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
object_class->destroy = sp_ctrlquadr_destroy;
item_class->update = sp_ctrlquadr_update;
item_class->render = sp_ctrlquadr_render;
}
static void
sp_ctrlquadr_init (SPCtrlQuadr *ctrlquadr)
{
ctrlquadr->rgba = 0x000000ff;
ctrlquadr->p1 = NR::Point(0, 0);
ctrlquadr->p2 = NR::Point(0, 0);
ctrlquadr->p3 = NR::Point(0, 0);
ctrlquadr->p4 = NR::Point(0, 0);
}
static void
sp_ctrlquadr_destroy (GtkObject *object)
{
g_return_if_fail (object != NULL);
g_return_if_fail (SP_IS_CTRLQUADR (object));
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void
sp_ctrlquadr_render (SPCanvasItem *item, SPCanvasBuf *buf)
{
SPCtrlQuadr *cq = SP_CTRLQUADR (item);
NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
if (!buf->ct)
return;
// RGB / BGR
cairo_new_path(buf->ct);
NR::Point min = NR::Point(buf->rect.x0, buf->rect.y0);
NR::Point p1 = (cq->p1 * cq->affine) - min;
NR::Point p2 = (cq->p2 * cq->affine) - min;
NR::Point p3 = (cq->p3 * cq->affine) - min;
NR::Point p4 = (cq->p4 * cq->affine) - min;
cairo_move_to(buf->ct, p1[NR::X], p1[NR::Y]);
cairo_line_to(buf->ct, p2[NR::X], p2[NR::Y]);
cairo_line_to(buf->ct, p3[NR::X], p3[NR::Y]);
cairo_line_to(buf->ct, p4[NR::X], p4[NR::Y]);
cairo_line_to(buf->ct, p1[NR::X], p1[NR::Y]);
// FIXME: this is supposed to draw inverse but cairo apparently is unable of this trick :(
//cairo_set_operator (buf->ct, CAIRO_OPERATOR_XOR);
cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cq->rgba), SP_RGBA32_G_F(cq->rgba), SP_RGBA32_R_F(cq->rgba), SP_RGBA32_A_F(cq->rgba));
cairo_fill(buf->ct);
}
#define MIN4(a,b,c,d)\
((a <= b && a <= c && a <= d) ? a : \
(b <= a && b <= c && b <= d) ? b : \
(c <= a && c <= b && c <= d) ? c : \
d )
#define MAX4(a,b,c,d)\
((a >= b && a >= c && a >= d) ? a : \
(b >= a && b >= c && b >= d) ? b : \
(c >= a && c >= b && c >= d) ? c : \
d )
static void
sp_ctrlquadr_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
{
SPCtrlQuadr *cq = SP_CTRLQUADR (item);
sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
if (parent_class->update)
(* parent_class->update) (item, affine, flags);
sp_canvas_item_reset_bounds (item);
cq->affine = affine;
NR::Point p1(cq->p1 * affine);
NR::Point p2(cq->p2 * affine);
NR::Point p3(cq->p3 * affine);
NR::Point p4(cq->p4 * affine);
item->x1 = (int)(MIN4(p1[NR::X], p2[NR::X], p3[NR::X], p4[NR::X]));
item->y1 = (int)(MIN4(p1[NR::Y], p2[NR::Y], p3[NR::Y], p4[NR::Y]));
item->x2 = (int)(MAX4(p1[NR::X], p2[NR::X], p3[NR::X], p4[NR::X]));
item->y2 = (int)(MAX4(p1[NR::Y], p2[NR::Y], p3[NR::Y], p4[NR::Y]));
sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
}
void
sp_ctrlquadr_set_rgba32 (SPCtrlQuadr *cl, guint32 rgba)
{
g_return_if_fail (cl != NULL);
g_return_if_fail (SP_IS_CTRLQUADR (cl));
if (rgba != cl->rgba) {
SPCanvasItem *item;
cl->rgba = rgba;
item = SP_CANVAS_ITEM (cl);
sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
}
}
void
sp_ctrlquadr_set_coords (SPCtrlQuadr *cl, NR::Point p1, NR::Point p2, NR::Point p3, NR::Point p4)
{
g_return_if_fail (cl != NULL);
g_return_if_fail (SP_IS_CTRLQUADR (cl));
if (p1 != cl->p1 || p2 != cl->p2 || p3 != cl->p3 || p4 != cl->p4) {
cl->p1 = p1;
cl->p2 = p2;
cl->p3 = p3;
cl->p4 = p4;
sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :