File: gtkhid-main.c

package info (click to toggle)
pcb 20110918-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,304 kB
  • sloc: ansic: 121,422; sh: 7,232; yacc: 5,088; pascal: 4,136; makefile: 1,535; perl: 564; lex: 438; awk: 158; lisp: 86; tcl: 63; xml: 20
file content (2204 lines) | stat: -rw-r--r-- 56,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <math.h>
#include <time.h>
#include <unistd.h>


#include "action.h"
#include "crosshair.h"
#include "error.h"
#include "../hidint.h"
#include "gui.h"
#include "hid/common/hidnogui.h"
#include "hid/common/draw_helpers.h"
#include "pcb-printf.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif


RCSID ("$Id$");


static void
pan_common (GHidPort *port)
{
  int event_x, event_y;

  /* We need to fix up the PCB coordinates corresponding to the last
  * event so convert it back to event coordinates temporarily. */
  ghid_pcb_to_event_coords (gport->pcb_x, gport->pcb_y, &event_x, &event_y);

  /* Don't pan so far the board is completely off the screen */
  port->view.x0 = MAX (-port->view.width,  port->view.x0);
  port->view.y0 = MAX (-port->view.height, port->view.y0);
  port->view.x0 = MIN ( port->view.x0, PCB->MaxWidth);
  port->view.y0 = MIN ( port->view.y0, PCB->MaxHeight);

  /* Fix up noted event coordinates to match where we clamped. Alternatively
   * we could call ghid_note_event_location (NULL); to get a new pointer
   * location, but this costs us an xserver round-trip (on X11 platforms)
   */
  ghid_event_to_pcb_coords (event_x, event_y, &gport->pcb_x, &gport->pcb_y);

  ghidgui->adjustment_changed_holdoff = TRUE;
  gtk_range_set_value (GTK_RANGE (ghidgui->h_range), gport->view.x0);
  gtk_range_set_value (GTK_RANGE (ghidgui->v_range), gport->view.y0);
  ghidgui->adjustment_changed_holdoff = FALSE;

  ghid_port_ranges_changed();
}

static void
ghid_pan_view_abs (Coord pcb_x, Coord pcb_y, int widget_x, int widget_y)
{
  gport->view.x0 = SIDE_X (pcb_x) - widget_x * gport->view.coord_per_px;
  gport->view.y0 = SIDE_Y (pcb_y) - widget_y * gport->view.coord_per_px;

  pan_common (gport);
}

void
ghid_pan_view_rel (Coord dx, Coord dy)
{
  gport->view.x0 += dx;
  gport->view.y0 += dy;

  pan_common (gport);
}


/* gport->view.coord_per_px:
 * zoom value is PCB units per screen pixel.  Larger numbers mean zooming
 * out - the largest value means you are looking at the whole board.
 *
 * gport->view_width and gport->view_height are in PCB coordinates
 */

#define ALLOW_ZOOM_OUT_BY 10 /* Arbitrary, and same as the lesstif HID */
static void
ghid_zoom_view_abs (Coord center_x, Coord center_y, double new_zoom)
{
  double min_zoom, max_zoom;
  double xtmp, ytmp;

  /* Limit the "minimum" zoom constant (maximum zoom), at 1 pixel per PCB
   * unit, and set the "maximum" zoom constant (minimum zoom), such that
   * the entire board just fits inside the viewport
   */
  min_zoom = 1;
  max_zoom = MAX (PCB->MaxWidth  / gport->width,
                  PCB->MaxHeight / gport->height) * ALLOW_ZOOM_OUT_BY;
  new_zoom = MIN (MAX (min_zoom, new_zoom), max_zoom);

  if (gport->view.coord_per_px == new_zoom)
    return;

  xtmp = (SIDE_X (center_x) - gport->view.x0) / (double)gport->view.width;
  ytmp = (SIDE_Y (center_y) - gport->view.y0) / (double)gport->view.height;

  gport->view.coord_per_px = new_zoom;
  pixel_slop = new_zoom;
  ghid_port_ranges_scale ();

  gport->view.x0 = SIDE_X (center_x) - xtmp * gport->view.width;
  gport->view.y0 = SIDE_Y (center_y) - ytmp * gport->view.height;

  pan_common (gport);

  ghid_set_status_line_label ();
}

static void
ghid_zoom_view_rel (Coord center_x, Coord center_y, double factor)
{
  ghid_zoom_view_abs (center_x, center_y, gport->view.coord_per_px * factor);
}

static void
ghid_zoom_view_fit (void)
{
  ghid_pan_view_abs (SIDE_X (0), SIDE_Y (0), 0, 0);
  ghid_zoom_view_abs (SIDE_X (0), SIDE_Y (0),
                      MAX (PCB->MaxWidth  / gport->width,
                           PCB->MaxHeight / gport->height));
}

static void
ghid_flip_view (Coord center_x, Coord center_y, bool flip_x, bool flip_y)
{
  int widget_x, widget_y;

  /* Work out where on the screen the flip point is */
  ghid_pcb_to_event_coords (center_x, center_y, &widget_x, &widget_y);

  gport->view.flip_x = gport->view.flip_x != flip_x;
  gport->view.flip_y = gport->view.flip_y != flip_y;

  /* Pan the board so the center location remains in the same place */
  ghid_pan_view_abs (center_x, center_y, widget_x, widget_y);

  ghid_invalidate_all ();
}

/* ------------------------------------------------------------ */

static const char zoom_syntax[] =
"Zoom()\n"
"Zoom(factor)";


static const char zoom_help[] =
N_("Various zoom factor changes.");

/* %start-doc actions Zoom
Changes the zoom (magnification) of the view of the board.  If no
arguments are passed, the view is scaled such that the board just fits
inside the visible window (i.e. ``view all'').  Otherwise,
@var{factor} specifies a change in zoom factor.  It may be prefixed by
@code{+}, @code{-}, or @code{=} to change how the zoom factor is
modified.  The @var{factor} is a floating point number, such as
@code{1.5} or @code{0.75}.

@table @code
  
@item +@var{factor}
Values greater than 1.0 cause the board to be drawn smaller; more of
the board will be visible.  Values between 0.0 and 1.0 cause the board
to be drawn bigger; less of the board will be visible.
  
@item -@var{factor}
Values greater than 1.0 cause the board to be drawn bigger; less of
the board will be visible.  Values between 0.0 and 1.0 cause the board
to be drawn smaller; more of the board will be visible.
 
@item =@var{factor}
 
The @var{factor} is an absolute zoom factor; the unit for this value
is "PCB units per screen pixel".  Since PCB units are 0.01 mil, a
@var{factor} of 1000 means 10 mils (0.01 in) per pixel, or 100 DPI,
about the actual resolution of most screens - resulting in an "actual
size" board.  Similarly, a @var{factor} of 100 gives you a 10x actual
size.
 
@end table
 
Note that zoom factors of zero are silently ignored.
 


%end-doc */

static int
Zoom (int argc, char **argv, Coord x, Coord y)
{
  const char *vp;
  double v;

  if (argc > 1)
    AFAIL (zoom);

  if (argc < 1)
    {
      ghid_zoom_view_fit ();
      return 0;
    }

  vp = argv[0];
  if (*vp == '+' || *vp == '-' || *vp == '=')
    vp++;
  v = g_ascii_strtod (vp, 0);
  if (v <= 0)
    return 1;
  switch (argv[0][0])
    {
    case '-':
      ghid_zoom_view_rel (x, y, 1 / v);
      break;
    default:
    case '+':
      ghid_zoom_view_rel (x, y, v);
      break;
    case '=':
      ghid_zoom_view_abs (x, y, v);
      break;
    }

  return 0;
}

/* ------------------------------------------------------------ */

void
ghid_calibrate (double xval, double yval)
{
  printf (_("ghid_calibrate() -- not implemented\n"));
}

static int ghid_gui_is_up = 0;

void
ghid_notify_gui_is_up ()
{
  ghid_gui_is_up = 1;
}

