Pass an explicit graphics context to DrawAttached and DrawMark
[geda-pcb/pcjc2.git] / src / crosshair.c
blob78b8111e086f1e1ad5b599bcd5a4ee1edb1bde49
1 /*
2 * COPYRIGHT
4 * PCB, interactive printed circuit board design
5 * Copyright (C) 1994,1995,1996 Thomas Nau
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Contact addresses for paper mail and Email:
22 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
23 * Thomas.Nau@rz.uni-ulm.de
28 /* crosshair stuff
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <memory.h>
36 #include <math.h>
38 #include "global.h"
39 #include "hid_draw.h"
41 #include "crosshair.h"
42 #include "data.h"
43 #include "draw.h"
44 #include "error.h"
45 #include "line.h"
46 #include "misc.h"
47 #include "mymem.h"
48 #include "search.h"
49 #include "polygon.h"
51 #ifdef HAVE_LIBDMALLOC
52 #include <dmalloc.h>
53 #endif
55 typedef struct
57 int x, y;
58 } point;
61 static void
62 thindraw_moved_pv (hidGC gc, PinType *pv, Coord x, Coord y)
64 /* Make a copy of the pin structure, moved to the correct position */
65 PinType moved_pv = *pv;
66 moved_pv.X += x;
67 moved_pv.Y += y;
69 gui->graphics->thindraw_pcb_pv (gc, gc, &moved_pv, true, false);
72 /* ---------------------------------------------------------------------------
73 * creates a tmp polygon with coordinates converted to screen system
75 static void
76 XORPolygon (hidGC gc, PolygonType *polygon, Coord dx, Coord dy)
78 Cardinal i;
79 for (i = 0; i < polygon->PointN; i++)
81 Cardinal next = next_contour_point (polygon, i);
82 gui->graphics->draw_line (gc,
83 polygon->Points[i].X + dx,
84 polygon->Points[i].Y + dy,
85 polygon->Points[next].X + dx,
86 polygon->Points[next].Y + dy);
90 /*-----------------------------------------------------------
91 * Draws the outline of an arc
93 static void
94 XORDrawAttachedArc (hidGC gc, Coord thick)
96 ArcType arc;
97 BoxType *bx;
98 Coord wx, wy;
99 Angle sa, dir;
100 Coord wid = thick / 2;
102 wx = Crosshair.X - Crosshair.AttachedBox.Point1.X;
103 wy = Crosshair.Y - Crosshair.AttachedBox.Point1.Y;
104 if (wx == 0 && wy == 0)
105 return;
106 arc.X = Crosshair.AttachedBox.Point1.X;
107 arc.Y = Crosshair.AttachedBox.Point1.Y;
108 if (XOR (Crosshair.AttachedBox.otherway, abs (wy) > abs (wx)))
110 arc.X = Crosshair.AttachedBox.Point1.X + abs (wy) * SGNZ (wx);
111 sa = (wx >= 0) ? 0 : 180;
112 #ifdef ARC45
113 if (abs (wy) >= 2 * abs (wx))
114 dir = (SGNZ (wx) == SGNZ (wy)) ? 45 : -45;
115 else
116 #endif
117 dir = (SGNZ (wx) == SGNZ (wy)) ? 90 : -90;
119 else
121 arc.Y = Crosshair.AttachedBox.Point1.Y + abs (wx) * SGNZ (wy);
122 sa = (wy >= 0) ? -90 : 90;
123 #ifdef ARC45
124 if (abs (wx) >= 2 * abs (wy))
125 dir = (SGNZ (wx) == SGNZ (wy)) ? -45 : 45;
126 else
127 #endif
128 dir = (SGNZ (wx) == SGNZ (wy)) ? -90 : 90;
129 wy = wx;
131 wy = abs (wy);
132 arc.StartAngle = sa;
133 arc.Delta = dir;
134 arc.Width = arc.Height = wy;
135 bx = GetArcEnds (&arc);
136 /* sa = sa - 180; */
137 gui->graphics->draw_arc (gc, arc.X, arc.Y, wy + wid, wy + wid, sa, dir);
138 if (wid > pixel_slop)
140 gui->graphics->draw_arc (gc, arc.X, arc.Y, wy - wid, wy - wid, sa, dir);
141 gui->graphics->draw_arc (gc, bx->X1, bx->Y1, wid, wid, sa, -180 * SGN (dir));
142 gui->graphics->draw_arc (gc, bx->X2, bx->Y2, wid, wid, sa + dir, 180 * SGN (dir));
146 /*-----------------------------------------------------------
147 * Draws the outline of a line
149 static void
150 XORDrawAttachedLine (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2, Coord thick)
152 Coord dx, dy, ox, oy;
153 double h;
155 dx = x2 - x1;
156 dy = y2 - y1;
157 if (dx != 0 || dy != 0)
158 h = 0.5 * thick / sqrt (SQUARE (dx) + SQUARE (dy));
159 else
160 h = 0.0;
161 ox = dy * h + 0.5 * SGN (dy);
162 oy = -(dx * h + 0.5 * SGN (dx));
163 gui->graphics->draw_line (gc, x1 + ox, y1 + oy, x2 + ox, y2 + oy);
164 if (abs (ox) >= pixel_slop || abs (oy) >= pixel_slop)
166 Angle angle = atan2 (dx, dy) * 57.295779;
167 gui->graphics->draw_line (gc, x1 - ox, y1 - oy, x2 - ox, y2 - oy);
168 gui->graphics->draw_arc (gc, x1, y1, thick / 2, thick / 2, angle - 180, 180);
169 gui->graphics->draw_arc (gc, x2, y2, thick / 2, thick / 2, angle, 180);
173 /* ---------------------------------------------------------------------------
174 * draws the elements of a loaded circuit which is to be merged in
176 static void
177 XORDrawElement (hidGC gc, ElementType *Element, Coord DX, Coord DY)
179 /* if no silkscreen, draw the bounding box */
180 if (Element->ArcN == 0 && Element->LineN == 0)
182 gui->graphics->draw_line (gc,
183 DX + Element->BoundingBox.X1,
184 DY + Element->BoundingBox.Y1,
185 DX + Element->BoundingBox.X1,
186 DY + Element->BoundingBox.Y2);
187 gui->graphics->draw_line (gc,
188 DX + Element->BoundingBox.X1,
189 DY + Element->BoundingBox.Y2,
190 DX + Element->BoundingBox.X2,
191 DY + Element->BoundingBox.Y2);
192 gui->graphics->draw_line (gc,
193 DX + Element->BoundingBox.X2,
194 DY + Element->BoundingBox.Y2,
195 DX + Element->BoundingBox.X2,
196 DY + Element->BoundingBox.Y1);
197 gui->graphics->draw_line (gc,
198 DX + Element->BoundingBox.X2,
199 DY + Element->BoundingBox.Y1,
200 DX + Element->BoundingBox.X1,
201 DY + Element->BoundingBox.Y1);
203 else
205 ELEMENTLINE_LOOP (Element);
207 gui->graphics->draw_line (gc,
208 DX + line->Point1.X,
209 DY + line->Point1.Y,
210 DX + line->Point2.X,
211 DY + line->Point2.Y);
213 END_LOOP;
215 /* arc coordinates and angles have to be converted to X11 notation */
216 ARC_LOOP (Element);
218 gui->graphics->draw_arc (gc,
219 DX + arc->X,
220 DY + arc->Y,
221 arc->Width, arc->Height, arc->StartAngle, arc->Delta);
223 END_LOOP;
225 /* pin coordinates and angles have to be converted to X11 notation */
226 PIN_LOOP (Element);
228 thindraw_moved_pv (gc, pin, DX, DY);
230 END_LOOP;
232 /* pads */
233 PAD_LOOP (Element);
235 if (PCB->InvisibleObjectsOn ||
236 (TEST_FLAG (ONSOLDERFLAG, pad) != 0) == Settings.ShowBottomSide)
238 /* Make a copy of the pad structure, moved to the correct position */
239 PadType moved_pad = *pad;
240 moved_pad.Point1.X += DX; moved_pad.Point1.Y += DY;
241 moved_pad.Point2.X += DX; moved_pad.Point2.Y += DY;
243 gui->graphics->thindraw_pcb_pad (gc, &moved_pad, false, false);
246 END_LOOP;
247 /* mark */
248 gui->graphics->draw_line (gc,
249 Element->MarkX + DX - EMARK_SIZE,
250 Element->MarkY + DY,
251 Element->MarkX + DX,
252 Element->MarkY + DY - EMARK_SIZE);
253 gui->graphics->draw_line (gc,
254 Element->MarkX + DX + EMARK_SIZE,
255 Element->MarkY + DY,
256 Element->MarkX + DX,
257 Element->MarkY + DY - EMARK_SIZE);
258 gui->graphics->draw_line (gc,
259 Element->MarkX + DX - EMARK_SIZE,
260 Element->MarkY + DY,
261 Element->MarkX + DX,
262 Element->MarkY + DY + EMARK_SIZE);
263 gui->graphics->draw_line (gc,
264 Element->MarkX + DX + EMARK_SIZE,
265 Element->MarkY + DY,
266 Element->MarkX + DX,
267 Element->MarkY + DY + EMARK_SIZE);
270 /* ---------------------------------------------------------------------------
271 * draws all visible and attached objects of the pastebuffer
273 static void
274 XORDrawBuffer (hidGC gc, BufferType *Buffer)
276 Cardinal i;
277 Coord x, y;
279 /* set offset */
280 x = Crosshair.X - Buffer->X;
281 y = Crosshair.Y - Buffer->Y;
283 /* draw all visible layers */
284 for (i = 0; i < max_copper_layer + 2; i++)
285 if (PCB->Data->Layer[i].On)
287 LayerType *layer = &Buffer->Data->Layer[i];
289 LINE_LOOP (layer);
292 XORDrawAttachedLine(x +line->Point1.X,
293 y +line->Point1.Y, x +line->Point2.X,
294 y +line->Point2.Y, line->Thickness);
296 gui->graphics->draw_line (gc,
297 x + line->Point1.X, y + line->Point1.Y,
298 x + line->Point2.X, y + line->Point2.Y);
300 END_LOOP;
301 ARC_LOOP (layer);
303 gui->graphics->draw_arc (gc,
304 x + arc->X,
305 y + arc->Y,
306 arc->Width,
307 arc->Height, arc->StartAngle, arc->Delta);
309 END_LOOP;
310 TEXT_LOOP (layer);
312 BoxType *box = &text->BoundingBox;
313 gui->graphics->draw_rect (gc,
314 x + box->X1, y + box->Y1, x + box->X2, y + box->Y2);
316 END_LOOP;
317 /* the tmp polygon has n+1 points because the first
318 * and the last one are set to the same coordinates
320 POLYGON_LOOP (layer);
322 XORPolygon (gc, polygon, x, y);
324 END_LOOP;
327 /* draw elements if visible */
328 if (PCB->PinOn && PCB->ElementOn)
329 ELEMENT_LOOP (Buffer->Data);
331 if (FRONT (element) || PCB->InvisibleObjectsOn)
332 XORDrawElement (gc, element, x, y);
334 END_LOOP;
336 /* and the vias */
337 if (PCB->ViaOn)
338 VIA_LOOP (Buffer->Data);
340 thindraw_moved_pv (gc, via, x, y);
342 END_LOOP;
345 /* ---------------------------------------------------------------------------
346 * draws the rubberband to insert points into polygons/lines/...
348 static void
349 XORDrawInsertPointObject (hidGC gc)
351 LineType *line = (LineType *) Crosshair.AttachedObject.Ptr2;
352 PointType *point = (PointType *) Crosshair.AttachedObject.Ptr3;
354 if (Crosshair.AttachedObject.Type != NO_TYPE)
356 gui->graphics->draw_line (gc, point->X, point->Y, line->Point1.X, line->Point1.Y);
357 gui->graphics->draw_line (gc, point->X, point->Y, line->Point2.X, line->Point2.Y);
361 /* ---------------------------------------------------------------------------
362 * draws the attached object while in MOVE_MODE or COPY_MODE
364 static void
365 XORDrawMoveOrCopyObject (hidGC gc)
367 RubberbandType *ptr;
368 Cardinal i;
369 Coord dx = Crosshair.X - Crosshair.AttachedObject.X,
370 dy = Crosshair.Y - Crosshair.AttachedObject.Y;
372 switch (Crosshair.AttachedObject.Type)
374 case VIA_TYPE:
376 PinType *via = (PinType *) Crosshair.AttachedObject.Ptr1;
377 thindraw_moved_pv (gc, via, dx, dy);
378 break;
381 case LINE_TYPE:
383 LineType *line = (LineType *) Crosshair.AttachedObject.Ptr2;
385 XORDrawAttachedLine (gc, line->Point1.X + dx, line->Point1.Y + dy,
386 line->Point2.X + dx, line->Point2.Y + dy,
387 line->Thickness);
388 break;
391 case ARC_TYPE:
393 ArcType *Arc = (ArcType *) Crosshair.AttachedObject.Ptr2;
395 gui->graphics->draw_arc (gc,
396 Arc->X + dx,
397 Arc->Y + dy,
398 Arc->Width, Arc->Height, Arc->StartAngle, Arc->Delta);
399 break;
402 case POLYGON_TYPE:
404 PolygonType *polygon =
405 (PolygonType *) Crosshair.AttachedObject.Ptr2;
407 /* the tmp polygon has n+1 points because the first
408 * and the last one are set to the same coordinates
410 XORPolygon (gc, polygon, dx, dy);
411 break;
414 case LINEPOINT_TYPE:
416 LineType *line;
417 PointType *point;
419 line = (LineType *) Crosshair.AttachedObject.Ptr2;
420 point = (PointType *) Crosshair.AttachedObject.Ptr3;
421 if (point == &line->Point1)
422 XORDrawAttachedLine (gc, point->X + dx, point->Y + dy,
423 line->Point2.X, line->Point2.Y, line->Thickness);
424 else
425 XORDrawAttachedLine (gc, point->X + dx, point->Y + dy,
426 line->Point1.X, line->Point1.Y, line->Thickness);
427 break;
430 case POLYGONPOINT_TYPE:
432 PolygonType *polygon;
433 PointType *point;
434 Cardinal point_idx, prev, next;
436 polygon = (PolygonType *) Crosshair.AttachedObject.Ptr2;
437 point = (PointType *) Crosshair.AttachedObject.Ptr3;
438 point_idx = polygon_point_idx (polygon, point);
440 /* get previous and following point */
441 prev = prev_contour_point (polygon, point_idx);
442 next = next_contour_point (polygon, point_idx);
444 /* draw the two segments */
445 gui->graphics->draw_line (gc,
446 polygon->Points[prev].X, polygon->Points[prev].Y,
447 point->X + dx, point->Y + dy);
448 gui->graphics->draw_line (gc,
449 point->X + dx, point->Y + dy,
450 polygon->Points[next].X, polygon->Points[next].Y);
451 break;
454 case ELEMENTNAME_TYPE:
456 /* locate the element "mark" and draw an association line from crosshair to it */
457 ElementType *element =
458 (ElementType *) Crosshair.AttachedObject.Ptr1;
460 gui->graphics->draw_line (gc,
461 element->MarkX,
462 element->MarkY, Crosshair.X, Crosshair.Y);
463 /* fall through to move the text as a box outline */
465 case TEXT_TYPE:
467 TextType *text = (TextType *) Crosshair.AttachedObject.Ptr2;
468 BoxType *box = &text->BoundingBox;
469 gui->graphics->draw_rect (gc,
470 box->X1 + dx,
471 box->Y1 + dy, box->X2 + dx, box->Y2 + dy);
472 break;
475 /* pin/pad movements result in moving an element */
476 case PAD_TYPE:
477 case PIN_TYPE:
478 case ELEMENT_TYPE:
479 XORDrawElement (gc, (ElementType *) Crosshair.AttachedObject.Ptr2, dx, dy);
480 break;
483 /* draw the attached rubberband lines too */
484 i = Crosshair.AttachedObject.RubberbandN;
485 ptr = Crosshair.AttachedObject.Rubberband;
486 while (i)
488 PointType *point1, *point2;
490 if (TEST_FLAG (VIAFLAG, ptr->Line))
492 /* this is a rat going to a polygon. do not draw for rubberband */;
494 else if (TEST_FLAG (RUBBERENDFLAG, ptr->Line))
496 /* 'point1' is always the fix-point */
497 if (ptr->MovedPoint == &ptr->Line->Point1)
499 point1 = &ptr->Line->Point2;
500 point2 = &ptr->Line->Point1;
502 else
504 point1 = &ptr->Line->Point1;
505 point2 = &ptr->Line->Point2;
507 XORDrawAttachedLine (gc, point1->X, point1->Y,
508 point2->X + dx, point2->Y + dy,
509 ptr->Line->Thickness);
511 else if (ptr->MovedPoint == &ptr->Line->Point1)
512 XORDrawAttachedLine (gc,
513 ptr->Line->Point1.X + dx,
514 ptr->Line->Point1.Y + dy,
515 ptr->Line->Point2.X + dx,
516 ptr->Line->Point2.Y + dy, ptr->Line->Thickness);
518 ptr++;
519 i--;
523 /* ---------------------------------------------------------------------------
524 * draws additional stuff that follows the crosshair
526 void
527 DrawAttached (hidGC gc)
529 gui->graphics->set_color (gc, Settings.CrosshairColor);
530 gui->graphics->set_draw_xor (gc, 1);
531 gui->graphics->set_line_cap (gc, Trace_Cap);
532 gui->graphics->set_line_width (gc, 1);
534 switch (Settings.Mode)
536 case VIA_MODE:
538 /* Make a dummy via structure to draw from */
539 PinType via;
540 via.X = Crosshair.X;
541 via.Y = Crosshair.Y;
542 via.Thickness = Settings.ViaThickness;
543 via.Clearance = 2 * Settings.Keepaway;
544 via.DrillingHole = Settings.ViaDrillingHole;
545 via.Mask = 0;
546 via.Flags = NoFlags ();
548 gui->graphics->thindraw_pcb_pv (gc, gc, &via, true, false);
550 if (TEST_FLAG (SHOWDRCFLAG, PCB))
552 Coord mask_r = Settings.ViaThickness / 2 + PCB->Bloat;
553 gui->graphics->set_color (gc, Settings.CrossColor);
554 gui->graphics->set_line_cap (gc, Round_Cap);
555 gui->graphics->set_line_width (gc, 0);
556 gui->graphics->draw_arc (gc, via.X, via.Y, mask_r, mask_r, 0, 360);
557 gui->graphics->set_color (gc, Settings.CrosshairColor);
559 break;
562 /* the attached line is used by both LINEMODE, POLYGON_MODE and POLYGONHOLE_MODE*/
563 case POLYGON_MODE:
564 case POLYGONHOLE_MODE:
565 /* draw only if starting point is set */
566 if (Crosshair.AttachedLine.State != STATE_FIRST)
567 gui->graphics->draw_line (gc,
568 Crosshair.AttachedLine.Point1.X,
569 Crosshair.AttachedLine.Point1.Y,
570 Crosshair.AttachedLine.Point2.X,
571 Crosshair.AttachedLine.Point2.Y);
573 /* draw attached polygon only if in POLYGON_MODE or POLYGONHOLE_MODE */
574 if (Crosshair.AttachedPolygon.PointN > 1)
576 XORPolygon (gc, &Crosshair.AttachedPolygon, 0, 0);
578 break;
580 case ARC_MODE:
581 if (Crosshair.AttachedBox.State != STATE_FIRST)
583 XORDrawAttachedArc (gc, Settings.LineThickness);
584 if (TEST_FLAG (SHOWDRCFLAG, PCB))
586 gui->graphics->set_color (gc, Settings.CrossColor);
587 XORDrawAttachedArc (gc, Settings.LineThickness + 2 * (PCB->Bloat + 1));
588 gui->graphics->set_color (gc, Settings.CrosshairColor);
592 break;
594 case LINE_MODE:
595 /* draw only if starting point exists and the line has length */
596 if (Crosshair.AttachedLine.State != STATE_FIRST &&
597 Crosshair.AttachedLine.draw)
599 XORDrawAttachedLine (gc, Crosshair.AttachedLine.Point1.X,
600 Crosshair.AttachedLine.Point1.Y,
601 Crosshair.AttachedLine.Point2.X,
602 Crosshair.AttachedLine.Point2.Y,
603 PCB->RatDraw ? 10 : Settings.LineThickness);
604 /* draw two lines ? */
605 if (PCB->Clipping)
606 XORDrawAttachedLine (gc, Crosshair.AttachedLine.Point2.X,
607 Crosshair.AttachedLine.Point2.Y,
608 Crosshair.X, Crosshair.Y,
609 PCB->RatDraw ? 10 : Settings.LineThickness);
610 if (TEST_FLAG (SHOWDRCFLAG, PCB))
612 gui->graphics->set_color (gc, Settings.CrossColor);
613 XORDrawAttachedLine (gc, Crosshair.AttachedLine.Point1.X,
614 Crosshair.AttachedLine.Point1.Y,
615 Crosshair.AttachedLine.Point2.X,
616 Crosshair.AttachedLine.Point2.Y,
617 PCB->RatDraw ? 10 : Settings.LineThickness
618 + 2 * (PCB->Bloat + 1));
619 if (PCB->Clipping)
620 XORDrawAttachedLine (gc, Crosshair.AttachedLine.Point2.X,
621 Crosshair.AttachedLine.Point2.Y,
622 Crosshair.X, Crosshair.Y,
623 PCB->RatDraw ? 10 : Settings.
624 LineThickness + 2 * (PCB->Bloat + 1));
625 gui->graphics->set_color (gc, Settings.CrosshairColor);
628 break;
630 case PASTEBUFFER_MODE:
631 XORDrawBuffer (gc, PASTEBUFFER);
632 break;
634 case COPY_MODE:
635 case MOVE_MODE:
636 XORDrawMoveOrCopyObject (gc);
637 break;
639 case INSERTPOINT_MODE:
640 XORDrawInsertPointObject (gc);
641 break;
644 /* an attached box does not depend on a special mode */
645 if (Crosshair.AttachedBox.State == STATE_SECOND ||
646 Crosshair.AttachedBox.State == STATE_THIRD)
648 Coord x1, y1, x2, y2;
650 x1 = Crosshair.AttachedBox.Point1.X;
651 y1 = Crosshair.AttachedBox.Point1.Y;
652 x2 = Crosshair.AttachedBox.Point2.X;
653 y2 = Crosshair.AttachedBox.Point2.Y;
654 gui->graphics->draw_rect (gc, x1, y1, x2, y2);
659 /* --------------------------------------------------------------------------
660 * draw the marker position
662 void
663 DrawMark (hidGC gc)
665 gui->graphics->set_color (gc, Settings.CrosshairColor);
666 gui->graphics->set_draw_xor (gc, 1);
667 gui->graphics->set_line_cap (gc, Trace_Cap);
668 gui->graphics->set_line_width (gc, 1);
670 /* Mark is not drawn when it is not set */
671 if (!Marked.status)
672 return;
674 gui->graphics->draw_line (gc,
675 Marked.X - MARK_SIZE,
676 Marked.Y - MARK_SIZE,
677 Marked.X + MARK_SIZE, Marked.Y + MARK_SIZE);
678 gui->graphics->draw_line (gc,
679 Marked.X + MARK_SIZE,
680 Marked.Y - MARK_SIZE,
681 Marked.X - MARK_SIZE, Marked.Y + MARK_SIZE);
684 /* ---------------------------------------------------------------------------
685 * Returns the nearest grid-point to the given Coord
687 Coord
688 GridFit (Coord x, Coord grid_spacing, Coord grid_offset)
690 x -= grid_offset;
691 x = grid_spacing * round ((double) x / grid_spacing);
692 x += grid_offset;
693 return x;
697 /* ---------------------------------------------------------------------------
698 * notify the GUI that data relating to the crosshair is being changed.
700 * The argument passed is false to notify "changes are about to happen",
701 * and true to notify "changes have finished".
703 * Each call with a 'false' parameter must be matched with a following one
704 * with a 'true' parameter. Unmatched 'true' calls are currently not permitted,
705 * but might be allowed in the future.
707 * GUIs should not complain if they receive extra calls with 'true' as parameter.
708 * They should initiate a redraw of the crosshair attached objects - which may
709 * (if necessary) mean repainting the whole screen if the GUI hasn't tracked the
710 * location of existing attached drawing.
712 void
713 notify_crosshair_change (bool changes_complete)
715 if (gui->notify_crosshair_change)
716 gui->notify_crosshair_change (changes_complete);
720 /* ---------------------------------------------------------------------------
721 * notify the GUI that data relating to the mark is being changed.
723 * The argument passed is false to notify "changes are about to happen",
724 * and true to notify "changes have finished".
726 * Each call with a 'false' parameter must be matched with a following one
727 * with a 'true' parameter. Unmatched 'true' calls are currently not permitted,
728 * but might be allowed in the future.
730 * GUIs should not complain if they receive extra calls with 'true' as parameter.
731 * They should initiate a redraw of the mark - which may (if necessary) mean
732 * repainting the whole screen if the GUI hasn't tracked the mark's location.
734 void
735 notify_mark_change (bool changes_complete)
737 if (gui->notify_mark_change)
738 gui->notify_mark_change (changes_complete);
742 /* ---------------------------------------------------------------------------
743 * Convenience for plugins using the old {Hide,Restore}Crosshair API.
744 * This links up to notify the GUI of the expected changes using the new APIs.
746 * Use of this old API is deprecated, as the names don't necessarily reflect
747 * what all GUIs may do in response to the notifications. Keeping these APIs
748 * is aimed at easing transition to the newer API, they will emit a harmless
749 * warning at the time of their first use.
752 void
753 HideCrosshair (void)
755 static bool warned_old_api = false;
756 if (!warned_old_api)
758 Message (_("WARNING: A plugin is using the deprecated API HideCrosshair().\n"
759 " This API may be removed in a future release of PCB.\n"));
760 warned_old_api = true;
763 notify_crosshair_change (false);
764 notify_mark_change (false);
767 void
768 RestoreCrosshair (void)
770 static bool warned_old_api = false;
771 if (!warned_old_api)
773 Message (_("WARNING: A plugin is using the deprecated API RestoreCrosshair().\n"
774 " This API may be removed in a future release of PCB.\n"));
775 warned_old_api = true;
778 notify_crosshair_change (true);
779 notify_mark_change (true);
782 /* ---------------------------------------------------------------------------
783 * Returns the square of the given number
785 static double
786 square (double x)
788 return x * x;
791 static double
792 crosshair_sq_dist (CrosshairType *crosshair, Coord x, Coord y)
794 return square (x - crosshair->X) + square (y - crosshair->Y);
797 struct snap_data {
798 CrosshairType *crosshair;
799 double nearest_sq_dist;
800 bool nearest_is_grid;
801 Coord x, y;
804 /* Snap to a given location if it is the closest thing we found so far.
805 * If "prefer_to_grid" is set, the passed location will take preference
806 * over a closer grid points we already snapped to UNLESS the user is
807 * pressing the SHIFT key. If the SHIFT key is pressed, the closest object
808 * (including grid points), is always preferred.
810 static void
811 check_snap_object (struct snap_data *snap_data, Coord x, Coord y,
812 bool prefer_to_grid)
814 double sq_dist;
816 sq_dist = crosshair_sq_dist (snap_data->crosshair, x, y);
817 if (sq_dist <= snap_data->nearest_sq_dist ||
818 (prefer_to_grid && snap_data->nearest_is_grid && !gui->shift_is_pressed()))
820 snap_data->x = x;
821 snap_data->y = y;
822 snap_data->nearest_sq_dist = sq_dist;
823 snap_data->nearest_is_grid = false;
827 static void
828 check_snap_offgrid_line (struct snap_data *snap_data,
829 Coord nearest_grid_x,
830 Coord nearest_grid_y)
832 void *ptr1, *ptr2, *ptr3;
833 int ans;
834 LineType *line;
835 Coord try_x, try_y;
836 double dx, dy;
837 double dist;
839 if (!TEST_FLAG (SNAPPINFLAG, PCB))
840 return;
842 /* Code to snap at some sensible point along a line */
843 /* Pick the nearest grid-point in the x or y direction
844 * to align with, then adjust until we hit the line
846 ans = SearchObjectByLocation (LINE_TYPE, &ptr1, &ptr2, &ptr3,
847 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
850 if (ans == NO_TYPE)
851 return;
853 line = (LineType *)ptr2;
855 /* Allow snapping to off-grid lines when drawing new lines (on
856 * the same layer), and when moving a line end-point
857 * (but don't snap to the same line)
859 if ((Settings.Mode != LINE_MODE || CURRENT != ptr1) &&
860 (Settings.Mode != MOVE_MODE ||
861 Crosshair.AttachedObject.Ptr1 != ptr1 ||
862 Crosshair.AttachedObject.Type != LINEPOINT_TYPE ||
863 Crosshair.AttachedObject.Ptr2 == line))
864 return;
866 dx = line->Point2.X - line->Point1.X;
867 dy = line->Point2.Y - line->Point1.Y;
869 /* Try snapping along the X axis */
870 if (dy != 0.)
872 /* Move in the X direction until we hit the line */
873 try_x = (nearest_grid_y - line->Point1.Y) / dy * dx + line->Point1.X;
874 try_y = nearest_grid_y;
875 check_snap_object (snap_data, try_x, try_y, true);
878 /* Try snapping along the Y axis */
879 if (dx != 0.)
881 try_x = nearest_grid_x;
882 try_y = (nearest_grid_x - line->Point1.X) / dx * dy + line->Point1.Y;
883 check_snap_object (snap_data, try_x, try_y, true);
886 if (dx != dy) /* If line not parallel with dX = dY direction.. */
888 /* Try snapping diagonally towards the line in the dX = dY direction */
890 if (dy == 0)
891 dist = line->Point1.Y - nearest_grid_y;
892 else
893 dist = ((line->Point1.X - nearest_grid_x) -
894 (line->Point1.Y - nearest_grid_y) * dx / dy) / (1 - dx / dy);
896 try_x = nearest_grid_x + dist;
897 try_y = nearest_grid_y + dist;
899 check_snap_object (snap_data, try_x, try_y, true);
902 if (dx != -dy) /* If line not parallel with dX = -dY direction.. */
904 /* Try snapping diagonally towards the line in the dX = -dY direction */
906 if (dy == 0)
907 dist = nearest_grid_y - line->Point1.Y;
908 else
909 dist = ((line->Point1.X - nearest_grid_x) -
910 (line->Point1.Y - nearest_grid_y) * dx / dy) / (1 + dx / dy);
912 try_x = nearest_grid_x + dist;
913 try_y = nearest_grid_y - dist;
915 check_snap_object (snap_data, try_x, try_y, true);
919 /* ---------------------------------------------------------------------------
920 * recalculates the passed coordinates to fit the current grid setting
922 void
923 FitCrosshairIntoGrid (Coord X, Coord Y)
925 Coord nearest_grid_x, nearest_grid_y;
926 void *ptr1, *ptr2, *ptr3;
927 struct snap_data snap_data;
928 int ans;
930 Crosshair.X = CLAMP (X, Crosshair.MinX, Crosshair.MaxX);
931 Crosshair.Y = CLAMP (Y, Crosshair.MinY, Crosshair.MaxY);
933 if (PCB->RatDraw)
935 nearest_grid_x = -MIL_TO_COORD (6);
936 nearest_grid_y = -MIL_TO_COORD (6);
938 else
940 nearest_grid_x = GridFit (Crosshair.X, PCB->Grid, PCB->GridOffsetX);
941 nearest_grid_y = GridFit (Crosshair.Y, PCB->Grid, PCB->GridOffsetY);
943 if (Marked.status && TEST_FLAG (ORTHOMOVEFLAG, PCB))
945 Coord dx = Crosshair.X - Marked.X;
946 Coord dy = Crosshair.Y - Marked.Y;
947 if (ABS (dx) > ABS (dy))
948 nearest_grid_y = Marked.Y;
949 else
950 nearest_grid_x = Marked.X;
955 snap_data.crosshair = &Crosshair;
956 snap_data.nearest_sq_dist =
957 crosshair_sq_dist (&Crosshair, nearest_grid_x, nearest_grid_y);
958 snap_data.nearest_is_grid = true;
959 snap_data.x = nearest_grid_x;
960 snap_data.y = nearest_grid_y;
962 ans = NO_TYPE;
963 if (!PCB->RatDraw)
964 ans = SearchObjectByLocation (ELEMENT_TYPE, &ptr1, &ptr2, &ptr3,
965 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
967 if (ans & ELEMENT_TYPE)
969 ElementType *el = (ElementType *) ptr1;
970 check_snap_object (&snap_data, el->MarkX, el->MarkY, false);
973 ans = NO_TYPE;
974 if (PCB->RatDraw || TEST_FLAG (SNAPPINFLAG, PCB))
975 ans = SearchObjectByLocation (PAD_TYPE, &ptr1, &ptr2, &ptr3,
976 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
978 /* Avoid self-snapping when moving */
979 if (ans != NO_TYPE &&
980 Settings.Mode == MOVE_MODE &&
981 Crosshair.AttachedObject.Type == ELEMENT_TYPE &&
982 ptr1 == Crosshair.AttachedObject.Ptr1)
983 ans = NO_TYPE;
985 if (ans != NO_TYPE &&
986 ( Settings.Mode == LINE_MODE ||
987 (Settings.Mode == MOVE_MODE &&
988 Crosshair.AttachedObject.Type == LINEPOINT_TYPE)))
990 PadType *pad = (PadType *) ptr2;
991 LayerType *desired_layer;
992 Cardinal desired_group;
993 Cardinal bottom_group, top_group;
994 int found_our_layer = false;
996 desired_layer = CURRENT;
997 if (Settings.Mode == MOVE_MODE &&
998 Crosshair.AttachedObject.Type == LINEPOINT_TYPE)
1000 desired_layer = (LayerType *)Crosshair.AttachedObject.Ptr1;
1003 /* find layer groups of the top and bottom sides */
1004 top_group = GetLayerGroupNumberBySide (TOP_SIDE);
1005 bottom_group = GetLayerGroupNumberBySide (BOTTOM_SIDE);
1006 desired_group = TEST_FLAG (ONSOLDERFLAG, pad) ? bottom_group : top_group;
1008 GROUP_LOOP (PCB->Data, desired_group);
1010 if (layer == desired_layer)
1012 found_our_layer = true;
1013 break;
1016 END_LOOP;
1018 if (found_our_layer == false)
1019 ans = NO_TYPE;
1022 if (ans != NO_TYPE)
1024 PadType *pad = (PadType *)ptr2;
1025 check_snap_object (&snap_data, pad->Point1.X + (pad->Point2.X - pad->Point1.X) / 2,
1026 pad->Point1.Y + (pad->Point2.Y - pad->Point1.Y) / 2,
1027 true);
1030 ans = NO_TYPE;
1031 if (PCB->RatDraw || TEST_FLAG (SNAPPINFLAG, PCB))
1032 ans = SearchObjectByLocation (PIN_TYPE, &ptr1, &ptr2, &ptr3,
1033 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
1035 /* Avoid self-snapping when moving */
1036 if (ans != NO_TYPE &&
1037 Settings.Mode == MOVE_MODE &&
1038 Crosshair.AttachedObject.Type == ELEMENT_TYPE &&
1039 ptr1 == Crosshair.AttachedObject.Ptr1)
1040 ans = NO_TYPE;
1042 if (ans != NO_TYPE)
1044 PinType *pin = (PinType *)ptr2;
1045 check_snap_object (&snap_data, pin->X, pin->Y, true);
1048 ans = NO_TYPE;
1049 if (TEST_FLAG (SNAPPINFLAG, PCB))
1050 ans = SearchObjectByLocation (VIA_TYPE, &ptr1, &ptr2, &ptr3,
1051 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
1053 /* Avoid snapping vias to any other vias */
1054 if (Settings.Mode == MOVE_MODE &&
1055 Crosshair.AttachedObject.Type == VIA_TYPE &&
1056 (ans & PIN_TYPES))
1057 ans = NO_TYPE;
1059 if (ans != NO_TYPE)
1061 PinType *pin = (PinType *)ptr2;
1062 check_snap_object (&snap_data, pin->X, pin->Y, true);
1065 ans = NO_TYPE;
1066 if (TEST_FLAG (SNAPPINFLAG, PCB))
1067 ans = SearchObjectByLocation (LINEPOINT_TYPE | ARCPOINT_TYPE,
1068 &ptr1, &ptr2, &ptr3,
1069 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
1071 if (ans != NO_TYPE)
1073 PointType *pnt = (PointType *)ptr3;
1074 check_snap_object (&snap_data, pnt->X, pnt->Y, true);
1077 check_snap_offgrid_line (&snap_data, nearest_grid_x, nearest_grid_y);
1079 ans = NO_TYPE;
1080 if (TEST_FLAG (SNAPPINFLAG, PCB))
1081 ans = SearchObjectByLocation (POLYGONPOINT_TYPE, &ptr1, &ptr2, &ptr3,
1082 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
1084 if (ans != NO_TYPE)
1086 PointType *pnt = (PointType *)ptr3;
1087 check_snap_object (&snap_data, pnt->X, pnt->Y, true);
1090 if (snap_data.x >= 0 && snap_data.y >= 0)
1092 Crosshair.X = snap_data.x;
1093 Crosshair.Y = snap_data.y;
1096 if (Settings.Mode == ARROW_MODE)
1098 ans = SearchObjectByLocation (LINEPOINT_TYPE | ARCPOINT_TYPE,
1099 &ptr1, &ptr2, &ptr3,
1100 Crosshair.X, Crosshair.Y, PCB->Grid / 2);
1101 if (ans == NO_TYPE)
1102 hid_action("PointCursor");
1103 else if (!TEST_FLAG(SELECTEDFLAG, (LineType *)ptr2))
1104 hid_actionl("PointCursor","True", NULL);
1107 if (Settings.Mode == LINE_MODE
1108 && Crosshair.AttachedLine.State != STATE_FIRST
1109 && TEST_FLAG (AUTODRCFLAG, PCB))
1110 EnforceLineDRC ();
1112 gui->set_crosshair (Crosshair.X, Crosshair.Y, HID_SC_DO_NOTHING);
1115 /* ---------------------------------------------------------------------------
1116 * move crosshair absolute
1117 * return true if the crosshair was moved from its existing position
1119 bool
1120 MoveCrosshairAbsolute (Coord X, Coord Y)
1122 Coord old_x = Crosshair.X;
1123 Coord old_y = Crosshair.Y;
1125 FitCrosshairIntoGrid (X, Y);
1127 if (Crosshair.X != old_x || Crosshair.Y != old_y)
1129 Coord new_x = Crosshair.X;
1130 Coord new_y = Crosshair.Y;
1132 /* back up to old position to notify the GUI
1133 * (which might want to erase the old crosshair) */
1134 Crosshair.X = old_x;
1135 Crosshair.Y = old_y;
1136 notify_crosshair_change (false); /* Our caller notifies when it has done */
1138 /* now move forward again */
1139 Crosshair.X = new_x;
1140 Crosshair.Y = new_y;
1141 return true;
1143 return false;
1146 /* ---------------------------------------------------------------------------
1147 * sets the valid range for the crosshair cursor
1149 void
1150 SetCrosshairRange (Coord MinX, Coord MinY, Coord MaxX, Coord MaxY)
1152 Crosshair.MinX = MAX (0, MinX);
1153 Crosshair.MinY = MAX (0, MinY);
1154 Crosshair.MaxX = MIN (PCB->MaxWidth, MaxX);
1155 Crosshair.MaxY = MIN (PCB->MaxHeight, MaxY);
1157 /* force update of position */
1158 FitCrosshairIntoGrid (Crosshair.X, Crosshair.Y);
1161 /* ---------------------------------------------------------------------------
1162 * initializes crosshair stuff
1163 * clears the struct, allocates to graphical contexts
1165 void
1166 InitCrosshair (void)
1168 /* set initial shape */
1169 Crosshair.shape = Basic_Crosshair_Shape;
1171 /* set default limits */
1172 Crosshair.MinX = Crosshair.MinY = 0;
1173 Crosshair.MaxX = PCB->MaxWidth;
1174 Crosshair.MaxY = PCB->MaxHeight;
1176 /* clear the mark */
1177 Marked.status = false;
1180 /* ---------------------------------------------------------------------------
1181 * exits crosshair routines, release GCs
1183 void
1184 DestroyCrosshair (void)
1186 FreePolygonMemory (&Crosshair.AttachedPolygon);