Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / display / guideline.cpp
index 51b2ec38deeeed821ad9a0243323d77643bb8bf8..ce1289faf30adf6a0d73ed97ac6338f937da14b9 100644 (file)
@@ -1,32 +1,33 @@
-#define __SP_GUIDELINE_C__
-
 /*
  * Horizontal/vertical but can also be angled line
  *
  * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   Johan Engelen
+ *   Maximilian Albert <maximilian.albert@gmail.com>
  *
  * Copyright (C) 2000-2002 Lauris Kaplinski
  * Copyright (C) 2007 Johan Engelen
+ * Copyright (C) 2009 Maximilian Albert
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
 
 #include <libnr/nr-pixops.h>
-#include "display-forward.h"
+#include <2geom/transforms.h>
 #include "sp-canvas-util.h"
+#include "sp-ctrlpoint.h"
 #include "guideline.h"
 
 static void sp_guideline_class_init(SPGuideLineClass *c);
 static void sp_guideline_init(SPGuideLine *guideline);
 static void sp_guideline_destroy(GtkObject *object);
 
-static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
+static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
 static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf);
 
-static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
+static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
 
 static void sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, guint32 rgba);
 
@@ -75,10 +76,24 @@ static void sp_guideline_init(SPGuideLine *gl)
     gl->angle = 3.14159265358979323846/2;
     gl->point_on_line = Geom::Point(0,0);
     gl->sensitive = 0;
+
+    gl->origin = NULL;
 }
 
 static void sp_guideline_destroy(GtkObject *object)
 {
+    g_return_if_fail (object != NULL);
+    g_return_if_fail (SP_IS_GUIDELINE (object));
+    //g_return_if_fail (SP_GUIDELINE(object)->origin != NULL);
+    //g_return_if_fail (SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin));
+    
+    if (SP_GUIDELINE(object)->origin != NULL && SP_IS_CTRLPOINT(SP_GUIDELINE(object)->origin)) {
+        gtk_object_destroy(GTK_OBJECT(SP_GUIDELINE(object)->origin));
+    } else {
+        // FIXME: This branch shouldn't be reached (although it seems to be harmless).
+        //g_error("Why can it be that gl->origin is not a valid SPCtrlPoint?\n");
+    }
+
     GTK_OBJECT_CLASS(parent_class)->destroy(object);
 }
 
@@ -94,7 +109,7 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
     unsigned int const a = NR_RGBA32_A (gl->rgba);
 
     if (gl->is_vertical()) {
-        int position = gl->point_on_line[Geom::X];
+        int position = (int) Inkscape::round(gl->point_on_line[Geom::X]);
         if (position < buf->rect.x0 || position >= buf->rect.x1) {
             return;
         }
@@ -102,7 +117,7 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
         int p0 = buf->rect.y0;
         int p1 = buf->rect.y1;
         int step = buf->buf_rowstride;
-        unsigned char *d = buf->buf + 3 * (position - buf->rect.x0);
+        unsigned char *d = buf->buf + 4 * (position - buf->rect.x0);
 
         for (int p = p0; p < p1; p++) {
             d[0] = NR_COMPOSEN11_1111(r, a, d[0]);
@@ -111,14 +126,14 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
             d += step;
         }
     } else if (gl->is_horizontal()) {
-        int position = gl->point_on_line[Geom::Y];
+        int position = (int) Inkscape::round(gl->point_on_line[Geom::Y]);
         if (position < buf->rect.y0 || position >= buf->rect.y1) {
             return;
         }
 
         int p0 = buf->rect.x0;
         int p1 = buf->rect.x1;
-        int step = 3;
+        int step = 4;
         unsigned char *d = buf->buf + (position - buf->rect.y0) * buf->buf_rowstride;
 
         for (int p = p0; p < p1; p++) {
@@ -130,14 +145,14 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
     } else {
         // render angled line, once intersection has been detected, draw from there.
         Geom::Point parallel_to_line( gl->normal_to_line[Geom::Y],
-                                      /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]); 
+                                      /*should be minus, but inverted y axis*/ gl->normal_to_line[Geom::X]);
 
         //try to intersect with left vertical of rect
         double y_intersect_left = (buf->rect.x0 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
         if ( (y_intersect_left >= buf->rect.y0) && (y_intersect_left <= buf->rect.y1) ) {
             // intersects with left vertical!
             double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
-            sp_guideline_drawline (buf, buf->rect.x0, round(y_intersect_left), buf->rect.x1, round(y_intersect_right), gl->rgba);
+            sp_guideline_drawline (buf, buf->rect.x0, static_cast<gint>(round(y_intersect_left)), buf->rect.x1, static_cast<gint>(round(y_intersect_right)), gl->rgba);
             return;
         }
 
@@ -145,7 +160,7 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
         double y_intersect_right = (buf->rect.x1 - gl->point_on_line[Geom::X]) * parallel_to_line[Geom::Y] / parallel_to_line[Geom::X] + gl->point_on_line[Geom::Y];
         if ( (y_intersect_right >= buf->rect.y0) && (y_intersect_right <= buf->rect.y1) ) {
             // intersects with right vertical!
-            sp_guideline_drawline (buf, buf->rect.x1, round(y_intersect_right), buf->rect.x0, round(y_intersect_left), gl->rgba);
+            sp_guideline_drawline (buf, buf->rect.x1, static_cast<gint>(round(y_intersect_right)), buf->rect.x0, static_cast<gint>(round(y_intersect_left)), gl->rgba);
             return;
         }
 
@@ -154,7 +169,7 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
             // intersects with top horizontal!
             double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
-            sp_guideline_drawline (buf, round(x_intersect_top), buf->rect.y0, round(x_intersect_bottom), buf->rect.y1, gl->rgba);
+            sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, gl->rgba);
             return;
         }
 