int
ghid_shift_is_pressed ()
{
  GdkModifierType mask;
  GHidPort *out = &ghid_port;

  if( ! ghid_gui_is_up ) 
    return 0;

  gdk_window_get_pointer (gtk_widget_get_window (out->drawing_area),
                          NULL, NULL, &mask);
  return (mask & GDK_SHIFT_MASK) ? TRUE : FALSE;
}

int
ghid_control_is_pressed ()
{
  GdkModifierType mask;
  GHidPort *out = &ghid_port;

  if( ! ghid_gui_is_up )
    return 0;

  gdk_window_get_pointer (gtk_widget_get_window (out->drawing_area),
                          NULL, NULL, &mask);
  return (mask & GDK_CONTROL_MASK) ? TRUE : FALSE;
}

int
ghid_mod1_is_pressed ()
{
  GdkModifierType mask;
  GHidPort *out = &ghid_port;

  if( ! ghid_gui_is_up )
    return 0;

  gdk_window_get_pointer (gtk_widget_get_window (out->drawing_area),
                          NULL, NULL, &mask);
#ifdef __APPLE__
  return (mask & ( 1 << 13 ) ) ? TRUE : FALSE;  // The option key is not MOD1, although it should be...
#else
  return (mask & GDK_MOD1_MASK) ? TRUE : FALSE;
#endif
}

void
ghid_set_crosshair (int x, int y, int action)
{
  GdkDisplay *display;
  GdkScreen *screen;
  int offset_x, offset_y;
  int widget_x, widget_y;
  int pointer_x, pointer_y;
  Coord pcb_x, pcb_y;

  if (gport->crosshair_x != x || gport->crosshair_y != y)
    {
      ghid_set_cursor_position_labels ();
      gport->crosshair_x = x;
      gport->crosshair_y = y;

      /* FIXME - does this trigger the idle_proc stuff?  It is in the
       * lesstif HID.  Maybe something is needed here?
       *
       * need_idle_proc ();
       */
    }

  if (action != HID_SC_PAN_VIEWPORT &&
      action != HID_SC_WARP_POINTER)
    return;

  /* Find out where the drawing area is on the screen. gdk_display_get_pointer
   * and gdk_display_warp_pointer work relative to the whole display, whilst
   * our coordinates are relative to the drawing area origin.
   */
  gdk_window_get_origin (gtk_widget_get_window (gport->drawing_area),
                         &offset_x, &offset_y);
  display = gdk_display_get_default ();

  switch (action) {
    case HID_SC_PAN_VIEWPORT:
      /* Pan the board in the viewport so that the crosshair (who's location
       * relative on the board was set above) lands where the pointer is.
       * We pass the request to pan a particular point on the board to a
       * given widget coordinate of the viewport into the rendering code
       */

      /* Find out where the pointer is relative to the display */
      gdk_display_get_pointer (display, NULL, &pointer_x, &pointer_y, NULL);

      widget_x = pointer_x - offset_x;
      widget_y = pointer_y - offset_y;

      ghid_event_to_pcb_coords (widget_x, widget_y, &pcb_x, &pcb_y);
      ghid_pan_view_abs (pcb_x, pcb_y, widget_x, widget_y);

      /* Just in case we couldn't pan the board the whole way,
       * we warp the pointer to where the crosshair DID land.
       */
      /* Fall through */

    case HID_SC_WARP_POINTER:
      screen = gdk_display_get_default_screen (display);

      ghid_pcb_to_event_coords (x, y, &widget_x, &widget_y);

      pointer_x = offset_x + widget_x;
      pointer_y = offset_y + widget_y;

      gdk_display_warp_pointer (display, screen, pointer_x, pointer_y);

      break;
  }
}

typedef struct
{
  void (*func) (hidval);
  guint id;
  hidval user_data;
}
GuiTimer;

  /* We need a wrapper around the hid timer because a gtk timer needs
     |  to return FALSE else the timer will be restarted.
   */
static gboolean
ghid_timer (GuiTimer * timer)
{
  (*timer->func) (timer->user_data);
  ghid_mode_cursor (Settings.Mode);
  return FALSE;			/* Turns timer off */
}

hidval
ghid_add_timer (void (*func) (hidval user_data),
		unsigned long milliseconds, hidval user_data)
{
  GuiTimer *timer = g_new0 (GuiTimer, 1);
  hidval ret;

  timer->func = func;
  timer->user_data = user_data;
  timer->id = g_timeout_add (milliseconds, (GSourceFunc) ghid_timer, timer);
  ret.ptr = (void *) timer;
  return ret;
}

void
ghid_stop_timer (hidval timer)
{
  void *ptr = timer.ptr;

  g_source_remove (((GuiTimer *) ptr)->id);
  g_free( ptr );
}

typedef struct
{
  void (*func) ( hidval, int, unsigned int, hidval );
  hidval user_data;
  int fd;
  GIOChannel *channel;
  gint id;
}
GuiWatch;

  /* We need a wrapper around the hid file watch to pass the correct flags
   */
static gboolean
ghid_watch (GIOChannel *source, GIOCondition condition, gpointer data)
{
  unsigned int pcb_condition = 0;
  hidval x;
  GuiWatch *watch = (GuiWatch*)data;

  if (condition & G_IO_IN)
    pcb_condition |= PCB_WATCH_READABLE;
  if (condition & G_IO_OUT)
    pcb_condition |= PCB_WATCH_WRITABLE;
  if (condition & G_IO_ERR)
    pcb_condition |= PCB_WATCH_ERROR;
  if (condition & G_IO_HUP)
    pcb_condition |= PCB_WATCH_HANGUP;

  x.ptr = (void *) watch;
  watch->func (x, watch->fd, pcb_condition, watch->user_data);
  ghid_mode_cursor (Settings.Mode);

  return TRUE;  /* Leave watch on */
}

hidval
ghid_watch_file (int fd, unsigned int condition, void (*func) (hidval watch, int fd, unsigned int condition, hidval user_data),
  hidval user_data)
{
  GuiWatch *watch = g_new0 (GuiWatch, 1);
  hidval ret;
  unsigned int glib_condition = 0;

  if (condition & PCB_WATCH_READABLE)
    glib_condition |= G_IO_IN;
  if (condition & PCB_WATCH_WRITABLE)
    glib_condition |= G_IO_OUT;
  if (condition & PCB_WATCH_ERROR)
    glib_condition |= G_IO_ERR;
  if (condition & PCB_WATCH_HANGUP)
    glib_condition |= G_IO_HUP;

  watch->func = func;
  watch->user_data = user_data;
  watch->fd = fd;
  watch->channel = g_io_channel_unix_new( fd );
  watch->id = g_io_add_watch( watch->channel, (GIOCondition)glib_condition, ghid_watch, watch );

  ret.ptr = (void *) watch;
  return ret;
}

void
ghid_unwatch_file (hidval data)
{
  GuiWatch *watch = (GuiWatch*)data.ptr;

  g_io_channel_shutdown( watch->channel, TRUE, NULL ); 
  g_io_channel_unref( watch->channel );
  g_free( watch );
}

typedef struct
{
  GSource source;
  void (*func) (hidval user_data);
  hidval user_data; 
} BlockHookSource;

static gboolean ghid_block_hook_prepare  (GSource     *source,
                                             gint     *timeout);
static gboolean ghid_block_hook_check    (GSource     *source);
static gboolean ghid_block_hook_dispatch (GSource     *source,
                                          GSourceFunc  callback,
                                          gpointer     user_data);

static GSourceFuncs ghid_block_hook_funcs = {
  ghid_block_hook_prepare,
  ghid_block_hook_check,
  ghid_block_hook_dispatch,
  NULL // No destroy notification
};

static gboolean
ghid_block_hook_prepare (GSource *source,
                         gint    *timeout)
{
  hidval data = ((BlockHookSource *)source)->user_data;
  ((BlockHookSource *)source)->func( data );
  return FALSE;
}

static gboolean
ghid_block_hook_check (GSource *source)
{
  return FALSE;
}

static gboolean
ghid_block_hook_dispatch (GSource     *source,
                          GSourceFunc  callback,
                          gpointer     user_data)
{
  return FALSE;
}

