Code

fix artefacts left by text cursor and disappearing cursor on empty canvas
[inkscape.git] / src / display / sp-ctrlline.cpp
1 #define __INKSCAPE_CTRLLINE_C__
3 /*
4  * Simple straight line
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2007 Johan Engelen
11  * Copyright (C) 1999-2002 Lauris Kaplinski
12  *
13  * Released under GNU GPL
14  */
16 /*
17  * TODO:
18  * Draw it by hand - we really do not need aa stuff for it
19  *
20  */
22 #include "display-forward.h"
23 #include "sp-canvas-util.h"
24 #include "sp-ctrlline.h"
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 #include <color.h>
30 #include "display/inkscape-cairo.h"
33 static void sp_ctrlline_class_init (SPCtrlLineClass *klass);
34 static void sp_ctrlline_init (SPCtrlLine *ctrlline);
35 static void sp_ctrlline_destroy (GtkObject *object);
37 static void sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
38 static void sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf);
40 static SPCanvasItemClass *parent_class;
42 GtkType
43 sp_ctrlline_get_type (void)
44 {
45     static GtkType type = 0;
47     if (!type) {
48         GtkTypeInfo info = {
49             "SPCtrlLine",
50             sizeof (SPCtrlLine),
51             sizeof (SPCtrlLineClass),
52             (GtkClassInitFunc) sp_ctrlline_class_init,
53             (GtkObjectInitFunc) sp_ctrlline_init,
54             NULL, NULL, NULL
55         };
56         type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
57     }
58     return type;
59 }
61 static void
62 sp_ctrlline_class_init (SPCtrlLineClass *klass)
63 {
64     GtkObjectClass *object_class = (GtkObjectClass *) klass;
65     SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
67     parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
69     object_class->destroy = sp_ctrlline_destroy;
71     item_class->update = sp_ctrlline_update;
72     item_class->render = sp_ctrlline_render;
73 }
75 static void
76 sp_ctrlline_init (SPCtrlLine *ctrlline)
77 {
78     ctrlline->rgba = 0x0000ff7f;
79     ctrlline->s[NR::X] = ctrlline->s[NR::Y] = ctrlline->e[NR::X] = ctrlline->e[NR::Y] = 0.0;
80     ctrlline->item=NULL;
81 }
83 static void
84 sp_ctrlline_destroy (GtkObject *object)
85 {
86     g_return_if_fail (object != NULL);
87     g_return_if_fail (SP_IS_CTRLLINE (object));
89     SPCtrlLine *ctrlline = SP_CTRLLINE (object);
91     ctrlline->item=NULL;
93     if (GTK_OBJECT_CLASS (parent_class)->destroy)
94         (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
95 }
97 static void
98 sp_ctrlline_render (SPCanvasItem *item, SPCanvasBuf *buf)
99 {
100     SPCtrlLine *cl = SP_CTRLLINE (item);
102     if (!buf->ct)
103         return;
105     sp_canvas_prepare_buffer (buf);
107     guint32 rgba = cl->rgba;
108     cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
110     cairo_set_line_width(buf->ct, 1);
111     cairo_new_path(buf->ct);
113     NR::Point s = cl->s * cl->affine;
114     NR::Point e = cl->e * cl->affine;
116     cairo_move_to (buf->ct, s[NR::X] - buf->rect.x0, s[NR::Y] - buf->rect.y0);
117     cairo_line_to (buf->ct, e[NR::X] - buf->rect.x0, e[NR::Y] - buf->rect.y0);
119     cairo_stroke(buf->ct);
122 static void
123 sp_ctrlline_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
125     SPCtrlLine *cl = SP_CTRLLINE (item);
127     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
129     if (parent_class->update)
130         (* parent_class->update) (item, affine, flags);
132     sp_canvas_item_reset_bounds (item);
134     cl->affine = affine;
136     NR::Point s = cl->s * affine;
137     NR::Point e = cl->e * affine;
139     item->x1 = round(MIN(s[NR::X], e[NR::X]) - 1);
140     item->y1 = round(MIN(s[NR::Y], e[NR::Y]) - 1);
141     item->x2 = round(MAX(s[NR::X], e[NR::X]) + 1);
142     item->y2 = round(MAX(s[NR::Y], e[NR::Y]) + 1);
144     sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
147 void
148 sp_ctrlline_set_rgba32 (SPCtrlLine *cl, guint32 rgba)
150     g_return_if_fail (cl != NULL);
151     g_return_if_fail (SP_IS_CTRLLINE (cl));
153     if (rgba != cl->rgba) {
154         SPCanvasItem *item;
155         cl->rgba = rgba;
156         item = SP_CANVAS_ITEM (cl);
157         sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
158     }
161 #define EPSILON 1e-6
162 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
164 void
165 sp_ctrlline_set_coords (SPCtrlLine *cl, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
167     g_return_if_fail (cl != NULL);
168     g_return_if_fail (SP_IS_CTRLLINE (cl));
170     if (DIFFER (x0, cl->s[NR::X]) || DIFFER (y0, cl->s[NR::Y]) || DIFFER (x1, cl->e[NR::X]) || DIFFER (y1, cl->e[NR::Y])) {
171         cl->s[NR::X] = x0;
172         cl->s[NR::Y] = y0;
173         cl->e[NR::X] = x1;
174         cl->e[NR::Y] = y1;
175         sp_canvas_item_request_update (SP_CANVAS_ITEM (cl));
176     }
179 void
180 sp_ctrlline_set_coords (SPCtrlLine *cl, const NR::Point start, const NR::Point end)
182     sp_ctrlline_set_coords(cl, start[0], start[1], end[0], end[1]);
185 /*
186   Local Variables:
187   mode:c++
188   c-file-style:"stroustrup"
189   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
190   indent-tabs-mode:nil
191   fill-column:99
192   End:
193 */
194 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :