hid/gtk (GL): I think the polygon renderer works in mask mode now
[geda-pcb/pcjc2.git] / src / hid / common / extents.c
blobbc92bdc72aeb56bbfa58b8cc402c84c820631ead
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
10 #include "global.h"
11 #include "data.h"
13 #include "hid.h"
14 #include "hid_draw.h"
15 #include "../hidint.h"
16 #include "hid/common/draw_helpers.h"
18 #ifdef HAVE_LIBDMALLOC
19 #include <dmalloc.h>
20 #endif
22 static BoxType box;
24 typedef struct extents_gc_struct
26 struct hid_gc_struct hid_gc; /* Parent */
28 int width;
29 } *extentsGC;
31 static int
32 extents_set_layer (const char *name, int group, int empty)
34 int idx = group;
35 if (idx >= 0 && idx < max_group)
37 idx = PCB->LayerGroups.Entries[idx][0];
39 if (idx >= 0 && idx < max_copper_layer + EXTRA_LAYERS)
40 return 1;
41 if (idx < 0)
43 switch (SL_TYPE (idx))
45 case SL_INVISIBLE:
46 case SL_MASK:
47 case SL_ASSY:
48 return 0;
49 case SL_SILK:
50 case SL_PDRILL:
51 case SL_UDRILL:
52 return 1;
55 return 0;
58 static hidGC
59 extents_make_gc (void)
61 hidGC gc = (hidGC)calloc (1, sizeof (struct extents_gc_struct));
63 return gc;
66 static void
67 extents_destroy_gc (hidGC gc)
69 free (gc);
72 static void
73 extents_use_mask (enum mask_mode mode)
77 static void
78 extents_set_color (hidGC gc, const char *name)
82 static void
83 extents_set_line_cap (hidGC gc, EndCapStyle style)
87 static void
88 extents_set_line_width (hidGC gc, Coord width)
90 extentsGC extents_gc = (extentsGC)gc;
92 extents_gc->width = width;
95 static void
96 extents_set_draw_xor (hidGC gc, int xor_)
100 #define PEX(x,w) if (box.X1 > (x)-(w)) box.X1 = (x)-(w); \
101 if (box.X2 < (x)+(w)) box.X2 = (x)+(w)
102 #define PEY(y,w) if (box.Y1 > (y)-(w)) box.Y1 = (y)-(w); \
103 if (box.Y2 < (y)+(w)) box.Y2 = (y)+(w)
105 static void
106 extents_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
108 extentsGC extents_gc = (extentsGC)gc;
110 PEX (x1, extents_gc->width);
111 PEY (y1, extents_gc->width);
112 PEX (x2, extents_gc->width);
113 PEY (y2, extents_gc->width);
116 static void
117 extents_draw_arc (hidGC gc, Coord cx, Coord cy, Coord width, Coord height,
118 Angle start_angle, Angle end_angle)
120 extentsGC extents_gc = (extentsGC)gc;
122 /* Naive but good enough. */
123 PEX (cx, width + extents_gc->width);
124 PEY (cy, height + extents_gc->width);
127 static void
128 extents_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
130 extentsGC extents_gc = (extentsGC)gc;
132 PEX (x1, extents_gc->width);
133 PEY (y1, extents_gc->width);
134 PEX (x2, extents_gc->width);
135 PEY (y2, extents_gc->width);
138 static void
139 extents_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
141 PEX (cx, radius);
142 PEY (cy, radius);
145 static void
146 extents_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
148 int i;
149 for (i = 0; i < n_coords; i++)
151 PEX (x[i], 0);
152 PEY (y[i], 0);
156 static void
157 extents_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
159 PEX (x1, 0);
160 PEY (y1, 0);
161 PEX (x2, 0);
162 PEY (y2, 0);
165 static HID extents_hid;
166 static HID_DRAW extents_graphics;
167 static HID_DRAW_CLASS extents_graphics_class;
169 void
170 hid_extents_init (void)
172 static bool initialised = false;
174 if (initialised)
175 return;
177 memset (&extents_hid, 0, sizeof (HID));
178 memset (&extents_graphics, 0, sizeof (HID_DRAW));
179 memset (&extents_graphics_class, 0, sizeof (HID_DRAW_CLASS));
181 extents_hid.struct_size = sizeof (HID);
182 extents_hid.name = "extents-extents";
183 extents_hid.description = "used to calculate extents";
185 common_draw_helpers_class_init (&extents_graphics_class);
187 extents_graphics_class.set_layer = extents_set_layer;
188 extents_graphics_class.make_gc = extents_make_gc;
189 extents_graphics_class.destroy_gc = extents_destroy_gc;
190 extents_graphics_class.use_mask = extents_use_mask;
191 extents_graphics_class.set_color = extents_set_color;
192 extents_graphics_class.set_line_cap = extents_set_line_cap;
193 extents_graphics_class.set_line_width = extents_set_line_width;
194 extents_graphics_class.set_draw_xor = extents_set_draw_xor;
195 extents_graphics_class.draw_line = extents_draw_line;
196 extents_graphics_class.draw_arc = extents_draw_arc;
197 extents_graphics_class.draw_rect = extents_draw_rect;
198 extents_graphics_class.fill_circle = extents_fill_circle;
199 extents_graphics_class.fill_polygon = extents_fill_polygon;
200 extents_graphics_class.fill_rect = extents_fill_rect;
202 extents_graphics.klass = &extents_graphics_class;
203 extents_graphics.poly_before = true;
204 common_draw_helpers_init (&extents_graphics);
206 initialised = true;
210 BoxType *
211 hid_get_extents (void *item)
213 BoxType region;
215 /* As this isn't a real "HID", we need to ensure we are initialised. */
216 hid_extents_init ();
218 box.X1 = COORD_MAX;
219 box.Y1 = COORD_MAX;
220 box.X2 = -COORD_MAX - 1;
221 box.Y2 = -COORD_MAX - 1;
223 region.X1 = -COORD_MAX - 1;
224 region.Y1 = -COORD_MAX - 1;
225 region.X2 = COORD_MAX;
226 region.Y2 = COORD_MAX;
227 hid_expose_callback (&extents_graphics, &region, item);
229 return &box;