static hidval
ghid_add_block_hook (void (*func) (hidval data),
                     hidval user_data)
{
  hidval ret;
  BlockHookSource *source;

  source = (BlockHookSource *)g_source_new (&ghid_block_hook_funcs, sizeof( BlockHookSource ));

  source->func = func;
  source->user_data = user_data;

  g_source_attach ((GSource *)source, NULL);

  ret.ptr = (void *) source;
  return ret;
}

static void
ghid_stop_block_hook (hidval mlpoll)
{
  GSource *source = (GSource *)mlpoll.ptr;
  g_source_destroy( source );
}

int
ghid_confirm_dialog (char *msg, ...)
{
  int rv = 0;
  va_list ap;
  char *cancelmsg = NULL, *okmsg = NULL;
  static gint x = -1, y = -1;
  GtkWidget *dialog;
  GHidPort *out = &ghid_port;

  va_start (ap, msg);
  cancelmsg = va_arg (ap, char *);
  okmsg = va_arg (ap, char *);
  va_end (ap);

  if (!cancelmsg)
    {
      cancelmsg = _("_Cancel");
      okmsg = _("_OK");
    }

  dialog = gtk_message_dialog_new (GTK_WINDOW (out->top_window),
				   (GtkDialogFlags) (GTK_DIALOG_MODAL |
						     GTK_DIALOG_DESTROY_WITH_PARENT),
				   GTK_MESSAGE_QUESTION,
				   GTK_BUTTONS_NONE,
				   "%s", msg);
  gtk_dialog_add_button (GTK_DIALOG (dialog), 
			  cancelmsg, GTK_RESPONSE_CANCEL);
  if (okmsg)
    {
      gtk_dialog_add_button (GTK_DIALOG (dialog), 
			     okmsg, GTK_RESPONSE_OK);
    }

  if(x != -1) {
  	gtk_window_move(GTK_WINDOW (dialog), x, y);
  }

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
    rv = 1;

  gtk_window_get_position(GTK_WINDOW (dialog), &x, &y);

  gtk_widget_destroy (dialog);
  return rv;
}

int
ghid_close_confirm_dialog ()
{
  switch (ghid_dialog_close_confirm ())
    {
    case GUI_DIALOG_CLOSE_CONFIRM_SAVE:
      {
        if (hid_actionl ("Save", NULL))
          { /* Save failed */
            return 0; /* Cancel */
          } else {
            return 1; /* Close */
          }
      }
    case GUI_DIALOG_CLOSE_CONFIRM_NOSAVE:
      {
        return 1; /* Close */
      }
    case GUI_DIALOG_CLOSE_CONFIRM_CANCEL:
    default:
      {
        return 0; /* Cancel */
      }
    }
}

void
ghid_report_dialog (char *title, char *msg)
{
  ghid_dialog_report (title, msg);
}

char *
ghid_prompt_for (const char *msg, const char *default_string)
{
  char *rv;

  rv = ghid_dialog_input (msg, default_string);
  return rv;
}

/* FIXME -- implement a proper file select dialog */
#ifdef FIXME
char *
ghid_fileselect (const char *title, const char *descr,
		 char *default_file, char *default_ext,
		 const char *history_tag, int flags)
{
  char *rv;

  rv = ghid_dialog_input (title, default_file);
  return rv;
}
#endif

void
ghid_show_item (void *item)
{
  ghid_pinout_window_show (&ghid_port, (ElementTypePtr) item);
}

void
ghid_beep ()
{
  gdk_beep ();
}

struct progress_dialog
{
  GtkWidget *dialog;
  GtkWidget *message;
  GtkWidget *progress;
  gint response_id;
  GMainLoop *loop;
  gboolean destroyed;
  gboolean started;
  GTimer *timer;

  gulong response_handler;
  gulong destroy_handler;
  gulong delete_handler;
};

static void
run_response_handler (GtkDialog *dialog,
                      gint response_id,
                      gpointer data)
{
  struct progress_dialog *pd = data;

  pd->response_id = response_id;
}

static gint
run_delete_handler (GtkDialog *dialog,
                    GdkEventAny *event,
                    gpointer data)
{
  struct progress_dialog *pd = data;

  pd->response_id = GTK_RESPONSE_DELETE_EVENT;

  return TRUE; /* Do not destroy */
}

static void
run_destroy_handler (GtkDialog *dialog, gpointer data)
{
  struct progress_dialog *pd = data;

  pd->destroyed = TRUE;
}

static struct progress_dialog *
make_progress_dialog (void)
{
  struct progress_dialog *pd;
  GtkWidget *content_area;
  GtkWidget *alignment;
  GtkWidget *vbox;

  pd = g_new0 (struct progress_dialog, 1);
  pd->response_id = GTK_RESPONSE_NONE;

  pd->dialog = gtk_dialog_new_with_buttons (_("Progress"),
                                            GTK_WINDOW (gport->top_window),
                                            /* Modal so nothing else can get events whilst
                                               the main mainloop isn't running */
                                            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                            GTK_STOCK_CANCEL,
                                            GTK_RESPONSE_CANCEL,
                                            NULL);

  gtk_window_set_deletable (GTK_WINDOW (pd->dialog), FALSE);
  gtk_window_set_skip_pager_hint (GTK_WINDOW (pd->dialog), TRUE);
  gtk_window_set_skip_taskbar_hint (GTK_WINDOW (pd->dialog), TRUE);
  gtk_widget_set_size_request (pd->dialog, 300, -1);

  pd->message = gtk_label_new (NULL);
  gtk_misc_set_alignment (GTK_MISC (pd->message), 0., 0.);

  pd->progress = gtk_progress_bar_new ();
  gtk_widget_set_size_request (pd->progress, -1, 26);

  vbox = gtk_vbox_new (false, 0);
  gtk_box_pack_start (GTK_BOX (vbox), pd->message, true, true, 8);
  gtk_box_pack_start (GTK_BOX (vbox), pd->progress, false, true, 8);

  alignment = gtk_alignment_new (0., 0., 1., 1.);
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 8, 8, 8, 8);
  gtk_container_add (GTK_CONTAINER (alignment), vbox);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (pd->dialog));
  gtk_box_pack_start (GTK_BOX (content_area), alignment, true, true, 0);

  gtk_widget_show_all (alignment);

  g_object_ref (pd->dialog);
  gtk_window_present (GTK_WINDOW (pd->dialog));

  pd->response_handler =
    g_signal_connect (pd->dialog, "response",
                      G_CALLBACK (run_response_handler), pd);
  pd->delete_handler =
    g_signal_connect (pd->dialog, "delete-event",
                      G_CALLBACK (run_delete_handler), pd);
  pd->destroy_handler =
    g_signal_connect (pd->dialog, "destroy",
                      G_CALLBACK (run_destroy_handler), pd);

  pd->loop = g_main_loop_new (NULL, FALSE);
  pd->timer = g_timer_new ();

  return pd;
}

static void
destroy_progress_dialog (struct progress_dialog *pd)
{
  if (pd == NULL)
    return;

  if (!pd->destroyed)
    {
      g_signal_handler_disconnect (pd->dialog, pd->response_handler);
      g_signal_handler_disconnect (pd->dialog, pd->delete_handler);
      g_signal_handler_disconnect (pd->dialog, pd->destroy_handler);
    }

  g_timer_destroy (pd->timer);
  g_object_unref (pd->dialog);
  g_main_loop_unref (pd->loop);

  gtk_widget_destroy (pd->dialog);

  pd->loop = NULL;
  g_free (pd);
}

static void
handle_progress_dialog_events (struct progress_dialog *pd)
{
  GMainContext * context = g_main_loop_get_context (pd->loop);

  /* Process events */
  while (g_main_context_pending (context))
    {
      g_main_context_iteration (context, FALSE);
    }
}

