File: note.c

package info (click to toggle)
xpostitplus 2.3.1-10
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 464 kB
  • ctags: 661
  • sloc: ansic: 4,831; makefile: 527; sh: 70
file content (2393 lines) | stat: -rw-r--r-- 50,002 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
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
#ifndef lint
static char	*RCSid = "$Id: note.c,v 2.1 1995/03/28 18:48:04 mjhammel Exp $";
#endif

/*
 * note.c - routines for handling notes.
 *
 * Originally by David A. Curry
 * SRI International
 * 333 Ravenswood Avenue
 * Menlo Park, CA 94025
 * davy@itstd.sri.com
 *
 * Modified by
 * Michael J. Hammel (03/01/95)
 * Contractor
 * 1150 Inca St. TH 70
 * Denver, CO 80204
 * mjhammel@csn.net
 *
 * $Log: note.c,v $
 * Revision 2.1  1995/03/28  18:48:04  mjhammel
 * changed options button to look like menu bar
 * changed horiz and vert distances of note form
 *
 * Revision 2.0  1995/03/27  18:56:22  mjhammel
 * Initial update to 2.0
 *
 *
 * Revision 1.4  90/06/14  11:20:09  davy
 * Ported to X11 Release 4.  Changed to get the note save position in an
 * ICCCM compliant (although kludgy) way.
 * 
 * Revision 1.3  89/01/10  09:57:24  davy
 * Changed XGetNormalHints to XGetWindowAttributes for getting the size of
 * the text window when saving.  XGetNormalHints comes up with the wrong
 * size, for some reason.
 * 
 * Revision 1.2  89/01/10  09:13:13  davy
 * Added XtChain... arguments to buttons and text window to prevent the
 * buttons from getting screwed up on a resize.
 * 
 * Revision 1.1  89/01/10  09:00:21  davy
 * Initial revision
 * 
 */
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/TextSrc.h>
#include <X11/Xaw/TextSink.h>
#include <X11/Xaw/AsciiSrc.h>
#include <X11/Xaw/AsciiSink.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeLine.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Shell.h>
#include <sys/param.h>
#ifdef USE_DIR
#include <sys/dir.h>
#else
#include <dirent.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>


#include "xpostit.h"
#include "alarmon.xbm"
#include "alarmoff.xbm"

/* external variables */
extern Widget	dialogwidget;
extern Widget 	error, errdialog;		/* error dialog box */

/* external routines */
extern void ClearName();

/* notes that are hidden */
Widget		notelist[DefaultMaxNotes];

/* the pixmap for when alarms are set */
Pixmap		alarmon_pixmap;
Pixmap		alarmoff_pixmap;

/* static PostItNote	*notes = NULL;
 */
PostItNote	*notes = NULL;

static int raisestatus = 1;
static int showstatus = 1;

extern void ErrPopDown();
extern void ErrPopUp();
extern void TimerPopDown();
extern void CreateTimerPrompt();
extern void CreateInsertPrompt();
extern void CreateIOPrompt();
extern void MakeNoteList();
extern void EmailNote();
extern void InsertText();

PostItNote		*FindNote();
static PostItNote	*AllocNote();

static void		SaveNote();
static void		OpenNote();
static void		ExportNote();
static void		PrintNote();
static void		EraseNote();
static void		DestroyNote();
static void		CancelErase();
static void		ConfirmErase();
static void		CancelDestroy();
static void		ConfirmDestroy();
static void		MakeNoteWidget();
static int		NoteIndex();
static void		HideNote();
static void		NameNote();
static void		AnchorNote();
static void		UnAnchorNote();
static void		InsertCalendar();
static void		InsertDates();
static void		ConfirmName();
static void		CancelName();
static void 		GetNotePosition();
static void 		SetTimerNote();
static void 		UnSetTimerNote();
static void 		MakeAlarmPopup();
static void 		AlarmPopDown();
static void 		EmailNoteCB();
static int		StringToNoteSize();

/*
 * CreateNewNote - create a new note of the specified size.
 */
void
CreateNewNote(size)
int size;
{
	static int hpi = 0;
	static int wpi = 0;
	register PostItNote *pn;
	register int hmm, wmm, hpixel, wpixel;

	/*
	 * Find out the number of pixels per inch on the screen.  We
	 * can get the number of pixels, and the size in millimeters.
	 * Then we convert to pixels/inch using the formula
	 *
	 *       2.54 cm     10 mm     pixels     pixels
	 *	--------- x ------- x -------- = --------
	 *	  inch        cm         mm        inch
	 *
	 * The only problem with this is that some servers (like Xsun)
	 * lie about what these numbers really are.
	 */
	if ((hpi == 0) || (wpi == 0)) {
		hpixel = DisplayHeight(display, DefaultScreen(display));
		wpixel = DisplayWidth(display, DefaultScreen(display));

		hmm = DisplayHeightMM(display, DefaultScreen(display));
		wmm = DisplayWidthMM(display, DefaultScreen(display));

		hpi = (int) ((25.4 * hpixel) / (float) hmm + 0.5);
		wpi = (int) ((25.4 * wpixel) / (float) wmm + 0.5);
	}

	/*
	 * Calculate sizes for the note.
	 */
	switch (size) {
	case PostItNote_1p5x2:
		hpixel = 1.5 * hpi;
		wpixel = 2 * wpi;
		break;
	case PostItNote_2x3:
		hpixel = 2 * hpi;
		wpixel = 3 * wpi;
		break;
	case PostItNote_3x3:
		hpixel = 3 * hpi;
		wpixel = 3 * wpi;
		break;
	case PostItNote_3x4:
		hpixel = 3 * hpi;
		wpixel = 4 * wpi;
		break;
	case PostItNote_3x5:
		hpixel = 3 * hpi;
		wpixel = 5 * wpi;
		break;
	case PostItNote_4x6:
		hpixel = 4 * hpi;
		wpixel = 6 * wpi;
		break;
	default:
		hpixel = 1.5 * hpi;
		wpixel = 2 * wpi;
		break;
	}

	/*
	 * Allocate a new note structure.
	 */
	pn = AllocNote(NewIndex);

	/*
	 * Set the text window size.
	 */
	pn->pn_textwidth = wpixel;
	pn->pn_textheight = hpixel;

	/*
	 * Make the widget for the note.
	 */
	MakeNoteWidget(pn);
}

/*
 * LoadSavedNotes - load in the notes the user has saved.
 */
