break down GdkEventExpose into distinct rectangles for canvas expose rather than...
[ardour.git] / libs / canvas / line.cc
index af2a0e47db19ea230acf2469ae044e6e71e8739f..09f9061c8593653b601c3d6791c7c42918f06982 100644 (file)
@@ -60,11 +60,17 @@ Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) cons
        Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y));
        Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y));
 
-       /* See Cairo FAQ on single pixel lines to understand why we add 0.5
-        */
-
-       context->move_to (p0.x + 0.5, p0.y + 0.5);
-       context->line_to (p1.x + 0.5, p1.y + 0.5);
+       if (_outline_width <= 1.0) {
+               /* See Cairo FAQ on single pixel lines to understand why we add 0.5
+                */
+               
+               const Duple half_a_pixel (0.5, 0.5);
+               p0 = p0.translate (half_a_pixel);
+               p1 = p1.translate (half_a_pixel);
+       }
+
+       context->move_to (p0.x, p0.y);
+       context->line_to (p1.x, p1.y);
        context->stroke ();
 }
 
@@ -147,3 +153,53 @@ Line::set_y1 (Coord y1)
 
        DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
 }
+
+bool
+Line::covers (Duple const & point) const
+{
+       const Duple p = canvas_to_item (point);
+       static const Distance threshold = 2.0;
+
+       /* this quick check works for vertical and horizontal lines, which are
+        * common.
+        */
+
+       if (_points[0].x == _points[1].x) {
+               /* line is vertical, just check x coordinate */
+               return fabs (_points[0].x - p.x) <= threshold;
+       }
+
+       if (_points[0].y == _points[1].y) {
+               /* line is horizontal, just check y coordinate */
+               return fabs (_points[0].y - p.y) <= threshold;
+       }
+
+       Duple at;
+       double t;
+       Duple a (_points[0]);
+       Duple b (_points[1]);
+       const Rect visible (_canvas->visible_area());
+
+       /*
+          Clamp the line endpoints to the visible area of the canvas. If we do
+          not do this, we have a line segment extending to COORD_MAX and our
+          math goes wrong.
+       */
+
+       a.x = min (a.x, visible.x1);
+       a.y = min (a.y, visible.y1);
+       b.x = min (b.x, visible.x1);
+       b.y = min (b.y, visible.y1);
+
+       double d = distance_to_segment_squared (p, a, b, t, at);
+
+       if (t < 0.0 || t > 1.0) {
+               return false;
+       }
+
+       if (d < threshold) {
+               return true;
+       }
+
+       return false;
+}