#define MIN_TIME_SEPARATION (50./1000.) /* 50ms */
static int
ghid_progress (int so_far, int total, const char *message)
{
  static struct progress_dialog *pd = NULL;

  /* If we are finished, destroy any dialog */
  if (so_far == 0 && total == 0 && message == NULL)
    {
      destroy_progress_dialog (pd);
      pd = NULL;
      return 0;
    }

  if (pd == NULL)
    pd = make_progress_dialog ();

  /* We don't want to keep the underlying process too busy whilst we
   * process events. If we get called quickly after the last progress
   * update, wait a little bit before we respond - perhaps the next
   * time progress is reported.

   * The exception here is that we always want to process the first
   * batch of events after having shown the dialog for the first time
   */
  if (pd->started && g_timer_elapsed (pd->timer, NULL) < MIN_TIME_SEPARATION)
    return 0;

  gtk_label_set_text (GTK_LABEL (pd->message), message);
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pd->progress),
                                 (double)so_far / (double)total);

  handle_progress_dialog_events (pd);
  g_timer_start (pd->timer);

  pd->started = TRUE;

  return (pd->response_id == GTK_RESPONSE_CANCEL ||
          pd->response_id == GTK_RESPONSE_DELETE_EVENT) ? 1 : 0;
}

/* ---------------------------------------------------------------------- */


typedef struct {
  GtkWidget *del;
  GtkWidget *w_name;
  GtkWidget *w_value;
} AttrRow;

static AttrRow *attr_row = 0;
static int attr_num_rows = 0;
static int attr_max_rows = 0;
static AttributeListType *attributes_list;
static GtkWidget *attributes_dialog, *attr_table;

static void attributes_delete_callback (GtkWidget *w, void *v);

#define GA_RESPONSE_REVERT	1
#define GA_RESPONSE_NEW		2

static void
ghid_attr_set_table_size ()
{
  gtk_table_resize (GTK_TABLE (attr_table), attr_num_rows > 0 ? attr_num_rows : 1, 3);
}