void
LoadSavedNotes()
{
	DIR *dp;
	FILE *fp;
	register PostItNote *pn;
#ifdef USE_DIR
	register struct direct *d;
#else
	register struct dirent *d;
#endif
	char buf[BUFSIZ], fname[MAXPATHLEN];
	int n, len, nlen, shellx, shelly, texth, textw, hidden, anchor, namelen;
	int pnalarm, month, day, hour, minute;
	int save_screenx, save_screeny;
	int version;
	time_t		time_tp;
	struct tm	*tm_tp;

	/*
	 * Try to open the directory.
	 */
	if ((dp = opendir(app_res.note_dir)) == NULL)
	{
		fprintf(stderr, "xpostit: ");
		perror(app_res.note_dir);
		return;
	}

	nlen = strlen(PostItNoteFname);

	/*
	 * For each entry...
	 */
	while ((d = readdir(dp)) != NULL) {
		/*
		 * Skip over anything which doesn't match our
		 * file naming scheme.
		 */
		if (strncmp(d->d_name, PostItNoteFname, nlen) != 0)
			continue;

		/*
		 * Make the full path name.
		 */
		sprintf(fname, "%s/%s", app_res.note_dir, d->d_name);

		/*
		 * Open the file.
		 */
		if ((fp = fopen(fname, "r")) == NULL)
			continue;

		/*
		 * Look for the magic cookie identifying this as
		 * a Post-It note.
		 */
		if (fscanf(fp, "%s", buf) == EOF) {
			fclose(fp);
			continue;
		}
		/*
		 * I changed the magic cookie so it didn't conflict with
		 * the Postscript magic cookie, but we need to maintain
		 * compatibility, if requested.
		 */
		version = 2;
		if (strcmp(buf, PostItNoteMagic_v2) != 0) {
			if (app_res.compatibility) {
				if (strcmp(buf, PostItNoteMagic) != 0) {
					fclose(fp);
					continue;
				}
				else 
					version = 1;
			}
		}
		else 
			version = 2;

		/*
		 * Get the note position and size information.
		 */
		fgets(buf, sizeof(buf), fp);

		if ( version == 1 )
		{
			n = sscanf(buf, "%d %d %d %d %d %d %d %d", 
				&shellx, &shelly, &texth, &textw, &len, 
				&hidden, &save_screenx, &save_screeny);
			/*
			 * Bad format; skip it.
			 */
			if (n < 5) {
				fclose(fp);
				continue;
			}
			if (n == 8) {
				/*
				 * New format, got screen size when note was saved
				 */
				if (curr_screenx!=save_screenx || curr_screeny!=save_screeny) {
					/*
					 * Saved on different sized screen, adjust coordinates 
					 */
					shellx = (shellx * curr_screenx) / save_screenx;
					shelly = (shelly * curr_screeny) / save_screeny;
				}
			}
		}
		else
		{
			n = sscanf(buf, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d", 
				&shellx, &shelly, &texth, &textw, &len, 
				&hidden, &anchor, &save_screenx, &save_screeny,
				&pnalarm, &month, &day, &hour, &minute);
			/*
			 * Bad format; skip it.
			 */
			if (n < 14) {
				fclose(fp);
				continue;
			}
			if (curr_screenx!=save_screenx || curr_screeny!=save_screeny) {
				/*
				 * Saved on different sized screen, adjust coordinates 
				 */
				shellx = (shellx * curr_screenx) / save_screenx;
				shelly = (shelly * curr_screeny) / save_screeny;
			}
		}


		/*
		 * Get the index number of this note.
		 */
		n = atoi(&(d->d_name[nlen]));

		/*
		 * Get a note structure.
		 */
		pn = AllocNote(n);

		/*
		 * Get the name of the note
		 */
		fgets(buf, sizeof(buf), fp);
		namelen = strlen ( buf ) - 1;
		/* remove newline character */
		bcopy ( (void *)"\0", (void *)(buf+namelen), 1 );
		pn->pn_name = XtNewString ( buf );

		/*
		 * Set the information.
		 */
		pn->pn_shellx = shellx;
		pn->pn_shelly = shelly;
		pn->pn_textwidth = textw;
		pn->pn_textheight = texth;
		pn->pn_positionit = True;
		pn->pn_hidden = (Boolean)hidden;
		if ( version == 2 )
		{
			pn->pn_anchor = (Boolean)anchor;
			pn->pn_alarm = (Boolean)pnalarm;
			pn->pn_alarm_mon = month;
			pn->pn_alarm_day = day;
			pn->pn_alarm_hour = hour;
			pn->pn_alarm_min = minute;
		}
		else
		{
			pn->pn_anchor = False;
			pn->pn_alarm = False;
			time(&time_tp);
			tm_tp = localtime(&time_tp);
			pn->pn_alarm_mon = tm_tp->tm_mon;
			pn->pn_alarm_day = tm_tp->tm_mday;
			pn->pn_alarm_hour = tm_tp->tm_hour;
			pn->pn_alarm_min = tm_tp->tm_min;
		}

		/*
		 * Save the file name.
		 */
		pn->pn_file = SafeAlloc(strlen(fname) + 1);
		strcpy(pn->pn_file, fname);

		/*
		 * If we need a bigger buffer than the default,
		 * get one.
		 */
		if (len >= pn->pn_textsize) {
			n = (len + app_res.buf_size - 1) / app_res.buf_size;
			n = n * app_res.buf_size;

			if ((pn->pn_text = (char *)realloc(pn->pn_text, n)) == NULL) {
				fprintf(stderr, "xpostit: out of memory.\n");
				ByeBye();
			}

			pn->pn_textsize = n;
		}

		/*
		 * Read in the text.
		 */
		fread(pn->pn_text, sizeof(char), len, fp);
		fclose(fp);

		/*
		 * Make a widget for this note.
		 */
		MakeNoteWidget(pn);
	}

	closedir(dp);
}

/*
 * RaiseAllNotes - raise all the notes by raising their shell windows.
 */
void
RaiseAllNotes()
{
	register PostItNote *pn;
	Boolean found;

	found = False;
	raisestatus = 1;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		XRaiseWindow(display, XtWindow(pn->pn_shellwidget));
		found = True;
	}

	if ( !found )
		ErrPopUp("There are no notes to raise.");
}

/*
 * HideAllNotes - hide all the notes
 */
void
HideAllNotes()
{
	register PostItNote *pn;
	Boolean found;

	found = False;
	showstatus = 0;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		if (pn->pn_hidden == False)
		{
			XtPopdown ( pn->pn_shellwidget );
			pn->pn_hidden = True;
			found = True;
		}
	}

	if ( !found )
		ErrPopUp("There are no notes to hide.");
}

/*
 * UnHideAllNotes - unhide all hidden notes
 */
void
UnHideAllNotes()
{
	register PostItNote *pn;
	Boolean	found;

	found = False;
	showstatus = 1;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
		if ( pn->pn_hidden )
		{
			XtPopup(pn->pn_shellwidget, XtGrabNone);
			pn->pn_hidden = False;
			found = True;
		}

	if ( !found )
		ErrPopUp("There are no hidden notes to unhide.");
}

/*
 * LowerAllNotes - lower all the notes by lowering their shell windows.
 */
void
LowerAllNotes()
{
	register PostItNote *pn;
	Boolean found;

	found = False;
	raisestatus = 0;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		XLowerWindow(display, XtWindow(pn->pn_shellwidget));
		found = True;
	}

	if ( !found )
		ErrPopUp("There are no notes to lower.");
}

/*
 * ToggleRaise - toggle raise status
 */
void
ToggleRaise() {
	if(!showstatus) {
		UnHideAllNotes(); RaiseAllNotes();
	} else
		raisestatus ? LowerAllNotes() : RaiseAllNotes();
}

/*
 * ToggleShow - toggle show status
 */
void
ToggleShow() {
	showstatus ? HideAllNotes() : UnHideAllNotes();
}

/*
 * SaveAllNotes - save all the notes.
 */