@@ -162,13 +177,13 @@ static void sp_guideline_render(SPCanvasItem *item, SPCanvasBuf *buf)
         double x_intersect_bottom = (buf->rect.y1 - gl->point_on_line[Geom::Y]) * parallel_to_line[Geom::X] / parallel_to_line[Geom::Y] + gl->point_on_line[Geom::X];
         if ( (x_intersect_top >= buf->rect.x0) && (x_intersect_top <= buf->rect.x1) ) {
             // intersects with bottom horizontal!
-            sp_guideline_drawline (buf, round(x_intersect_bottom), buf->rect.y1, round(x_intersect_top), buf->rect.y0, gl->rgba);
+            sp_guideline_drawline (buf, static_cast<gint>(round(x_intersect_bottom)), buf->rect.y1, static_cast<gint>(round(x_intersect_top)), buf->rect.y0, gl->rgba);
             return;
         }
     }
 }
 
-static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
+static void sp_guideline_update(SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
 {
     SPGuideLine *gl = SP_GUIDELINE(item);
 
@@ -176,20 +191,23 @@ static void sp_guideline_update(SPCanvasItem *item, NR::Matrix const &affine, un
         ((SPCanvasItemClass *) parent_class)->update(item, affine, flags);
     }
 
-    gl->point_on_line[Geom::X] = affine[4] +0.5;
-    gl->point_on_line[Geom::Y] = affine[5] -0.5;
+    gl->point_on_line[Geom::X] = affine[4];
+    gl->point_on_line[Geom::Y] = affine[5];
+
+    sp_ctrlpoint_set_coords(gl->origin, gl->point_on_line * affine.inverse());
+    sp_canvas_item_request_update(SP_CANVAS_ITEM (gl->origin));
 
     if (gl->is_horizontal()) {
-        sp_canvas_update_bbox (item, -1000000, gl->point_on_line[Geom::Y], 1000000, gl->point_on_line[Geom::Y] + 1);
+        sp_canvas_update_bbox (item, -1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y]), 1000000, (int) Inkscape::round(gl->point_on_line[Geom::Y] + 1));
     } else if (gl->is_vertical()) {
-        sp_canvas_update_bbox (item, gl->point_on_line[Geom::X], -1000000, gl->point_on_line[Geom::X]+1, 1000000);
+        sp_canvas_update_bbox (item, (int) Inkscape::round(gl->point_on_line[Geom::X]), -1000000, (int) Inkscape::round(gl->point_on_line[Geom::X] + 1), 1000000);
     } else {
         sp_canvas_update_bbox (item, -1000000, -1000000, 1000000, 1000000);
     }
 }
 
 // Returns 0.0 if point is on the guideline
