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
41 #include "crosshair.h"
51 #ifdef HAVE_LIBDMALLOC
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
;
69 gui
->graphics
->thindraw_pcb_pv (gc
, gc
, &moved_pv
, true, false);
72 /* ---------------------------------------------------------------------------
73 * creates a tmp polygon with coordinates converted to screen system
76 XORPolygon (hidGC gc
, PolygonType
*polygon
, Coord dx
, Coord dy
)
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
94 XORDrawAttachedArc (hidGC gc
, Coord thick
)
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)
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;
113 if (abs (wy
) >= 2 * abs (wx
))
114 dir
= (SGNZ (wx
) == SGNZ (wy
)) ? 45 : -45;
117 dir
= (SGNZ (wx
) == SGNZ (wy
)) ? 90 : -90;
121 arc
.Y
= Crosshair
.AttachedBox
.Point1
.Y
+ abs (wx
) * SGNZ (wy
);
122 sa
= (wy
>= 0) ? -90 : 90;
124 if (abs (wx
) >= 2 * abs (wy
))
125 dir
= (SGNZ (wx
) == SGNZ (wy
)) ? -45 : 45;
128 dir
= (SGNZ (wx
) == SGNZ (wy
)) ? -90 : 90;
134 arc
.Width
= arc
.Height
= wy
;
135 bx
= GetArcEnds (&arc
);
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
150 XORDrawAttachedLine (hidGC gc
, Coord x1
, Coord y1
, Coord x2
, Coord y2
, Coord thick
)
152 Coord dx
, dy
, ox
, oy
;
157 if (dx
!= 0 || dy
!= 0)
158 h
= 0.5 * thick
/ sqrt (SQUARE (dx
) + SQUARE (dy
));
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
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
);
205 ELEMENTLINE_LOOP (Element
);
207 gui
->graphics
->draw_line (gc
,
211 DY
+ line
->Point2
.Y
);
215 /* arc coordinates and angles have to be converted to X11 notation */
218 gui
->graphics
->draw_arc (gc
,
221 arc
->Width
, arc
->Height
, arc
->StartAngle
, arc
->Delta
);
225 /* pin coordinates and angles have to be converted to X11 notation */
228 thindraw_moved_pv (gc
, pin
, DX
, DY
);
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);
248 gui
->graphics
->draw_line (gc
,
249 Element
->MarkX
+ DX
- EMARK_SIZE
,
252 Element
->MarkY
+ DY
- EMARK_SIZE
);
253 gui
->graphics
->draw_line (gc
,
254 Element
->MarkX
+ DX
+ EMARK_SIZE
,
257 Element
->MarkY
+ DY
- EMARK_SIZE
);
258 gui
->graphics
->draw_line (gc
,
259 Element
->MarkX
+ DX
- EMARK_SIZE
,
262 Element
->MarkY
+ DY
+ EMARK_SIZE
);
263 gui
->graphics
->draw_line (gc
,
264 Element
->MarkX
+ DX
+ EMARK_SIZE
,
267 Element
->MarkY
+ DY
+ EMARK_SIZE
);
270 /* ---------------------------------------------------------------------------
271 * draws all visible and attached objects of the pastebuffer
274 XORDrawBuffer (hidGC gc
, BufferType
*Buffer
)
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
];
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
);
303 gui
->graphics
->draw_arc (gc
,
307 arc
->Height
, arc
->StartAngle
, arc
->Delta
);
312 BoxType
*box
= &text
->BoundingBox
;
313 gui
->graphics
->draw_rect (gc
,
314 x
+ box
->X1
, y
+ box
->Y1
, x
+ box
->X2
, y
+ box
->Y2
);
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
);
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
);
338 VIA_LOOP (Buffer
->Data
);
340 thindraw_moved_pv (gc
, via
, x
, y
);
345 /* ---------------------------------------------------------------------------
346 * draws the rubberband to insert points into polygons/lines/...
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
365 XORDrawMoveOrCopyObject (hidGC gc
)
369 Coord dx
= Crosshair
.X
- Crosshair
.AttachedObject
.X
,
370 dy
= Crosshair
.Y
- Crosshair
.AttachedObject
.Y
;
372 switch (Crosshair
.AttachedObject
.Type
)
376 PinType
*via
= (PinType
*) Crosshair
.AttachedObject
.Ptr1
;
377 thindraw_moved_pv (gc
, via
, dx
, dy
);
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
,
393 ArcType
*Arc
= (ArcType
*) Crosshair
.AttachedObject
.Ptr2
;
395 gui
->graphics
->draw_arc (gc
,
398 Arc
->Width
, Arc
->Height
, Arc
->StartAngle
, Arc
->Delta
);
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
);
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
);
425 XORDrawAttachedLine (gc
, point
->X
+ dx
, point
->Y
+ dy
,
426 line
->Point1
.X
, line
->Point1
.Y
, line
->Thickness
);
430 case POLYGONPOINT_TYPE
:
432 PolygonType
*polygon
;
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
);
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
,
462 element
->MarkY
, Crosshair
.X
, Crosshair
.Y
);
463 /* fall through to move the text as a box outline */
467 TextType
*text
= (TextType
*) Crosshair
.AttachedObject
.Ptr2
;
468 BoxType
*box
= &text
->BoundingBox
;
469 gui
->graphics
->draw_rect (gc
,
471 box
->Y1
+ dy
, box
->X2
+ dx
, box
->Y2
+ dy
);
475 /* pin/pad movements result in moving an element */
479 XORDrawElement (gc
, (ElementType
*) Crosshair
.AttachedObject
.Ptr2
, dx
, dy
);
483 /* draw the attached rubberband lines too */
484 i
= Crosshair
.AttachedObject
.RubberbandN
;
485 ptr
= Crosshair
.AttachedObject
.Rubberband
;
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
;
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
);
523 /* ---------------------------------------------------------------------------
524 * draws additional stuff that follows the crosshair
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
)
538 /* Make a dummy via structure to draw from */
542 via
.Thickness
= Settings
.ViaThickness
;
543 via
.Clearance
= 2 * Settings
.Keepaway
;
544 via
.DrillingHole
= Settings
.ViaDrillingHole
;
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
);
562 /* the attached line is used by both LINEMODE, POLYGON_MODE and POLYGONHOLE_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);
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
);
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 ? */
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));
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
);
630 case PASTEBUFFER_MODE
:
631 XORDrawBuffer (gc
, PASTEBUFFER
);
636 XORDrawMoveOrCopyObject (gc
);
639 case INSERTPOINT_MODE
:
640 XORDrawInsertPointObject (gc
);
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
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 */
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
688 GridFit (Coord x
, Coord grid_spacing
, Coord grid_offset
)
691 x
= grid_spacing
* round ((double) x
/ grid_spacing
);
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.
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.
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.
755 static bool warned_old_api
= false;
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);
768 RestoreCrosshair (void)
770 static bool warned_old_api
= false;
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
792 crosshair_sq_dist (CrosshairType
*crosshair
, Coord x
, Coord y
)
794 return square (x
- crosshair
->X
) + square (y
- crosshair
->Y
);
798 CrosshairType
*crosshair
;
799 double nearest_sq_dist
;
800 bool nearest_is_grid
;
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.
811 check_snap_object (struct snap_data
*snap_data
, Coord x
, Coord y
,
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()))
822 snap_data
->nearest_sq_dist
= sq_dist
;
823 snap_data
->nearest_is_grid
= false;
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
;
839 if (!TEST_FLAG (SNAPPINFLAG
, PCB
))
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);
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
))
866 dx
= line
->Point2
.X
- line
->Point1
.X
;
867 dy
= line
->Point2
.Y
- line
->Point1
.Y
;
869 /* Try snapping along the X axis */
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 */
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 */
891 dist
= line
->Point1
.Y
- nearest_grid_y
;
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 */
907 dist
= nearest_grid_y
- line
->Point1
.Y
;
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
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
;
930 Crosshair
.X
= CLAMP (X
, Crosshair
.MinX
, Crosshair
.MaxX
);
931 Crosshair
.Y
= CLAMP (Y
, Crosshair
.MinY
, Crosshair
.MaxY
);
935 nearest_grid_x
= -MIL_TO_COORD (6);
936 nearest_grid_y
= -MIL_TO_COORD (6);
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
;
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
;
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);
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
)
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;
1018 if (found_our_layer
== false)
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,
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
)
1044 PinType
*pin
= (PinType
*)ptr2
;
1045 check_snap_object (&snap_data
, pin
->X
, pin
->Y
, true);
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
&&
1061 PinType
*pin
= (PinType
*)ptr2
;
1062 check_snap_object (&snap_data
, pin
->X
, pin
->Y
, true);
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);
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
);
1080 if (TEST_FLAG (SNAPPINFLAG
, PCB
))
1081 ans
= SearchObjectByLocation (POLYGONPOINT_TYPE
, &ptr1
, &ptr2
, &ptr3
,
1082 Crosshair
.X
, Crosshair
.Y
, PCB
->Grid
/ 2);
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);
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
))
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
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
;
1146 /* ---------------------------------------------------------------------------
1147 * sets the valid range for the crosshair cursor
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
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
1184 DestroyCrosshair (void)
1186 FreePolygonMemory (&Crosshair
.AttachedPolygon
);