void
SaveAllNotes(showmsg)
int	showmsg;
{
	register PostItNote *pn;
	Boolean found;

	found = False;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		SaveNote(pn->pn_shellwidget, (caddr_t) pn->pn_index, 0);
		found = True;
	}

	if ( showmsg )
		if ( !found )
			ErrPopUp("There are no notes to save.");
}

/*
 * MakeNewNote - called from the "tearoff" action.
 */
void
MakeNewNote(w, client_data, call_data)
XtPointer client_data, call_data;
Widget w;
{
	int notesize;

	if (call_data)
		notesize = StringToNoteSize((char *) call_data);
	else
		notesize = PostItNote_1p5x2;    /* arbitrary default */

	CreateNewNote(notesize);
}

/*
 * StringToNoteSize - convert strings to note sizes.
 */
static int
StringToNoteSize(s)
char *s;
{
	if (strcmp(s, "1.5x2") == 0)
		return(PostItNote_1p5x2);
	else if (strcmp(s, "2x3") == 0)
		return(PostItNote_2x3);
	else if (strcmp(s, "3x3") == 0)
		return(PostItNote_3x3);
	else if (strcmp(s, "3x4") == 0)
		return(PostItNote_3x4);
	else if (strcmp(s, "3x5") == 0)
		return(PostItNote_3x5);
	else if (strcmp(s, "4x6") == 0)
		return(PostItNote_4x6);
	else
		fprintf(stderr, "xpostit: bad note size: \"%s\".\n", s);

	return(PostItNote_1p5x2);
}

/*
 * CascadeNotes - cascade the notes on any anchor notes.
 * This isn't the best, but it distributes the notes among the notes
 * marked as anchors fairly evenly.
 */
void
CascadeNotes()
{
	register PostItNote *pn;
	PNLinks *linklist, *pnlink, *prevlink;
	int	nargs;
	Arg 	args[5];
	Dimension shellx, shelly;

	/*
	 * get a list of anchor notes
	 */
	linklist = pnlink = prevlink = NULL;
	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		if ( pn->pn_anchor )
		{
			pnlink = (PNLinks *)SafeAlloc(sizeof (PNLinks));
			pnlink->pn = pn;
			pnlink->next = NULL;
			nargs = 0;
			SetArg(XtNx, &shellx);
			SetArg(XtNy, &shelly);
			XtGetValues(pn->pn_shellwidget, args, nargs);
			pnlink->x = (int)shellx;
			pnlink->y = (int)shelly;
			if ( linklist == NULL )
				linklist = pnlink;
			if ( prevlink != NULL )
				prevlink->next = pnlink;
			prevlink = pnlink;
		}
	}

	/*
	 * Now run the list of notes and put any non-anchored notes over the
	 * anchored notes.
	 */
	if ( linklist != NULL )
	{
		pnlink = linklist;
		for (pn = notes; pn != NULL; pn = pn->pn_next)
		{
			if ( ( ! pn->pn_anchor ) && ( ! pn->pn_hidden ) )
			{
				/*
				 * reposition the non-anchor down and to
				 * the right of the last note.
				 */
				nargs = 0;
				SetArg(XtNx, pnlink->x+app_res.anchor_offset);
				SetArg(XtNy, pnlink->y+app_res.anchor_offset);
				XtSetValues(pn->pn_shellwidget, args, nargs);
				XRaiseWindow(display, XtWindow(pn->pn_shellwidget));

				/*
				 * reset the anchor position
				 */
				pnlink->x = pnlink->x + app_res.anchor_offset;
				pnlink->y = pnlink->y + app_res.anchor_offset;

				/*
				 * move to the next anchor
				 */
				if ( pnlink->next != NULL )
					pnlink = pnlink->next;
				else
					pnlink = linklist;
			}
		}
	}

	/*
	 * Clean up that storage
	 */
	if ( linklist != NULL )
	{
		for ( pnlink = linklist, prevlink = linklist; pnlink != NULL; 
			prevlink = pnlink->next, free(pnlink), pnlink = prevlink );
		linklist = NULL;
	}
	else 
		ErrPopUp("There are no anchored notes.");
}

void
SetSaveSensitive(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote	*pn;

	pn = (PostItNote *)client_data;

	XtSetSensitive ( pn->pn_savewidget, True );
}

/*
 * MakeNoteWidget - make a widget for a Post-It note.
 */
static void
MakeNoteWidget(pn)
PostItNote *pn;
{
	Arg 		args[19];
	char 		geometry[64];
	Widget 		form, widgets[25];
	XtCallbackRec	callbacks[2];
	register int 	nargs, nwidgets;
	Dimension	width, shellx, shelly;

	bzero(callbacks, sizeof(callbacks));
	nargs = 0;

	/*
	 * If the shell window coordinates are valid, use them.
	 */
	if (pn->pn_positionit) {
		sprintf(geometry, "%dx%d+%d+%d", 
				pn->pn_textwidth, pn->pn_textheight, pn->pn_shellx, pn->pn_shelly);
		SetArg(XtNgeometry, geometry);
		SetArg(XtNx, pn->pn_shellx);
		SetArg(XtNy, pn->pn_shelly);
	}

	/*
	 * Make the shell widget for this note.  We just use
	 * a pop-up shell for this.
	 */
	SetArg(XtNtitle, pn->pn_name);
	pn->pn_shellwidget = XtCreatePopupShell("PostItNote",
						wmShellWidgetClass, toplevel,
						args, nargs);

	/*
	 * Make a form widget.
	 */
	nargs=0;
	SetArg(XtNdefaultDistance, 0);
	form = XtCreateManagedWidget("Note", formWidgetClass, pn->pn_shellwidget,
			      args, nargs);

	nwidgets = -1;

	/*
	 * Put each button into the shell widget.  The second and third
	 * buttons will position themselves relative to the first one;
	 * the first one will position itself relative to the edge of
	 * the form.
	 */

	/* the label first */

#ifdef NOWM
	nargs = 0;
	SetArg(XtNlabel, pn->pn_name);
	SetArg(XtNwidth, pn->pn_textwidth);
	SetArg(XtNtop, XtChainTop);
	SetArg(XtNbottom, XtChainTop);
	SetArg(XtNleft, XtChainLeft);
	SetArg(XtNright, XtChainRight);
	pn->pn_labelwidget = widgets[++nwidgets] = 
		XtCreateManagedWidget("Label", labelWidgetClass, form, args, nargs);
#endif

	/* create the File button */
	nargs = 0;
	SetArg(XtNlabel, "File");
	SetArg(XtNtop, XtChainTop);
	SetArg(XtNbottom, XtChainTop);
	SetArg(XtNleft, XtChainLeft);
	SetArg(XtNright, XtChainLeft);
	SetArg(XtNborderWidth, 0);
	SetArg(XtNhighlightThickness, 0);
#ifdef NOWM
	SetArg(XtNfromVert, pn->pn_labelwidget);
#endif
	SetArg(XtNmenuName, PostItFileMenu );
	pn->pn_filebutton = widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"File", 
			menuButtonWidgetClass, 
			form, 
			args, nargs);


	/* create the options button
	 * In 2.3 this changed to a "Notes" button
	 */
	nargs = 0;
	SetArg(XtNlabel, "Notes");
	SetArg(XtNtop, XtChainTop);
	SetArg(XtNbottom, XtChainTop);
	SetArg(XtNleft, XtChainLeft);
	SetArg(XtNright, XtChainLeft);
	SetArg(XtNborderWidth, 0);
	SetArg(XtNhighlightThickness, 0);