-static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
+static double sp_guideline_point(SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
 {
     SPGuideLine *gl = SP_GUIDELINE (item);
 
@@ -200,28 +218,33 @@ static double sp_guideline_point(SPCanvasItem *item, NR::Point p, SPCanvasItem *
     *actual_item = item;
 
     Geom::Point vec(gl->normal_to_line[Geom::X], - gl->normal_to_line[Geom::Y]);
-    double distance = Geom::dot((p.to_2geom() - gl->point_on_line), vec);
+    double distance = Geom::dot((p - gl->point_on_line), vec);
     return MAX(fabs(distance)-1, 0);
 }
 
 SPCanvasItem *sp_guideline_new(SPCanvasGroup *parent, Geom::Point point_on_line, Geom::Point normal)
 {
     SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_GUIDELINE, NULL);
+    SPCanvasItem *origin = sp_canvas_item_new(parent, SP_TYPE_CTRLPOINT, NULL);
 
     SPGuideLine *gl = SP_GUIDELINE(item);
+    SPCtrlPoint *cp = SP_CTRLPOINT(origin);
+    gl->origin = cp;
 
     normal.normalize();
     gl->normal_to_line = normal;
     gl->angle = tan( -gl->normal_to_line[Geom::X] / gl->normal_to_line[Geom::Y]);
     sp_guideline_set_position(gl, point_on_line);
 
+    sp_ctrlpoint_set_coords(cp, point_on_line);
+
     return item;
 }
 
 void sp_guideline_set_position(SPGuideLine *gl, Geom::Point point_on_line)
 {
-    sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl),
-                                   NR::Matrix(NR::translate(point_on_line)));
+    sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl), Geom::Matrix(Geom::Translate(point_on_line)));
+    sp_canvas_item_affine_absolute(SP_CANVAS_ITEM (gl->origin), Geom::Matrix(Geom::Translate(point_on_line)));
 }
 
 void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
@@ -235,6 +258,7 @@ void sp_guideline_set_normal(SPGuideLine *gl, Geom::Point normal_to_line)
 void sp_guideline_set_color(SPGuideLine *gl, unsigned int rgba)
 {
     gl->rgba = rgba;
+    sp_ctrlpoint_set_color(gl->origin, rgba);
 
     sp_canvas_item_request_update(SP_CANVAS_ITEM(gl));
 }
@@ -244,6 +268,12 @@ void sp_guideline_set_sensitive(SPGuideLine *gl, int sensitive)
     gl->sensitive = sensitive;
 }
 
+void sp_guideline_delete(SPGuideLine *gl)
+{
+    //gtk_object_destroy(GTK_OBJECT(gl->origin));
+    gtk_object_destroy(GTK_OBJECT(gl));
+}
+
 //##########################################################
 // Line rendering
 #define SAFE_SETPIXEL   //undefine this when it is certain that setpixel is never called with invalid params
@@ -269,7 +299,7 @@ sp_guideline_setpixel (SPCanvasBuf *buf, gint x, gint y, guint32 rgba)
         g = NR_RGBA32_G (rgba);
         b = NR_RGBA32_B (rgba);
         a = NR_RGBA32_A (rgba);
-        guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 3;
+        guchar * p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride + (x - buf->rect.x0) * 4;
         p[0] = NR_COMPOSEN11_1111 (r, a, p[0]);
         p[1] = NR_COMPOSEN11_1111 (g, a, p[1]);
         p[2] = NR_COMPOSEN11_1111 (b, a, p[2]);
@@ -331,4 +361,4 @@ sp_guideline_drawline (SPCanvasBuf *buf, gint x0, gint y0, gint x1, gint y1, gui
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :