-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObsloc.c
269 lines (235 loc) · 9.73 KB
/
Obsloc.c
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
/*
Sunclock for Windows
Set Observer Location dialogue
*/
#include "sunclock.h"
static double currlat, currlon; // Working latitude and longitude
static char currsite[sizeof siteName]; // Working site name
static FILE *fp; // Site name database file
static int firstmod; // First explicit entry in latitude/longitude box
static int chasetail = TRUE; // Respond to changes in edit fields ?
/* DEGTODMS -- Convert fractional degrees to degrees, minutes, and seconds. */
static void degToDMS(double degrees, double *deg, double *min, double *sec)
{
*deg = (int) degrees;
*min = (int) ((degrees - *deg) * 60);
*sec = (degrees - (*deg + (*min / 60))) * 3600;
}
/* FILLLATLON -- Fill latitude and longitude edit fields from given values. */
static void fillLatLon(HWND hDlg, double lat, double lon)
{
double d, m, s;
char tbuf[20];
int sfm = firstmod;
firstmod = chasetail = FALSE;
degToDMS(abs(lat), &d, &m, &s);
#define Sf(value, field) sprintf(tbuf, "%.0f", value); SetDlgItemText(hDlg, field, (LPSTR) tbuf)
Sf(d, IDM_OL_LATDEG);
Sf(m, IDM_OL_LATMIN);
Sf(s, IDM_OL_LATSEC);
SendDlgItemMessage(hDlg, IDM_OL_NORS, CB_SETCURSEL, (lat >= 0) ? 0 : 1, 0L);
degToDMS(abs(lon), &d, &m, &s);
Sf(d, IDM_OL_LONDEG);
Sf(m, IDM_OL_LONMIN);
Sf(s, IDM_OL_LONSEC);
SendDlgItemMessage(hDlg, IDM_OL_EORW, CB_SETCURSEL, (lon >= 0) ? 1 : 0, 0L);
chasetail = TRUE;
firstmod = sfm;
}
/* UPDATELATLON -- Update latitude and longitude from edit fields. */
static void updateLatLon(HWND hDlg)
{
char tbuf[80];
#define Df(field) (GetDlgItemText(hDlg, field, (LPSTR) tbuf, (sizeof tbuf) - 1), atof(tbuf))
/* This excrescence of written-out code is thanks, as usual, to
the Monkey C (.NET) "optimiser", which screws up the following
expression, evaluting the dialogue fields in precisely ass-backward
order. */
// currlat = (Df(IDM_OL_LATDEG) + (Df(IDM_OL_LATMIN) / 60.0) + (Df(IDM_OL_LATSEC) / 3600.0))
// * ((SendDlgItemMessage(hDlg, IDM_OL_NORS, CB_GETCURSEL, 0, 0L) == 1) ? -1 : 1);
currlat = Df(IDM_OL_LATDEG);
currlat += Df(IDM_OL_LATMIN) / 60.0;
currlat += Df(IDM_OL_LATSEC) / 3600.0;
currlat *= (SendDlgItemMessage(hDlg, IDM_OL_NORS, CB_GETCURSEL, 0, 0L) == 1) ? -1 : 1;
// currlon = (Df(IDM_OL_LONDEG) + (Df(IDM_OL_LONMIN) / 60.0) + (Df(IDM_OL_LONSEC) / 3600.0))
// * ((SendDlgItemMessage(hDlg, IDM_OL_EORW, CB_GETCURSEL, 0, 0L) == 1) ? 1 : -1);
currlon = Df(IDM_OL_LONDEG);
currlon += Df(IDM_OL_LONMIN) / 60.0;
currlon += Df(IDM_OL_LONSEC) / 3600.0;
currlon *= (SendDlgItemMessage(hDlg, IDM_OL_EORW, CB_GETCURSEL, 0, 0L) == 1) ? 1 : -1;
/* If the user enters a custom latitude and longitude, the site name in the
combo box is now invalid. Clear it to encourage him to enter a site
name. */
if (firstmod) {
firstmod = FALSE;
strcpy(currsite, "");
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, WM_SETTEXT, 0, (LPARAM) (LPSTR) currsite);
}
}
/* SITEPICKED -- Fire up dialogue again after user picks site on map. */
void sitePicked(WORD px, WORD py, WORD sx, WORD sy)
{
DLGPROC lpfnMsgProc;
int nRc;
currlat = -(((double) py / sy) * 180 - 90);
currlon = -(((double) px / sx) * 360 - 180);
strcpy(currsite, "");
lpfnMsgProc = (DLGPROC) MakeProcInstance((FARPROC) SetObserverLocation, hInst);
nRc = DialogBox(hInst, MAKEINTRESOURCE(IDM_OBSLOC), hWndMain, lpfnMsgProc);
FreeProcInstance((FARPROC) lpfnMsgProc);
if (nRc) {
planetSiteChanged();
if (tracking) {
// Must update satellite position as seen from new site
updsat(faketime, FALSE, FALSE);
} else {
satviewchanged();
}
updateSky(faketime, TRUE);
}
}
/* SETOBSERVERLOCATION -- Main dialogue procedure. */
DialogueProc(SetObserverLocation)
{
char tbuf[80];
switch (message) {
case WM_INITDIALOG:
firstmod = FALSE;
if (sitePicking) {
sitePicking = FALSE;
mapCursor = LoadCursor(NULL, IDC_ARROW);
} else {
currlat = siteLat;
currlon = siteLon;
strcpy(currsite, siteName);
}
SendDlgItemMessage(hDlg, IDM_OL_NORS, CB_ADDSTRING, 0, (LPARAM) ((LPCSTR) rstring(IDS_NORTH)));
SendDlgItemMessage(hDlg, IDM_OL_NORS, CB_ADDSTRING, 0, (LPARAM) ((LPCSTR) rstring(IDS_SOUTH)));
SendDlgItemMessage(hDlg, IDM_OL_EORW, CB_ADDSTRING, 0, (LPARAM) ((LPCSTR) rstring(IDS_EAST)));
SendDlgItemMessage(hDlg, IDM_OL_EORW, CB_ADDSTRING, 0, (LPARAM) ((LPCSTR) rstring(IDS_WEST)));
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, CB_LIMITTEXT, 59, 0L);
fp = fopen("sitename.txt", "r");
if (fp != NULL) {
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, WM_SETREDRAW, FALSE, 0L);
while (fgets(tbuf, sizeof tbuf, fp)) {
if (tbuf[0] != ';' && tbuf[0] != ' ') {
tbuf[strlen(tbuf) - 1] = 0;
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, CB_ADDSTRING,
0, (LPARAM) (LPSTR) (tbuf + ((tbuf[17] == '+') ? 18 : 17)));
}
}
}
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, WM_SETTEXT,
0, (LPARAM) (LPSTR) currsite);
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, WM_SETREDRAW, TRUE, 0L);
fillLatLon(hDlg, currlat, currlon);
firstmod = TRUE; // Have first mod clear site name
return TRUE;
case WM_DESTROY:
if (fp != NULL) {
fclose(fp);
}
return TRUE;
case WM_COMMAND:
switch (WM_COMMAND_ID(wParam)) {
case IDOK:
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, WM_GETTEXT,
sizeof(currsite) - 1, (LPARAM) (LPCSTR) currsite);
if (strlen(currsite) > 0) {
siteLat = currlat;
siteLon = currlon;
strcpy(siteName, currsite);
EndDialog(hDlg, TRUE);
} else {
MessageBeep(MB_ICONQUESTION);
MessageBox(hDlg, rstring(IDS_NEED_SITENAME), NULL,
MB_ICONQUESTION | MB_OK);
}
return TRUE;
case IDCANCEL:
EndDialog(hDlg, FALSE);
return TRUE;
case IDM_OL_LATDEG:
case IDM_OL_LATMIN:
case IDM_OL_LATSEC:
case IDM_OL_LONDEG:
case IDM_OL_LONMIN:
case IDM_OL_LONSEC:
if (HIWORD(wParam) == EN_CHANGE && chasetail) {
updateLatLon(hDlg);
}
return TRUE;
case IDM_OL_NORS:
case IDM_OL_EORW:
if (HIWORD(wParam) == CBN_SELCHANGE && chasetail) {
updateLatLon(hDlg);
}
return TRUE;
case IDM_OL_HELP:
WinHelp(hWndMain, rstring(IDS_HELPFILE), HELP_KEY,
((DWORD) ((LPSTR) rstring(IDS_HELP_ON_OBSLOC))));
holped = TRUE;
return TRUE;
case IDM_OL_SITENAME:
if (HIWORD(wParam) == CBN_SELCHANGE || HIWORD(wParam) == CBN_DBLCLK) {
if (fp != NULL) {
int item = (int) SendDlgItemMessage(hDlg, IDM_OL_SITENAME,
CB_GETCURSEL, 0, 0L);
rewind(fp);
while (fgets(tbuf, sizeof tbuf, fp)) {
if (tbuf[0] != ';' && tbuf[0] != ' ') {
if (item-- <= 0) {
currlat = atol(tbuf) / 10000.0;
currlon = atol(tbuf + 8) / 10000.0;
fillLatLon(hDlg, currlat, currlon);
break;
}
}
}
}
}
return TRUE;
case IDM_OL_CLOSECITY:
if (fp != NULL) {
int item = 0, bitem;
double cl, cx, cy, cz, dx, dy, dz, dd, ilat, ilon, acl, bd = 2;
cl = fixangle(currlon);
cx = cos(dtr(cl)) * abs(cos(dtr(currlat)));
cy = sin(dtr(cl)) * abs(cos(dtr(currlat)));
cz = sin(dtr(currlat));
rewind(fp);
while (fgets(tbuf, sizeof tbuf, fp)) {
if (tbuf[0] != ';' && tbuf[0] != ' ') {
if (tbuf[17] == '+') { // Only consider "major cities"
ilat = dtr(atol(tbuf) / 10000.0);
ilon = dtr(fixangle(atol(tbuf + 8) / 10000.0));
acl = abs(cos(ilat));
dx = cx - cos(ilon) * acl;
dy = cy - sin(ilon) * acl;
dz = cz - sin(ilat);
if ((dd = (dx * dx + dy * dy + dz * dz)) < bd) {
bitem = item;
bd = dd;
}
}
item++;
}
}
SendDlgItemMessage(hDlg, IDM_OL_SITENAME, CB_SETCURSEL, (WPARAM) bitem, 0L);
}
return TRUE;
case IDM_OL_PICK:
sitePicking = TRUE;
if (xhairCursor == NULL) {
xhairCursor = LoadCursor(hInst, "xhairs");
}
mapCursor = xhairCursor; // Set cross-hair cursor
EndDialog(hDlg, FALSE); // Return to map window for pick
return TRUE;
default:
break;
}
break;
}
return FALSE;
}