#ifdef NOWM
	SetArg(XtNfromVert, pn->pn_labelwidget);
#endif
	SetArg(XtNfromHoriz, pn->pn_filebutton);
	SetArg(XtNmenuName, PostItOptionsMenu );
	pn->pn_options = widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"Options", 
			menuButtonWidgetClass, 
			form, 
			args, nargs);


	/* create the alarm label and its pixmaps */
	alarmon_pixmap = XCreateBitmapFromData (
			XtDisplay(toplevel),
			RootWindowOfScreen(XtScreen(toplevel)),
			alarmon_bits, alarmon_width, alarmon_height);
	alarmoff_pixmap = XCreateBitmapFromData (
			XtDisplay(toplevel),
			RootWindowOfScreen(XtScreen(toplevel)),
			alarmoff_bits, alarmoff_width, alarmoff_height);
	nargs = 0;
	if ( pn->pn_alarm )
	{
		SetArg(XtNbitmap, alarmon_pixmap);
	}
	else
	{
		SetArg(XtNbitmap, alarmoff_pixmap);
	}
	SetArg(XtNtop, XtChainTop);
	SetArg(XtNbottom, XtChainTop);
	SetArg(XtNleft, XtChainLeft);
	SetArg(XtNright, XtChainLeft);
	SetArg(XtNborderWidth, 0);
	SetArg(XtNhighlightThickness, 0);
#ifdef NOWM
	SetArg(XtNfromVert, pn->pn_labelwidget);