static void
ghid_attributes_need_rows (int new_max)
{
  if (attr_max_rows < new_max)
    {
      if (attr_row)
	attr_row = (AttrRow *) realloc (attr_row, new_max * sizeof(AttrRow));
      else
	attr_row = (AttrRow *) malloc (new_max * sizeof(AttrRow));
    }
  while (attr_max_rows < new_max)
    {
      /* add [attr_max_rows] */
      attr_row[attr_max_rows].del = gtk_button_new_with_label ("del");
      gtk_table_attach (GTK_TABLE (attr_table), attr_row[attr_max_rows].del,
			0, 1,
			attr_max_rows, attr_max_rows+1,
			(GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
			GTK_FILL,
			0, 0);
      g_signal_connect (G_OBJECT (attr_row[attr_max_rows].del), "clicked",
			G_CALLBACK (attributes_delete_callback), GINT_TO_POINTER (attr_max_rows) );

      attr_row[attr_max_rows].w_name = gtk_entry_new ();
      gtk_table_attach (GTK_TABLE (attr_table), attr_row[attr_max_rows].w_name,
			1, 2,
			attr_max_rows, attr_max_rows+1,
			(GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
			GTK_FILL,
			0, 0);

      attr_row[attr_max_rows].w_value = gtk_entry_new ();
      gtk_table_attach (GTK_TABLE (attr_table), attr_row[attr_max_rows].w_value,
			2, 3,
			attr_max_rows, attr_max_rows+1,
			(GtkAttachOptions)(GTK_FILL | GTK_EXPAND),
			GTK_FILL,
			0, 0);

      attr_max_rows ++;
    }

  /* Manage any previously unused rows we now need to show.  */
  while (attr_num_rows < new_max)
    {
      /* manage attr_num_rows */
      gtk_widget_show (attr_row[attr_num_rows].del);
      gtk_widget_show (attr_row[attr_num_rows].w_name);
      gtk_widget_show (attr_row[attr_num_rows].w_value);
      attr_num_rows ++;
    }
}

static void
ghid_attributes_revert ()
{
  int i;

  ghid_attributes_need_rows (attributes_list->Number);

  /* Unmanage any previously used rows we don't need.  */
  while (attr_num_rows > attributes_list->Number)
    {
      attr_num_rows --;
      gtk_widget_hide (attr_row[attr_num_rows].del);
      gtk_widget_hide (attr_row[attr_num_rows].w_name);
      gtk_widget_hide (attr_row[attr_num_rows].w_value);
    }

  /* Fill in values */
  for (i=0; i<attributes_list->Number; i++)
    {
      /* create row [i] */
      gtk_entry_set_text (GTK_ENTRY (attr_row[i].w_name), attributes_list->List[i].name);
      gtk_entry_set_text (GTK_ENTRY (attr_row[i].w_value), attributes_list->List[i].value);
#if 0
#endif
    }
  ghid_attr_set_table_size ();
}

static void
attributes_delete_callback (GtkWidget *w, void *v)
{
  int i, n;

  n = GPOINTER_TO_INT (v);

  for (i=n; i<attr_num_rows-1; i++)
    {
      gtk_entry_set_text (GTK_ENTRY (attr_row[i].w_name),
			  gtk_entry_get_text (GTK_ENTRY (attr_row[i+1].w_name)));
      gtk_entry_set_text (GTK_ENTRY (attr_row[i].w_value),
			  gtk_entry_get_text (GTK_ENTRY (attr_row[i+1].w_value)));
    }
  attr_num_rows --;

  gtk_widget_hide (attr_row[attr_num_rows].del);
  gtk_widget_hide (attr_row[attr_num_rows].w_name);
  gtk_widget_hide (attr_row[attr_num_rows].w_value);

  ghid_attr_set_table_size ();
}

static void
ghid_attributes (char *owner, AttributeListType *attrs)
{
  GtkWidget *content_area;
  int response;

  attributes_list = attrs;

  attr_max_rows = 0;
  attr_num_rows = 0;

  attributes_dialog = gtk_dialog_new_with_buttons (owner,
						   GTK_WINDOW (ghid_port.top_window),
						   GTK_DIALOG_MODAL,
						   GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
						   "Revert", GA_RESPONSE_REVERT,
						   "New", GA_RESPONSE_NEW,
						   GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

  attr_table = gtk_table_new (attrs->Number, 3, 0);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (attributes_dialog));
  gtk_box_pack_start (GTK_BOX (content_area), attr_table, FALSE, FALSE, 0);

  gtk_widget_show (attr_table);

  ghid_attributes_revert ();

  while (1)
    {
      response = gtk_dialog_run (GTK_DIALOG (attributes_dialog));

      if (response == GTK_RESPONSE_CANCEL)
	break;

      if (response == GTK_RESPONSE_OK)
	{
	  int i;
	  /* Copy the values back */
	  for (i=0; i<attributes_list->Number; i++)
	    {
	      if (attributes_list->List[i].name)
		free (attributes_list->List[i].name);
	      if (attributes_list->List[i].value)
		free (attributes_list->List[i].value);
	    }
	  if (attributes_list->Max < attr_num_rows)
	    {
	      int sz = attr_num_rows * sizeof (AttributeType);
	      if (attributes_list->List == NULL)
		attributes_list->List = (AttributeType *) malloc (sz);
	      else
		attributes_list->List = (AttributeType *) realloc (attributes_list->List, sz);
	      attributes_list->Max = attr_num_rows;
	    }
	  for (i=0; i<attr_num_rows; i++)
	    {
	      attributes_list->List[i].name = strdup (gtk_entry_get_text (GTK_ENTRY (attr_row[i].w_name)));
	      attributes_list->List[i].value = strdup (gtk_entry_get_text (GTK_ENTRY (attr_row[i].w_value)));
	      attributes_list->Number = attr_num_rows;
	    }

	  break;
	}

      if (response == GA_RESPONSE_REVERT)
	{
	  /* Revert */
	  ghid_attributes_revert ();
	}

      if (response == GA_RESPONSE_NEW)
	{
	  ghid_attributes_need_rows (attr_num_rows + 1); /* also bumps attr_num_rows */

	  gtk_entry_set_text (GTK_ENTRY (attr_row[attr_num_rows-1].w_name), "");
	  gtk_entry_set_text (GTK_ENTRY (attr_row[attr_num_rows-1].w_value), "");

	  ghid_attr_set_table_size ();
	}
    }

  gtk_widget_destroy (attributes_dialog);
  free (attr_row);
  attr_row = NULL;
}

/* ---------------------------------------------------------------------- */

HID_DRC_GUI ghid_drc_gui = {
  1,				/* log_drc_overview */
  0,				/* log_drc_details */
  ghid_drc_window_reset_message,
  ghid_drc_window_append_violation,
  ghid_drc_window_throw_dialog,
};

extern HID_Attribute *ghid_get_export_options (int *);


/* ------------------------------------------------------------ 
 *
 * Actions specific to the GTK HID follow from here
 *
 */


/* ------------------------------------------------------------ */
static const char about_syntax[] =
"About()";

static const char about_help[] =
N_("Tell the user about this version of PCB.");

/* %start-doc actions About

This just pops up a dialog telling the user which version of
@code{pcb} they're running.

%end-doc */


static int
About (int argc, char **argv, Coord x, Coord y)
{
  ghid_dialog_about ();
  return 0;
}

/* ------------------------------------------------------------ */
static const char getxy_syntax[] =
"GetXY()";

static const char getxy_help[] =
N_("Get a coordinate.");

/* %start-doc actions GetXY

Prompts the user for a coordinate, if one is not already selected.

%end-doc */

static int
GetXY (int argc, char **argv, Coord x, Coord y)
{
  return 0;
}

/* ---------------------------------------------------------------------- */

static int PointCursor (int argc, char **argv, Coord x, Coord y)
{
  if (!ghidgui)
    return 0;

  if (argc > 0)
    ghid_point_cursor ();
  else
    ghid_mode_cursor (Settings.Mode);
  return 0;
}

/* ---------------------------------------------------------------------- */

static int
RouteStylesChanged (int argc, char **argv, Coord x, Coord y)
{
  if (!ghidgui || !ghidgui->route_style_selector)
    return 0;

  ghid_route_style_selector_sync
    (GHID_ROUTE_STYLE_SELECTOR (ghidgui->route_style_selector),
     Settings.LineThickness, Settings.ViaDrillingHole,
     Settings.ViaThickness, Settings.Keepaway);

  return 0;
}

/* ---------------------------------------------------------------------- */

int
PCBChanged (int argc, char **argv, Coord x, Coord y)
{
  if (!ghidgui)
    return 0;

  ghid_window_set_name_label (PCB->Name);

  if (!gport->pixmap)
    return 0;

  if (ghidgui->route_style_selector)
    {
      ghid_route_style_selector_empty
        (GHID_ROUTE_STYLE_SELECTOR (ghidgui->route_style_selector));
      make_route_style_buttons
        (GHID_ROUTE_STYLE_SELECTOR (ghidgui->route_style_selector));
    }
  RouteStylesChanged (0, NULL, 0, 0);

  ghid_port_ranges_scale ();
  ghid_zoom_view_fit ();
  ghid_sync_with_new_layout ();
  return 0;
}

/* ---------------------------------------------------------------------- */

static int
LayerGroupsChanged (int argc, char **argv, Coord x, Coord y)
{
  printf (_("LayerGroupsChanged -- not implemented\n"));
  return 0;
}

/* ---------------------------------------------------------------------- */

static int
LibraryChanged (int argc, char **argv, Coord x, Coord y)
{
  /* No need to show the library window every time it changes...
   *  ghid_library_window_show (&ghid_port, FALSE);
   */
  return 0;
}

/* ---------------------------------------------------------------------- */

static int
Command (int argc, char **argv, Coord x, Coord y)
{
  ghid_handle_user_command (TRUE);
  return 0;
}

/* ---------------------------------------------------------------------- */

static int
Load (int argc, char **argv, Coord x, Coord y)
{
  char *function;
  char *name = NULL;

  static gchar *current_element_dir = NULL;
  static gchar *current_layout_dir = NULL;
  static gchar *current_netlist_dir = NULL;

  if(!current_element_dir)
    current_element_dir = get_current_dir_name();
  if(!current_layout_dir)
    current_layout_dir = get_current_dir_name();
  if(!current_netlist_dir)
    current_netlist_dir = get_current_dir_name();

  /* we've been given the file name */
  if (argc > 1)
    return hid_actionv ("LoadFrom", argc, argv);

  function = argc ? argv[0] : (char *)"Layout";

  if (strcasecmp (function, "Netlist") == 0)
    {
      name = ghid_dialog_file_select_open (_("Load netlist file"),
					   &current_netlist_dir,
					   Settings.FilePath);
    }
  else if (strcasecmp (function, "ElementToBuffer") == 0)
    {
      name = ghid_dialog_file_select_open (_("Load element to buffer"),
					   &current_element_dir,
					   Settings.LibraryTree);
    }
  else if (strcasecmp (function, "LayoutToBuffer") == 0)
    {
      name = ghid_dialog_file_select_open (_("Load layout file to buffer"),
					   &current_layout_dir,
					   Settings.FilePath);
    }
  else if (strcasecmp (function, "Layout") == 0)
    {
      name = ghid_dialog_file_select_open (_("Load layout file"),
					   &current_layout_dir,
					   Settings.FilePath);
    }

  if (name)
    {
      if (Settings.verbose)
      	fprintf (stderr, "%s:  Calling LoadFrom(%s, %s)\n", __FUNCTION__,
		 function, name);
      hid_actionl ("LoadFrom", function, name, NULL);
      g_free (name);
    }

  return 0;
}


/* ---------------------------------------------------------------------- */
static const char save_syntax[] =
"Save()\n"
"Save(Layout|LayoutAs)\n"
"Save(AllConnections|AllUnusedPins|ElementConnections)\n"
"Save(PasteBuffer)";

static const char save_help[] =
N_("Save layout and/or element data to a user-selected file.");

/* %start-doc actions Save

This action is a GUI front-end to the core's @code{SaveTo} action
(@pxref{SaveTo Action}).  If you happen to pass a filename, like
@code{SaveTo}, then @code{SaveTo} is called directly.  Else, the
user is prompted for a filename to save, and then @code{SaveTo} is
called with that filename.

%end-doc */

static int
Save (int argc, char **argv, Coord x, Coord y)
{
  char *function;
  char *name;
  char *prompt;

  static gchar *current_dir = NULL;

  if(!current_dir)
    current_dir = get_current_dir_name();

  if (argc > 1)
    return hid_actionv ("SaveTo", argc, argv);

  function = argc ? argv[0] : (char *)"Layout";

  if (strcasecmp (function, "Layout") == 0)
    if (PCB->Filename)
      return hid_actionl ("SaveTo", "Layout", PCB->Filename, NULL);

  if (strcasecmp (function, "PasteBuffer") == 0)
    prompt = _("Save element as");
  else
    prompt = _("Save layout as");
  
  name = ghid_dialog_file_select_save (prompt,
				       &current_dir,
				       PCB->Filename, Settings.FilePath);
  
  if (name)
    {
      if (Settings.verbose)
	fprintf (stderr, "%s:  Calling SaveTo(%s, %s)\n", 
		 __FUNCTION__, function, name);
      
      if (strcasecmp (function, "PasteBuffer") == 0)
	hid_actionl ("PasteBuffer", "Save", name, NULL);
      else
	{
	  /* 
	   * if we got this far and the function is Layout, then
	   * we really needed it to be a LayoutAs.  Otherwise 
	   * ActionSaveTo() will ignore the new file name we
	   * just obtained.
	   */
	  if (strcasecmp (function, "Layout") == 0)
	    hid_actionl ("SaveTo", "LayoutAs", name, NULL);
	  else
	    hid_actionl ("SaveTo", function, name, NULL);
	}
      g_free (name);
    }
  else
    {
      return 1;
    }

  return 0;
}

/* ---------------------------------------------------------------------- */
static const char swapsides_syntax[] =
"SwapSides(|v|h|r)";

static const char swapsides_help[] =
N_("Swaps the side of the board you're looking at.");

/* %start-doc actions SwapSides

This action changes the way you view the board.

@table @code

@item v
Flips the board over vertically (up/down).

@item h
Flips the board over horizontally (left/right), like flipping pages in
a book.

@item r
Rotates the board 180 degrees without changing sides.

@end table

If no argument is given, the board isn't moved but the opposite side
is shown.

Normally, this action changes which pads and silk layer are drawn as
true silk, and which are drawn as the "invisible" layer.  It also
determines which solder mask you see.

As a special case, if the layer group for the side you're looking at
is visible and currently active, and the layer group for the opposite
is not visible (i.e. disabled), then this action will also swap which
layer group is visible and active, effectively swapping the ``working
side'' of the board.

%end-doc */


static int
SwapSides (int argc, char **argv, Coord x, Coord y)
{
  int active_group = GetLayerGroupNumberByNumber (LayerStack[0]);
  int comp_group = GetLayerGroupNumberByNumber (component_silk_layer);
  int solder_group = GetLayerGroupNumberByNumber (solder_silk_layer);
  bool comp_on = LAYER_PTR (PCB->LayerGroups.Entries[comp_group][0])->On;
  bool solder_on = LAYER_PTR (PCB->LayerGroups.Entries[solder_group][0])->On;

  if (argc > 0)
    {
      switch (argv[0][0]) {
        case 'h':
        case 'H':
          ghid_flip_view (gport->pcb_x, gport->pcb_y, true, false);
          break;
        case 'v':
        case 'V':
          ghid_flip_view (gport->pcb_x, gport->pcb_y, false, true);
          break;
        case 'r':
        case 'R':
          ghid_flip_view (gport->pcb_x, gport->pcb_y, true, true);
          Settings.ShowSolderSide = !Settings.ShowSolderSide; /* Swapped back below */
          break;
        default:
          return 1;
      }
    }

  Settings.ShowSolderSide = !Settings.ShowSolderSide;

  if ((active_group == comp_group   && comp_on   && !solder_on) ||
      (active_group == solder_group && solder_on && !comp_on))
    {
      bool new_solder_vis = Settings.ShowSolderSide;

      ChangeGroupVisibility (PCB->LayerGroups.Entries[comp_group][0],
                             !new_solder_vis, !new_solder_vis);
      ChangeGroupVisibility (PCB->LayerGroups.Entries[solder_group][0],
                             new_solder_vis, new_solder_vis);
    }

  return 0;
}

/* ------------------------------------------------------------ */

static const char print_syntax[] =
"Print()";

static const char print_help[] =
N_("Print the layout.");

/* %start-doc actions Print

This will find the default printing HID, prompt the user for its
options, and print the layout.

%end-doc */

static int
Print (int argc, char **argv, Coord x, Coord y)
{
  HID **hids;
  int i;
  HID *printer = NULL;

  hids = hid_enumerate ();
  for (i = 0; hids[i]; i++)
    {
      if (hids[i]->printer)
	printer = hids[i];
    }

  if (printer == NULL)
    {
      gui->log (_("Can't find a suitable printer HID"));
      return -1;
    }

  /* check if layout is empty */
  if (!IsDataEmpty (PCB->Data))
    {
      ghid_dialog_print (printer);
    }
  else
    gui->log (_("Can't print empty layout"));

  return 0;
}


/* ------------------------------------------------------------ */

static HID_Attribute
printer_calibrate_attrs[] = {
  {N_("Enter Values here:"), "",
   HID_Label, 0, 0, {0, 0, 0}, 0, 0},
  {N_("x-calibration"), N_("X scale for calibrating your printer"),
   HID_Real, 0.5, 25, {0, 0, 1.00}, 0, 0},
  {N_("y-calibration"), N_("Y scale for calibrating your printer"),
   HID_Real, 0.5, 25, {0, 0, 1.00}, 0, 0}
};
static HID_Attr_Val printer_calibrate_values[3];

static const char printcalibrate_syntax[] =
"PrintCalibrate()";

static const char printcalibrate_help[] =
N_("Calibrate the printer.");

/* %start-doc actions PrintCalibrate

This will print a calibration page, which you would measure and type
the measurements in, so that future printouts will be more precise.

%end-doc */

static int
PrintCalibrate (int argc, char **argv, Coord x, Coord y)
{
  HID *printer = hid_find_printer ();
  printer->calibrate (0.0, 0.0);

  if (gui->attribute_dialog (printer_calibrate_attrs, 3,
			     printer_calibrate_values,
			     _("Printer Calibration Values"),
			     _("Enter calibration values for your printer")))
    return 1;
  printer->calibrate (printer_calibrate_values[1].real_value,
		      printer_calibrate_values[2].real_value);
  return 0;
}

/* ------------------------------------------------------------ */

static int
Export (int argc, char **argv, Coord x, Coord y)
{

  /* check if layout is empty */
  if (!IsDataEmpty (PCB->Data))
    {
      ghid_dialog_export ();
    }
  else
    gui->log (_("Can't export empty layout"));

  return 0;
}

/* ------------------------------------------------------------ */

static int
Benchmark (int argc, char **argv, Coord x, Coord y)
{
  int i = 0;
  time_t start, end;
  GdkDisplay *display;

  display = gdk_drawable_get_display (gport->drawable);

  gdk_display_sync (display);
  time (&start);
  do
    {
      ghid_invalidate_all ();
      gdk_window_process_updates (gtk_widget_get_window (gport->drawing_area),
                                  FALSE);
      time (&end);
      i++;
    }
  while (end - start < 10);

  printf (_("%g redraws per second\n"), i / 10.0);

  return 0;
}

/* ------------------------------------------------------------ */

static const char center_syntax[] =
"Center()\n";

static const char center_help[] =
N_("Moves the pointer to the center of the window.");

/* %start-doc actions Center

Move the pointer to the center of the window, but only if it's
currently within the window already.

%end-doc */

static int
Center(int argc, char **argv, Coord pcb_x, Coord pcb_y)
{
  GdkDisplay *display;
  GdkScreen *screen;
  int offset_x, offset_y;
  int widget_x, widget_y;
  int pointer_x, pointer_y;

  if (argc != 0)
    AFAIL (center);

  /* Aim to put the given x, y PCB coordinates in the center of the widget */
  widget_x = gport->width / 2;
  widget_y = gport->height / 2;

  ghid_pan_view_abs (pcb_x, pcb_y, widget_x, widget_y);

  /* Now move the mouse pointer to the place where the board location
   * actually ended up.
   *
   * XXX: Should only do this if we confirm we are inside our window?
   */

  ghid_pcb_to_event_coords (pcb_x, pcb_y, &widget_x, &widget_y);
  gdk_window_get_origin (gtk_widget_get_window (gport->drawing_area),
                         &offset_x, &offset_y);

  pointer_x = offset_x + widget_x;
  pointer_y = offset_y + widget_y;

  display = gdk_display_get_default ();
  screen = gdk_display_get_default_screen (display);
  gdk_display_warp_pointer (display, screen, pointer_x, pointer_y);

  return 0;
}

/* ------------------------------------------------------------ */
static const char cursor_syntax[] =
"Cursor(Type,DeltaUp,DeltaRight,Units)";

static const char cursor_help[] =
N_("Move the cursor.");

/* %start-doc actions Cursor

This action moves the mouse cursor.  Unlike other actions which take
coordinates, this action's coordinates are always relative to the
user's view of the board.  Thus, a positive @var{DeltaUp} may move the
cursor towards the board origin if the board is inverted.

Type is one of @samp{Pan} or @samp{Warp}.  @samp{Pan} causes the
viewport to move such that the crosshair is under the mouse cursor.
@samp{Warp} causes the mouse cursor to move to be above the crosshair.

@var{Units} can be one of the following:

@table @samp

@item mil
@itemx mm
The cursor is moved by that amount, in board units.

@item grid
The cursor is moved by that many grid points.

@item view
The values are percentages of the viewport's view.  Thus, a pan of
@samp{100} would scroll the viewport by exactly the width of the
current view.

@item board
The values are percentages of the board size.  Thus, a move of
@samp{50,50} moves you halfway across the board.

@end table

%end-doc */

static int
CursorAction(int argc, char **argv, Coord x, Coord y)
{
  UnitList extra_units_x = {
    { "grid",  PCB->Grid, 0 },
    { "view",  gport->view.width, UNIT_PERCENT },
    { "board", PCB->MaxWidth, UNIT_PERCENT },
    { "", 0, 0 }
  };
  UnitList extra_units_y = {
    { "grid",  PCB->Grid, 0 },
    { "view",  gport->view.height, UNIT_PERCENT },
    { "board", PCB->MaxHeight, UNIT_PERCENT },
    { "", 0, 0 }
  };
  int pan_warp = HID_SC_DO_NOTHING;
  double dx, dy;

  if (argc != 4)
    AFAIL (cursor);

  if (strcasecmp (argv[0], "pan") == 0)
    pan_warp = HID_SC_PAN_VIEWPORT;
  else if (strcasecmp (argv[0], "warp") == 0)
    pan_warp = HID_SC_WARP_POINTER;
  else
    AFAIL (cursor);

  dx = GetValueEx (argv[1], argv[3], NULL, extra_units_x, "");
  if (gport->view.flip_x)
    dx = -dx;
  dy = GetValueEx (argv[2], argv[3], NULL, extra_units_y, "");
  if (!gport->view.flip_y)
    dy = -dy;

  EventMoveCrosshair (Crosshair.X + dx, Crosshair.Y + dy);
  gui->set_crosshair (Crosshair.X, Crosshair.Y, pan_warp);

  return 0;
}
/* ------------------------------------------------------------ */

static const char dowindows_syntax[] =
"DoWindows(1|2|3|4|5|6)\n"
"DoWindows(Layout|Library|Log|Netlist|Preferences|DRC)";

static const char dowindows_help[] =
N_("Open various GUI windows.");

/* %start-doc actions DoWindows

@table @code

@item 1
@itemx Layout
Open the layout window.  Since the layout window is always shown
anyway, this has no effect.

@item 2
@itemx Library
Open the library window.

@item 3
@itemx Log
Open the log window.

@item 4
@itemx Netlist
Open the netlist window.

@item 5
@itemx Preferences
Open the preferences window.

@item 6
@itemx DRC
Open the DRC violations window.

@end table

%end-doc */

static int
DoWindows (int argc, char **argv, Coord x, Coord y)
{
  char *a = argc == 1 ? argv[0] : (char *)"";

  if (strcmp (a, "1") == 0 || strcasecmp (a, "Layout") == 0)
    {
    }
  else if (strcmp (a, "2") == 0 || strcasecmp (a, "Library") == 0)
    {
      ghid_library_window_show (gport, TRUE);
    }
  else if (strcmp (a, "3") == 0 || strcasecmp (a, "Log") == 0)
    {
      ghid_log_window_show (TRUE);
    }
  else if (strcmp (a, "4") == 0 || strcasecmp (a, "Netlist") == 0)
    {
      ghid_netlist_window_show (gport, TRUE);
    }
  else if (strcmp (a, "5") == 0 || strcasecmp (a, "Preferences") == 0)
    {
      ghid_config_window_show ();
    }
  else if (strcmp (a, "6") == 0 || strcasecmp (a, "DRC") == 0)
    {
      ghid_drc_window_show (TRUE);
    }
  else
    {
      AFAIL (dowindows);
    }

  return 0;
}

/* ------------------------------------------------------------ */
static const char setunits_syntax[] =
"SetUnits(mm|mil)";

static const char setunits_help[] =
N_("Set the default measurement units.");

/* %start-doc actions SetUnits

@table @code

@item mil
Sets the display units to mils (1/1000 inch).

@item mm
Sets the display units to millimeters.

@end table

%end-doc */

static int
SetUnits (int argc, char **argv, Coord x, Coord y)
{
  const Unit *new_unit;
  if (argc == 0)
    return 0;

  new_unit = get_unit_struct (argv[0]);
  if (new_unit != NULL && new_unit->allow != NO_PRINT)
    {
      Settings.grid_unit = new_unit;
      Settings.increments = get_increments_struct (Settings.grid_unit->suffix);
      AttributePut (PCB, "PCB::grid::unit", argv[0]);
    }

  ghid_config_handle_units_changed ();

  ghid_set_status_line_label ();

  /* FIXME ?
   * lesstif_sizes_reset ();
   * lesstif_styles_update_values ();
   */
  return 0;
}

/* ------------------------------------------------------------ */
static const char scroll_syntax[] =
"Scroll(up|down|left|right, [div])";

static const char scroll_help[] =
N_("Scroll the viewport.");

/* % start-doc actions Scroll

@item up|down|left|right
Specifies the direction to scroll

@item div
Optional.  Specifies how much to scroll by.  The viewport is scrolled
by 1/div of what is visible, so div = 1 scrolls a whole page. If not
default is given, div=40.

%end-doc */

static int
ScrollAction (int argc, char **argv, Coord x, Coord y)
{
  gdouble dx = 0.0, dy = 0.0;
  int div = 40;

  if (!ghidgui)
    return 0;

  if (argc != 1 && argc != 2)
    AFAIL (scroll);

  if (argc == 2)
    div = atoi(argv[1]);

  if (strcasecmp (argv[0], "up") == 0)
    dy = -gport->view.height / div;
  else if (strcasecmp (argv[0], "down") == 0)
    dy = gport->view.height / div;
  else if (strcasecmp (argv[0], "right") == 0)
    dx = gport->view.width / div;
  else if (strcasecmp (argv[0], "left") == 0)
    dx = -gport->view.width / div;
  else
    AFAIL (scroll);

  ghid_pan_view_rel (dx, dy);

  return 0;
}

/* ------------------------------------------------------------ */
static const char pan_syntax[] =
"Pan([thumb], Mode)";

static const char pan_help[] =
N_("Start or stop panning (Mode = 1 to start, 0 to stop)\n"
"Optional thumb argument is ignored for now in gtk hid.\n");

/* %start-doc actions Pan

Start or stop panning.  To start call with Mode = 1, to stop call with
Mode = 0.

%end-doc */

static int
PanAction (int argc, char **argv, Coord x, Coord y)
{
  int mode;

  if (!ghidgui)
    return 0;

  if (argc != 1 && argc != 2)
    AFAIL (pan);

  if (argc == 1)
    mode = atoi(argv[0]);
  else
    {
      mode = atoi(argv[1]);
      Message (_("The gtk gui currently ignores the optional first argument "
               "to the Pan action.\nFeel free to provide patches.\n"));
    }

  gport->panning = mode;

  return 0;
}

/* ------------------------------------------------------------ */
static const char popup_syntax[] =
"Popup(MenuName, [Button])";

static const char popup_help[] =
N_("Bring up the popup menu specified by @code{MenuName}.\n"
"If called by a mouse event then the mouse button number\n"
"must be specified as the optional second argument.");

/* %start-doc actions Popup

This just pops up the specified menu.  The menu must have been defined
as a named subresource of the Popups resource in the menu resource
file.

%end-doc */


static int
Popup (int argc, char **argv, Coord x, Coord y)
{
  GtkMenu *menu;

  if (argc != 1 && argc != 2)
    AFAIL (popup);

  menu = ghid_main_menu_get_popup (GHID_MAIN_MENU (ghidgui->menu_bar), argv[0]);
  if (! GTK_IS_MENU (menu))
    {
      Message (_("The specified popup menu \"%s\" has not been defined.\n"), argv[0]);
      return 1;
    }
  else
    {
      ghidgui->in_popup = TRUE;
      gtk_widget_grab_focus (ghid_port.drawing_area);
      gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 0, 
		      gtk_get_current_event_time());
    }
  return 0;
}
/* ------------------------------------------------------------ */
static const char importgui_syntax[] =
"ImportGUI()";

static const char importgui_help[] =
N_("Asks user which schematics to import into PCB.\n");

/* %start-doc actions ImportGUI

Asks user which schematics to import into PCB.

%end-doc */


static int
ImportGUI (int argc, char **argv, Coord x, Coord y)
{
    char *name = NULL;
    static gchar *current_layout_dir = NULL;
    static int I_am_recursing = 0; 
    int rv;

    if(!current_layout_dir)
      current_layout_dir = get_current_dir_name();

    if (I_am_recursing)
	return 1;


    name = ghid_dialog_file_select_open (_("Load schematics"),
					 &current_layout_dir,
					 Settings.FilePath);

#ifdef DEBUG
    printf("File selected = %s\n", name);
#endif

    AttributePut (PCB, "import::src0", name);
    free (name);

    I_am_recursing = 1;
    rv = hid_action ("Import");
    I_am_recursing = 0;

    return rv;
}

/* ------------------------------------------------------------ */
static int
Busy (int argc, char **argv, Coord x, Coord y)
{
  ghid_watch_cursor ();
  return 0;
}

HID_Action ghid_main_action_list[] = {
  {"About", 0, About, about_help, about_syntax},
  {"Benchmark", 0, Benchmark},
  {"Busy", 0, Busy},
  {"Center", N_("Click on a location to center"), Center, center_help, center_syntax},
  {"Command", 0, Command},
  {"Cursor", 0, CursorAction, cursor_help, cursor_syntax},
  {"DoWindows", 0, DoWindows, dowindows_help, dowindows_syntax},
  {"Export", 0, Export},
  {"GetXY", "", GetXY, getxy_help, getxy_syntax},
  {"ImportGUI", 0, ImportGUI, importgui_help, importgui_syntax},
  {"LayerGroupsChanged", 0, LayerGroupsChanged},
  {"LibraryChanged", 0, LibraryChanged},
  {"Load", 0, Load},
  {"Pan", 0, PanAction, pan_help, pan_syntax},
  {"PCBChanged", 0, PCBChanged},
  {"PointCursor", 0, PointCursor},
  {"Popup", 0, Popup, popup_help, popup_syntax},
  {"Print", 0, Print,
   print_help, print_syntax},
  {"PrintCalibrate", 0, PrintCalibrate,
   printcalibrate_help, printcalibrate_syntax},
  {"RouteStylesChanged", 0, RouteStylesChanged},
  {"Save", 0, Save, save_help, save_syntax},
  {"Scroll", N_("Click on a place to scroll"), ScrollAction, scroll_help, scroll_syntax},
  {"SetUnits", 0, SetUnits, setunits_help, setunits_syntax},
  {"SwapSides", 0, SwapSides, swapsides_help, swapsides_syntax},
  {"Zoom", N_("Click on zoom focus"), Zoom, zoom_help, zoom_syntax}
};

REGISTER_ACTIONS (ghid_main_action_list)


static int
flag_flipx (int x)
{
  return gport->view.flip_x;
}

static int
flag_flipy (int x)
{
  return gport->view.flip_y;
}

HID_Flag ghid_main_flag_list[] = {
  {"flip_x", flag_flipx, 0},
  {"flip_y", flag_flipy, 0}
};

REGISTER_FLAGS (ghid_main_flag_list)

#include "dolists.h"

/*
 * We will need these for finding the windows installation
 * directory.  Without that we can't find our fonts and
 * footprint libraries.
 */
#ifdef WIN32
#include <windows.h>
#include <winreg.h>
#endif

HID ghid_hid;

void
hid_gtk_init ()
{
#ifdef WIN32
  char * tmps;
  char * share_dir;
  char *loader_cache;
  FILE *loader_file;
#endif

#ifdef WIN32
  tmps = g_win32_get_package_installation_directory (PACKAGE "-" VERSION, NULL);
#define REST_OF_PATH G_DIR_SEPARATOR_S "share" G_DIR_SEPARATOR_S PACKAGE
#define REST_OF_CACHE G_DIR_SEPARATOR_S "loaders.cache"
  share_dir = (char *) malloc(strlen(tmps) + 
			  strlen(REST_OF_PATH) +
			  1);
  sprintf (share_dir, "%s%s", tmps, REST_OF_PATH);

  /* Point to our gdk-pixbuf loader cache.  */
  loader_cache = (char *) malloc (strlen (bindir) +
				  strlen (REST_OF_CACHE) +
				  1);
  sprintf (loader_cache, "%s%s", bindir, REST_OF_CACHE);
  loader_file = fopen (loader_cache, "r");
  if (loader_file)
    {
      fclose (loader_file);
      g_setenv ("GDK_PIXBUF_MODULE_FILE", loader_cache, TRUE);
    }

  free (tmps);
#undef REST_OF_PATH
  printf ("\"Share\" installation path is \"%s\"\n", share_dir);
#endif

  memset (&ghid_hid, 0, sizeof (HID));

  common_nogui_init (&ghid_hid);
  common_draw_helpers_init (&ghid_hid);

  ghid_hid.struct_size              = sizeof (HID);
  ghid_hid.name                     = "gtk";
  ghid_hid.description              = "Gtk - The Gimp Toolkit";
  ghid_hid.gui                      = 1;
  ghid_hid.poly_after               = 1;

  ghid_hid.get_export_options       = ghid_get_export_options;
  ghid_hid.do_export                = ghid_do_export;
  ghid_hid.parse_arguments          = ghid_parse_arguments;
  ghid_hid.invalidate_lr            = ghid_invalidate_lr;
  ghid_hid.invalidate_all           = ghid_invalidate_all;
  ghid_hid.notify_crosshair_change  = ghid_notify_crosshair_change;
  ghid_hid.notify_mark_change       = ghid_notify_mark_change;
  ghid_hid.set_layer                = ghid_set_layer;
  ghid_hid.make_gc                  = ghid_make_gc;
  ghid_hid.destroy_gc               = ghid_destroy_gc;
  ghid_hid.use_mask                 = ghid_use_mask;
  ghid_hid.set_color                = ghid_set_color;
  ghid_hid.set_line_cap             = ghid_set_line_cap;
  ghid_hid.set_line_width           = ghid_set_line_width;
  ghid_hid.set_draw_xor             = ghid_set_draw_xor;
  ghid_hid.draw_line                = ghid_draw_line;
  ghid_hid.draw_arc                 = ghid_draw_arc;
  ghid_hid.draw_rect                = ghid_draw_rect;
  ghid_hid.fill_circle              = ghid_fill_circle;
  ghid_hid.fill_polygon             = ghid_fill_polygon;
  ghid_hid.fill_rect                = ghid_fill_rect;

  ghid_hid.calibrate                = ghid_calibrate;
  ghid_hid.shift_is_pressed         = ghid_shift_is_pressed;
  ghid_hid.control_is_pressed       = ghid_control_is_pressed;
  ghid_hid.mod1_is_pressed          = ghid_mod1_is_pressed,
  ghid_hid.get_coords               = ghid_get_coords;
  ghid_hid.set_crosshair            = ghid_set_crosshair;
  ghid_hid.add_timer                = ghid_add_timer;
  ghid_hid.stop_timer               = ghid_stop_timer;
  ghid_hid.watch_file               = ghid_watch_file;
  ghid_hid.unwatch_file             = ghid_unwatch_file;
  ghid_hid.add_block_hook           = ghid_add_block_hook;
  ghid_hid.stop_block_hook          = ghid_stop_block_hook;

  ghid_hid.log                      = ghid_log;
  ghid_hid.logv                     = ghid_logv;
  ghid_hid.confirm_dialog           = ghid_confirm_dialog;
  ghid_hid.close_confirm_dialog     = ghid_close_confirm_dialog;
  ghid_hid.report_dialog            = ghid_report_dialog;
  ghid_hid.prompt_for               = ghid_prompt_for;
  ghid_hid.fileselect               = ghid_fileselect;
  ghid_hid.attribute_dialog         = ghid_attribute_dialog;
  ghid_hid.show_item                = ghid_show_item;
  ghid_hid.beep                     = ghid_beep;
  ghid_hid.progress                 = ghid_progress;
  ghid_hid.drc_gui                  = &ghid_drc_gui,
  ghid_hid.edit_attributes          = ghid_attributes;

  ghid_hid.request_debug_draw       = ghid_request_debug_draw;
  ghid_hid.flush_debug_draw         = ghid_flush_debug_draw;
  ghid_hid.finish_debug_draw        = ghid_finish_debug_draw;

  ghid_hid.notify_save_pcb          = ghid_notify_save_pcb;
  ghid_hid.notify_filename_changed  = ghid_notify_filename_changed;

  hid_register_hid (&ghid_hid);
#include "gtk_lists.h"
}