#endif
	SetArg(XtNfromHoriz, pn->pn_options);
	pn->pn_alarmwidget = widgets[++nwidgets] = 
		XtCreateManagedWidget(
				"AlarmIcon", 
				labelWidgetClass, 
				form, 
				args, nargs);

	/* hack - make the button a little wider since it won't span the
	 * width of the note
	 */
	nargs = 0;
	SetArg(XtNwidth, &width);
	XtGetValues(pn->pn_options, args, nargs);
	width+=15;
	nargs = 0;
	SetArg(XtNwidth, width);
	XtSetValues(pn->pn_options, args, nargs);

	/* now create the pop down menus */

	/* the Notes menu */
	nargs = 0;
	SetArg(XtNlabel, "Menu");
	pn->pn_optionsmenu = 
		XtCreatePopupShell(
			PostItOptionsMenu, 
			simpleMenuWidgetClass, 
			pn->pn_options, 
			args, nargs);
	
	/* add a line seperator */
	widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"OptionsLine", 
			smeLineObjectClass, 
			pn->pn_optionsmenu, 
			NULL, 0);
	
	/* the File menu */
	nargs = 0;
	SetArg(XtNlabel, "Menu");
	pn->pn_filemenu = 
		XtCreatePopupShell(
			PostItFileMenu, 
			simpleMenuWidgetClass, 
			pn->pn_filebutton, 
			args, nargs);
	
	/* add a line seperator */
	widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"FileLine", 
			smeLineObjectClass, 
			pn->pn_filemenu, 
			NULL, 0);
	

	/* all the menu buttons */

	SetCallback(SaveNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	pn->pn_savewidget = widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"Save", 
			smeBSBObjectClass, 
			pn->pn_filemenu, 
			args, nargs);

	SetCallback(OpenNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"Open", 
			smeBSBObjectClass, 
			pn->pn_filemenu, 
			args, nargs);

	SetCallback(ExportNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = 
		XtCreateManagedWidget(
			"Export", 
			smeBSBObjectClass, 
			pn->pn_filemenu, 
			args, nargs);


	SetCallback(PrintNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = XtCreateManagedWidget(
			"Print", 
			smeBSBObjectClass, 
			pn->pn_filemenu, 
			args, nargs);

	SetCallback(EmailNoteCB, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Email...");
	widgets[++nwidgets] = XtCreateManagedWidget(
			"EmailNote", 
			smeBSBObjectClass, 
			pn->pn_filemenu, 
			args, nargs);

	SetCallback(HideNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = XtCreateManagedWidget(
			"Hide", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	SetCallback(EraseNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = XtCreateManagedWidget(
			"Erase", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	widgets[++nwidgets] = XtCreateManagedWidget(
			"Line", 
			smeLineObjectClass,
			pn->pn_optionsmenu, 
			NULL, 0);

	SetCallback(DestroyNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	widgets[++nwidgets] = XtCreateManagedWidget(
			"Destroy", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	widgets[++nwidgets] = XtCreateManagedWidget(
			"Line", 
			smeLineObjectClass,
			pn->pn_optionsmenu, 
			NULL, 0);

	SetCallback(NameNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Name...");
	widgets[++nwidgets] = XtCreateManagedWidget(
			"Name", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	SetCallback(AnchorNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	pn->pn_anchorwidget = widgets[++nwidgets] = XtCreateManagedWidget(
			"Anchor", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	SetCallback(UnAnchorNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	pn->pn_unanchorwidget = widgets[++nwidgets] = XtCreateManagedWidget(
			"UnAnchor", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	if ( pn->pn_anchor )
	{
		XtSetSensitive ( pn->pn_unanchorwidget, True );
		XtSetSensitive ( pn->pn_anchorwidget, False );
	}
	else
	{
		XtSetSensitive ( pn->pn_unanchorwidget, False );
		XtSetSensitive ( pn->pn_anchorwidget, True );
	}

	SetCallback(SetTimerNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Set Alarm...");
	pn->pn_settimewidget = widgets[++nwidgets] = XtCreateManagedWidget(
			"SetAlarm", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	SetCallback(UnSetTimerNote, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Unset Alarm");
	pn->pn_unsettimewidget = widgets[++nwidgets] = XtCreateManagedWidget(
			"UnsetAlarm", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	if ( !app_res.noalarm )
	{
		if ( pn->pn_alarm )
		{
			XtSetSensitive ( pn->pn_unsettimewidget, True );
			XtSetSensitive ( pn->pn_settimewidget, False );
		}
		else
		{
			XtSetSensitive ( pn->pn_unsettimewidget, False );
			XtSetSensitive ( pn->pn_settimewidget, True );
		}
	}
	else
	{
		XtSetSensitive ( pn->pn_unsettimewidget, False );
		XtSetSensitive ( pn->pn_settimewidget, False );
	}

	SetCallback(InsertCalendar, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Insert Calendar");
	widgets[++nwidgets] = XtCreateManagedWidget(
			"InsertCalendar", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);

	SetCallback(InsertDates, pn->pn_index);

	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Insert Date...");
	widgets[++nwidgets] = XtCreateManagedWidget(
			"InsertDate", 
			smeBSBObjectClass,
			pn->pn_optionsmenu, 
			args, nargs);


	/*
	 * Create the text window.
	 */

	/* set its callback, for when a character has changed in the text */
	SetCallback(SetSaveSensitive, pn);

	nargs = 0;
	SetArg(XtNtop, XtChainTop);
	SetArg(XtNleft, XtChainLeft);
	SetArg(XtNright, XtChainRight);
	SetArg(XtNbottom, XtChainBottom);
	SetArg(XtNfromVert, pn->pn_options);
	SetArg(XtNstring, pn->pn_text);
	SetArg(XtNeditType, XawtextEdit);
	SetArg(XtNuseStringInPlace, True);
	SetArg(XtNlength, pn->pn_textsize);
	SetArg(XtNwidth, pn->pn_textwidth);
	SetArg(XtNheight, pn->pn_textheight);
	SetArg(XtNresize, XawtextResizeNever);
	SetArg(XtNhorizDistance, 0);
	SetArg(XtNvertDistance, 0);
	SetArg(XtNcallback, callbacks);

	if (app_res.scroll_bar) {
		SetArg(XtNscrollVertical, XawtextScrollAlways);
	}
	else {
		SetArg(XtNscrollVertical, XawtextScrollWhenNeeded);
	}

	widgets[++nwidgets] = XtCreateManagedWidget("NoteText", asciiTextWidgetClass,
					     form, args, nargs);

	/*
	 * Save the text widget.
	 */
	pn->pn_textwidget = widgets[nwidgets];

	/*
	 * Let the top level shell know all these guys are here.
	 */
	XtSetSensitive ( pn->pn_savewidget, False );

	/*
	 * Realize the note and pop it up.
	 */
	XtRealizeWidget(pn->pn_shellwidget);

	/*
	 * set the note shells coordinates in the data struct
	 */
	nargs = 0;
	SetArg ( XtNx, &shellx);
	SetArg ( XtNy, &shelly);
	XtGetValues ( pn->pn_shellwidget, args, nargs);
	pn->pn_shellx = (int) shellx;
	pn->pn_shelly = (int) shelly;

	/* don't display if its a hidden note */
	if ( pn->pn_hidden == False )
		XtPopup(pn->pn_shellwidget, XtGrabNone);
}

/*
 * Open a file (ie import it) to a note
 */
static void
OpenNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	char *path;
	Boolean	freeit;


	freeit = False;

	/*
	 * Find the note we're opening.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/* get configured or current directory, whichever applies */
	if ( app_res.home_dir == NULL )
	{
		if ( getenv("HOME") == NULL )
		{
			path = "/";
		}
		else
		{
			path = (char *)malloc (strlen(getenv("HOME")));
			strcpy(path, getenv("HOME"));
			freeit = True;
		}
	}
	else
	{
		path = (char *)malloc (strlen(app_res.home_dir));
		strcpy(path, app_res.home_dir);
			freeit = True;
	}

	/*
	 * Call CreateIOPrompt() to allow user to select or name a file
	 */
	CreateIOPrompt(pn, XPOSTIT_OPEN_FILE, path);

	if (freeit)
		free(path);
}


/*
 * Export a note to a file (ie some other file besides the notes file)
 */
static void
ExportNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	char *path;
	Boolean	freeit;

	freeit = False;

	/*
	 * Find the note we're exporting.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/* get configured or current directory, whichever applies */
	if ( app_res.home_dir == NULL )
	{
		if ( getenv("HOME") == NULL )
		{
			path = "/";
		}
		else
		{
			path = (char *)malloc (strlen(getenv("HOME")));
			strcpy(path, getenv("HOME"));
			freeit = True;
		}
	}
	else
	{
		path = (char *)malloc (strlen(app_res.home_dir));
		strcpy(path, app_res.home_dir);
			freeit = True;
	}

	/*
	 * Call CreateIOPrompt() to allow user to select or name a file
	 */
	CreateIOPrompt(pn, XPOSTIT_EXPORT_FILE, path);

	if (freeit)
		free(path);
}


/*
 * SaveNote - save a note to a file.
 * Use temporary files for saves in case the system crashes or xpostit
 * exits unexpectedly during a save.  This will preserve, at a minimum,
 * the last copy of the note.
 */
static void
SaveNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	FILE *fp;
	char *MakeFname();
	register PostItNote *pn;
	XWindowAttributes win_attributes;
	Arg args[5];
	int nargs;
	int len, shellx, shelly, texth, textw;
	Boolean saveit;
	Boolean sensitive;
	char	tmpfile1[512], tmpfile2[512];
	struct stat stat_buf;
	char	cmd[512];
	int       xchange,ychange;
	int       written = 0;

	/*
	 * Find the note we're saving.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/* by default, we're not going to save the note */
	saveit = False;

	/*
	 * if the notes text has changed, the sensitivity of the note
	 * save option will be True.
	 */
	nargs = 0;
	SetArg(XtNsensitive, &sensitive);
	XtGetValues(pn->pn_savewidget, args, nargs);
	if ( sensitive )
		saveit = True;

	/*
	 * Get the position of the shell window.
	 */
	GetNotePosition(pn->pn_shellwidget, &shellx, &shelly);

	/* KLUDGE KLUDGE The calculation made in GetNotePosition()
	   is suspect - we end up with notes drifting slowly around
	   the screen by pixels at a time. Instead of checking if
	   positions are identical to previously, check to see if
	   they've changed by >10 pixels. Should work for most
	   people. SAM 15/08/1999 */

	xchange=abs(shellx-pn->pn_shellx);
	ychange=abs(shelly-pn->pn_shelly);

	if ( ( xchange > 10 ) || ( ychange >10 ) )
	{
		pn->pn_shellx = shellx;
		pn->pn_shelly = shelly;
		saveit = True;
	}

	/*
	 * if nothing about the note has changed, don't waste effort saving it */
	if ( !saveit )
		return;

	/*
	 * If it doesn't have a file name, make one.
	 */
	if (pn->pn_file == NULL)
		pn->pn_file = MakeFname(pn->pn_index);


	/*
	 * Get the size of the text window.
	 */
	XGetWindowAttributes(display, XtWindow(pn->pn_shellwidget),
			     &win_attributes);
	textw = win_attributes.width;
	texth = win_attributes.height;

	/*
	 * Get the length of the text in the window.
	 */
	len = strlen(pn->pn_text);

	/*
	 * Create the temporary filenames.  These temporary files go in
	 * the same directory as the real notes to avoid problems with
	 * renaming files across file system boundaries, etc.
	 */
	sprintf(tmpfile1,"%s/xp1.%d", app_res.note_dir, (int)getpid());
	sprintf(tmpfile2,"%s/xp2.%d", app_res.note_dir, (int)getpid());

	/*
	 * Open the first temporary file for writing
	 */
	if ((fp = fopen(tmpfile1, "w")) == NULL) {
		fprintf(stderr, "xpostit: ");
		perror(tmpfile1);
		return;
	}

	/*
	 * Print out the information needed to recreate the note.
	 */
	fprintf(fp, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d \n%s\n", 
		PostItNoteMagic_v2, shellx, shelly, texth, textw, 
		len, pn->pn_hidden, pn->pn_anchor,
            	HeightOfScreen(XtScreen(pn->pn_shellwidget)),
            	WidthOfScreen(XtScreen(pn->pn_shellwidget)),
		pn->pn_alarm,
		pn->pn_alarm_mon, pn->pn_alarm_day, 
		pn->pn_alarm_hour, pn->pn_alarm_min, 
		pn->pn_name);

	/*
	 * Write out the text of the note.
	 */
	if (len)
	    written = fwrite(pn->pn_text, sizeof(char), len, fp);
	if(written != len)
	{
	    /* error - couldn't do save */
	    sprintf(cmd, "xpostit: %s", tmpfile1);
	    perror(cmd);
	    sprintf(cmd,"Couldn't write to temp file \n%s.\nSave of note failed.\n"
		  "%s may contain a valid copy of the note.", 
		  tmpfile1, pn->pn_file);
	    ErrPopUp(cmd);
	    return;
	}
	    
	
	fclose(fp);

	/* rename the old note (pn->pn_file, if it exists) to tmp2 */
	if ( stat(pn->pn_file, &stat_buf) == 0 )
	{
		if ( rename(pn->pn_file, tmpfile2) != 0 )
		{
			/* error - couldn't do save */
			sprintf(cmd, "xpostit: %s-%s", pn->pn_file, tmpfile2);
			perror(cmd);
			sprintf(cmd,"Couldn't rename %s\nto %s.\nSave of note failed.\n"
				"%s should contain valid copy of note.", 
				pn->pn_file, tmpfile2, tmpfile2);
			ErrPopUp(cmd);
			return;
		}

		/* check that rename worked (due to NFS problems) */
		if ( stat(tmpfile2, &stat_buf) != 0 )
		{
			/* rename didn't work! Goddam NFS.... */
			sprintf(cmd,"Rename %s to\n %s failed.\n"
				"Might be NFS problems.\nSave of note failed.", 
				pn->pn_file, tmpfile2);
			ErrPopUp(cmd);
			return;
		}
	}

	/* rename tmp1 note to old note */
	if ( rename(tmpfile1, pn->pn_file) != 0 )
	{
		/* error - couldn't do save - clean up */
		sprintf(cmd, "xpostit: %s-%s", tmpfile1, pn->pn_file);
		perror(cmd);
		sprintf(cmd,"Couldn't rename %s\nto %s.\n"
			"Save of note failed."
			"%s should contain valid copy of note.", 
			tmpfile1, pn->pn_file, tmpfile1);
		ErrPopUp(cmd);
		return;
	}

	/* check that rename worked (due to NFS problems) */
	if ( stat(pn->pn_file, &stat_buf) != 0 )
	{
		/* rename didn't work! Goddam NFS.... */
		sprintf(cmd,"Rename %s to\n %s failed.\n"
			"Might be NFS problems.\nSave of note failed.", 
			tmpfile1, pn->pn_file );
		ErrPopUp(cmd);
		return;
	}

	/* unlink tmp2 */
	if ( stat(tmpfile2, &stat_buf) == 0 )
	{
		if ( unlink(tmpfile2) != 0 )
		{
			sprintf(cmd,"Problems doing cleanup after save.\n"
				"%s could not be unlinked.\n", tmpfile2);
			ErrPopUp(cmd);
		}
	}

	/* reset save button to be insensitive */
	XtSetSensitive ( pn->pn_savewidget, False );
}

/*
 * EraseNote - erase all the text in a note.
 */
static void
EraseNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	XtCallbackRec cancel[2], confirm[2];

	bzero(confirm, sizeof(confirm));
	bzero(cancel, sizeof(cancel));

	/*
	 * Find the note we're erasing.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/*
	 * If there's nothing in the window, then there's
	 * no need to erase it.
	 */
	if (strlen(pn->pn_text) == 0)
		return;

	confirm[0].callback = ConfirmErase;
	confirm[0].closure = (caddr_t) pn->pn_index;
	cancel[0].callback = CancelErase;
	cancel[0].closure = (caddr_t) pn->pn_index;

	/*
	 * Get confirmation of what they want to do.
	 */
	ConfirmIt(confirm, cancel);
}

/*
 * DestroyNote - destroy a note.
 */
static void
DestroyNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	XtCallbackRec cancel[2], confirm[2];

	bzero(confirm, sizeof(confirm));
	bzero(cancel, sizeof(cancel));

	/*
	 * Find the note we're destroying.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	confirm[0].callback = ConfirmDestroy;
	confirm[0].closure = (caddr_t) pn->pn_index;
	cancel[0].callback = CancelDestroy;
	cancel[0].closure = (caddr_t) pn->pn_index;

	/*
	 * Get confirmation of what they want to do.
	 */
	ConfirmIt(confirm, cancel);
}

/*
 * PrintNote - prints a note.
 */
static void
PrintNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	FILE	*fp;
	int	len;
	char	tmpfile[512];
	char	cmd[1024];
	int       written = 0;

#ifdef DEBUG
	int	rc;
#endif

	/*
	 * Find the note we're printing.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/*
	 * Save the text to a temporary file
	 */
	sprintf(tmpfile,"%s/xp%d", app_res.tmp_dir, (int)getpid());
	if ((fp = fopen(tmpfile, "w")) == NULL) {
		fprintf(stderr, "xpostit: ");
		perror(tmpfile);
		sprintf(cmd,"Can't open temporary file for\nwriting: %s", tmpfile);
		ErrPopUp(cmd);
		return;
	}
	len = strlen(pn->pn_text);
	if (len)
	{
	    written = fwrite(pn->pn_text, sizeof(char), len, fp);
	    if(written != len)
	    {
	        /* error - couldn't do save */
	        sprintf(cmd, "xpostit: %s", tmpfile);
	        perror(cmd);
	        sprintf(cmd,"Couldn't write to temp file \n%s.\nPrint of note failed.\n",
		      tmpfile);
	        ErrPopUp(cmd);
	        return;
	    }
	}
	else
	{
		ErrPopUp("No text in note to print!");
		return;
	}
	fclose(fp);

	/*
	 * Use the printcmd resource to determine how to print the note
	 */
	sprintf (cmd, app_res.print_cmd, tmpfile );
#ifdef DEBUG
	printf("Command used to print the note: %s\n", cmd);
	rc = system ( cmd );
	switch ( rc )
	{
		case 127:
			sprintf(cmd,"%s failed: errno=%d", cmd, errno);
			ErrPopUp(cmd);
			break;
			
		case -1:
			sprintf(cmd,"%s failed: errno=%d", cmd, rc);
			ErrPopUp(cmd);
			break;
			
		default:
			sprintf(cmd,"%s succeeded: rc=%d", cmd, rc);
			ErrPopUp(cmd);
			break;
	}
#else
	system ( cmd );
#endif

	/*
	 * clean up the temporary file
	 */
	sprintf (cmd, "rm %s", tmpfile );
	system ( cmd );

}


/*
 * EmailNoteCB - email a note.
 */
static void
EmailNoteCB(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;

	/*
	 * Find the note we're emailing.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	CreateEmailNotePrompt(pn);
}

/*
 * NameNote - name a note.
 */
static void
NameNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	XtCallbackRec cancel[2], confirm[2];

	bzero(confirm, sizeof(confirm));
	bzero(cancel, sizeof(cancel));

	/*
	 * Find the note we're naming.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	confirm[0].callback = ConfirmName;
	confirm[0].closure = (caddr_t) pn->pn_index;
	cancel[0].callback = CancelName;
	cancel[0].closure = (caddr_t) pn->pn_index;

	/*
	 * Get the title the user wants for this postitnote
	 */
	NameIt(pn, confirm, cancel);
}

/*
 * AnchorNote - name a note.
 */
static void
AnchorNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;

	/*
	 * Find the note we're naming.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	pn->pn_anchor = True;
	XtSetSensitive ( pn->pn_unanchorwidget, True );
	XtSetSensitive ( pn->pn_anchorwidget, False );
	XtSetSensitive ( pn->pn_savewidget, True );
}

/*
 * UnAnchorNote - name a note.
 */
static void
UnAnchorNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;

	/*
	 * Find the note we're naming.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	pn->pn_anchor = False;
	XtSetSensitive ( pn->pn_unanchorwidget, False );
	XtSetSensitive ( pn->pn_anchorwidget, True );
	XtSetSensitive ( pn->pn_savewidget, True );
}

/*
 * HideNote - hide a note.
 */
static void
HideNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn;

	/*
	 * Find the note we're hiding.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/* mark it hidden */
	pn->pn_hidden = True;

	/*
	 * pop the window down
	 */
	XtPopdown ( pn->pn_shellwidget );
}

/*
 * MakeNoteListCB - create a list of the notes for the
 * user to choose from so a particular note can be
 * popped up at the current mouse pointer location.
 * This is really just a front end to MakeNoteList
 * so we can pass the notes pointer.
 */
void
MakeNoteListCB(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	CreateFindNotePrompt(notes, False);
}

/*
 * SetTimerNote - callback for setting the alarm for a note
 */
static void
SetTimerNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn;

	/*
	 * Find the note we're hiding.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	CreateTimerPrompt(pn);

}

/*
 * UnSetTimerNote - callback for removing the alarm for a note
 */
static void
UnSetTimerNote(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn;
	Arg			args[5];
	int			nargs;

	/*
	 * Find the note we're hiding.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	XtSetSensitive ( pn->pn_unsettimewidget, False );
	XtSetSensitive ( pn->pn_settimewidget, True );
	XtSetSensitive ( pn->pn_savewidget, True );

	/*
	 * Turn off the alarms settings
	 */
	pn->pn_alarm = False;

	/* turn off the alarm pixmap */
	nargs = 0;
	SetArg(XtNbitmap, alarmoff_pixmap);
	XtSetValues(pn->pn_alarmwidget, args, nargs);
}

/*
 * InsertCalendar - insert this months calendar into the note
 */
static void
InsertCalendar(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote *pn;
	char	tmpfile[512];
	char	cmd[1024];
	int	rc;

	/*
	 * Find the note we're adding the calender to.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/*
	 * Run the calendar generating program to get the text.
	 * We use system() instead of popen() because the latter
	 * doesn't reliably tell us if the process actually ran.
	 * system() will provide the return value of the invoked
	 * program.
	 */
	sprintf(tmpfile,"%s/xp%d", app_res.tmp_dir, (int)getpid());
	sprintf (cmd, "%s > %s", app_res.calendar_cmd, tmpfile );
	if ( (rc=system ( cmd )) != 0 ) {

		sprintf(tmpfile,"Couldn't run %s: return code = %d", 
			app_res.calendar_cmd, rc);
		ErrPopUp(tmpfile);
		/* cleanup, if necessary */
		sprintf (cmd, "rm %s", tmpfile );
		system ( cmd );
		return;
	}

	/* stuff the output into the note */
	InsertText(pn, tmpfile, True, FILE_TYPE);

	XtSetSensitive ( pn->pn_savewidget, True );
}


/*
 * InsertDates - callback for inserting a formatted date in the note
 */
static void
InsertDates(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn;

	/*
	 * Find the note we're hiding.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	CreateInsertPrompt(pn);

}

/*
 * ConfirmName - callback for name confirmation.
 */
static void
ConfirmName(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	Arg 			args[4];
	register int		nargs;
	register PostItNote	*pn;
	String			string;

	/*
	 * Get rid of the confirmation box.
	 */
	ClearName();

	/*
	 * Find the note we're naming.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/* get the name of the shell widget */
	string = XawDialogGetValueString ( dialogwidget );

	/* don't change the name if no text was entered */
	if ( strcmp ( string, "" ) != 0 )
	{
#ifdef NOWM
		nargs = 0;
		SetArg(XtNlabel, string );
		XtSetValues(pn->pn_labelwidget, args, nargs);
#endif
		nargs = 0;
		SetArg(XtNtitle, string );
		XtSetValues(pn->pn_shellwidget, args, nargs);

		/* I should just copy the name, but I'm not clear on
		 * how you do this with X Strings - mjh
		 */
		if ( pn->pn_name != NULL )
			XtFree ( pn->pn_name );
		pn->pn_name = XtNewString ( string );
		XtSetSensitive ( pn->pn_savewidget, True );
	}
}


/*
 * CancelName - callback for name cancellation.
 */
static void
CancelName(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	/*
	 * Get rid of the confirmation box.
	 */
	ClearName();
}


/*
 * ConfirmErase - callback for erase confirmation.
 */
static void
ConfirmErase(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	Arg args[4];
	register int nargs;
	register PostItNote *pn;

	/*
	 * Get rid of the confirmation box.
	 */
	ClearConfirm();

	/*
	 * Find the note we're erasing.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/*
	 * This is a kludge.  They should have provided
	 * an XtTextErase function.
	 */
	bzero(pn->pn_text, pn->pn_textsize);

	nargs = 0;
	SetArg(XtNstring, pn->pn_text);
	XtSetValues(pn->pn_textwidget, args, nargs);
	XtSetSensitive ( pn->pn_savewidget, True );
}

/*
 * CancelErase - callback for erase cancellation.
 */
static void
CancelErase(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	/*
	 * Get rid of the confirmation box.
	 */
	ClearConfirm();
}

/*
 * ConfirmDestroy - callback for destroy confirmation.
 */
static void
ConfirmDestroy(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	register PostItNote *pn, *prev;

	/*
	 * Get rid of the confirmation box.
	 */
	ClearConfirm();

	/*
	 * Find the note we're destroying.
	 */
	if ((pn = FindNote((int) client_data)) == NULL)
		return;

	/*
	 * Get rid of the widgets for this note.
	 */
	XtPopdown(pn->pn_shellwidget);
	XtDestroyWidget(pn->pn_shellwidget);

	/*
	 * Get rid of the note structure.
	 */
	if (pn != notes) {

		/*
		 * first, free up the name buffer
		 */
		if ( pn->pn_name != NULL )
			XtFree ( pn->pn_name );

		for (prev = notes; prev->pn_next; prev = prev->pn_next) {
			if (prev->pn_next == pn)
				break;
		}

		prev->pn_next = pn->pn_next;
	}
	else {
		notes = pn->pn_next;
	}

	/*
	 * Get rid of the file.
	 */
	if (pn->pn_file) {
		unlink(pn->pn_file);
		free(pn->pn_file);
	}

	/*
	 * Free the memory we used.
	 */
	free(pn->pn_text);
	free(pn);
}

/*
 * CancelDestroy - callback for destroy cancellation.
 */
static void
CancelDestroy(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	/*
	 * Get rid of the confirmation box.
	 */
	ClearConfirm();
}

/*
 * AllocNote - allocate a new note structure and insert in into the
 *	       list of notes.
 */
static PostItNote *
AllocNote(index)
int index;
{
	register PostItNote	*pn;
	char			label[15];		
	time_t			time_tp;
	struct tm		*tm_tp;

	/*
	 * Allocate a structure.
	 */
	if (notes == NULL) {
		notes = (PostItNote *) SafeAlloc(sizeof(PostItNote));
		pn = notes;
	}
	else {
		for (pn = notes; pn->pn_next != NULL; pn = pn->pn_next)
			;

		pn->pn_next = (PostItNote *) SafeAlloc(sizeof(PostItNote));
		pn = pn->pn_next;
	}

	/*
	 * Initialize the note.
	 */
	pn->pn_positionit = False;
	pn->pn_hidden = False;
	pn->pn_anchor = False;
	pn->pn_alarm = False;

	/* set todays date in the alarm entries */
	time(&time_tp);
	tm_tp = localtime(&time_tp);
	pn->pn_alarm_mon = tm_tp->tm_mon;
	pn->pn_alarm_day = tm_tp->tm_mday;
	pn->pn_alarm_hour = tm_tp->tm_hour;
	pn->pn_alarm_min = tm_tp->tm_min;

	pn->pn_hide_index = -1;
	pn->pn_textsize = app_res.buf_size;
	pn->pn_text = SafeAlloc(pn->pn_textsize);

	/*
	 * If the index number was given, use it.  Otherwise,
	 * get a new index number.
	 */
	pn->pn_index = (index == NewIndex ? NoteIndex() : index);

	/* save the name of the note */
	sprintf (label, "Note %d", pn->pn_index);
	pn->pn_name = XtNewString ( label );

	return(pn);
}

/*
 * FindNote - find the note structure with the given index number.
 */
PostItNote *
FindNote(index)
register int index;
{
	register PostItNote *pn;

	for (pn = notes; pn != NULL; pn = pn->pn_next) {
		if (pn->pn_index == index)
			return(pn);
	}

	return(NULL);
}

/*
 * NoteIndex - find the lowest free index number.
 */
static int
NoteIndex()
{
	register int index;
	register PostItNote *pn;

	/*
	 * This is O(n**2), but the list should be small.
	 */
	for (index = 1; ; index++) {
		if ((pn = FindNote(index)) == NULL)
			return(index);
	}
}

/*
 * GetNotePosition - find the position of the widget window, taking into
 *		     account any borders stuck on by reparenting window
 *		     managers.
 *
 *		     This is a KLUDGE.  The ICCCM does not specify a way
 *		     for a client to find out what kind of border stuff
 *		     the window manager has added.
 *
 *		     Thanks to Stewart Levin for the original code.
 */
static void
GetNotePosition(w, x, y)
int *x, *y;
Widget w;
{
	Window *children;
	int status, nchildren;
	Window here, parent, root;
	XWindowAttributes win_attributes;

	parent = XtWindow(w);

	/*
	 * Walk up the tree looking for a parent of this window whose
	 * parent is the root.  That'll either be this window (if there
	 * is no window manager window) or the window added by the
	 * window manager.
	 */
	do {
		here = parent;

		status = XQueryTree(display, here, &root, &parent, &children,
				    &nchildren);

		if (!status)
			break;

		if (children)
			XFree(children);
	} while (parent != root);

	/*
	 * Get the attributes of this window we just found.
	 */
	XGetWindowAttributes(display, here, &win_attributes);

	/*
	 * Now deduct the border from the position of the window.
	 * We know the coordinates don't need translating, since
	 * this window's parent is the root.
	 */
	*x = win_attributes.x - win_attributes.border_width;
	*y = win_attributes.y - win_attributes.border_width;
}


/*
 * Check the notes for any alarms that need to go off
 */
void
AlarmCheck(client_data, id)
XtPointer client_data;
XtIntervalId	*id;
{
	register PostItNote *pn;
	XtIntervalId	alarm_timer;
	time_t		time_tp;
	struct tm	*tm_tp;

	for (pn = notes; pn != NULL; pn = pn->pn_next)
	{
		if (pn->pn_alarm)
		{
			time(&time_tp);
			tm_tp = localtime(&time_tp);
			if ( (pn->pn_alarm_mon == tm_tp->tm_mon) &&
			     (pn->pn_alarm_day == tm_tp->tm_mday) &&
			     (pn->pn_alarm_hour == tm_tp->tm_hour) &&
			     (pn->pn_alarm_min == tm_tp->tm_min) )
			{
				MakeAlarmPopup(pn);
			}
		}
	}
	alarm_timer = XtAppAddTimeOut (
			XtWidgetToApplicationContext(toplevel),
			alarm_interval,
			AlarmCheck,
			NULL );
}

/*
 * Create popups for alarms so user knows the alarm has expired.
 */
static void
MakeAlarmPopup(pn)
PostItNote *pn;
{
	int		i;
	Arg		args[4];
	int		nargs;
	Widget		alarmshell, alarmdialog, alarmconfirm;
	Window 		root, child;
	unsigned int 	buttons;
	int 		root_x, root_y, child_x, child_y;
	char		string[512];
	XtCallbackRec	callbacks[2];

	/*
	 * Create a popup shell widget
	 */
	nargs = 0;
	SetArg(XtNtitle, PostItAlarmDialog);
	alarmshell = XtCreatePopupShell(
			"AlarmShell", 
			transientShellWidgetClass,
			toplevel, 
			args, nargs);

	/*
	 * Create the alarm dialog box
	 */
	alarmdialog = XtCreateManagedWidget(
				"alarmDialog",
				dialogWidgetClass,
				alarmshell,
				NULL, 0);

	/*
	 * Create the alarm dialog confirmation button
	 */
	bzero(callbacks, sizeof(callbacks));
	SetCallback(AlarmPopDown, (XtPointer)alarmshell);
	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "OK");
	alarmconfirm = XtCreateManagedWidget(
				"alarmconfirm",
				commandWidgetClass,
				alarmdialog,
				args, nargs);

	/* get mouse location */
	XQueryPointer(display, XtWindow(toplevel), &root, &child,
		      &root_x, &root_y, &child_x, &child_y, &buttons);

	sprintf(string,
		"The alarm has expired for the PostIt Note\n"
		"with the following name:\n"
		"\"%s\"", pn->pn_name);

	nargs = 0;
	SetArg(XtNlabel, string);
	XtSetValues(alarmdialog, args, nargs);

	nargs = 0;
	SetArg(XtNx, root_x);
	SetArg(XtNy, root_y);
	XtSetValues(alarmshell, args, nargs);
	XtPopup( alarmshell, XtGrabNonexclusive );

	/* double-ring bell 3 times */
	for (i=0; i<3; i++)
		XBell(XtDisplay(toplevel), 80);
}

/*
 * Clean up the alarm pop up windows
 */
static void
AlarmPopDown(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	/* pop down shell */
	XtPopdown( (Widget)client_data );

	/* destroy shell */
	XtDestroyWidget((Widget)client_data);
}