-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapControl.cpp
More file actions
228 lines (190 loc) · 5.72 KB
/
MapControl.cpp
File metadata and controls
228 lines (190 loc) · 5.72 KB
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
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include "DownloadWorker.h"
#include "GdiPlusWrapper.h"
#include "MapControl.h"
#include "TileCache.h"
#include "TileDownloader.h"
#include "ViewportRenderer.h"
// Compatibility with VC++6
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A
#endif
#define IDM_COPY_LON_LAT 10001
std::map<HWND, ViewportRenderer*> renderers;
LonLat clickLonLat;
void putTextIntoClipboard(char* text);
LRESULT CALLBACK MapWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
LonLat* lonLat;
ViewportRenderer* viewportRenderer = NULL;
try {
if (message != WM_CREATE && !renderers.count(hWnd)) {
// should not happen
return DefWindowProc(hWnd, message, wParam, lParam);
} else {
viewportRenderer = renderers[hWnd];
}
switch (message) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_COPY_LON_LAT: {
char latLonTxt[256];
snprintf(latLonTxt, sizeof(latLonTxt), TEXT("%.8f %.8f"), clickLonLat.lat, clickLonLat.lon);
putTextIntoClipboard(latLonTxt);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
viewportRenderer = new ViewportRenderer(2, hWnd);
viewportRenderer->setCenterLonLat(13.377222, 52.526944);
renderers[hWnd] = viewportRenderer;
break;
case WM_SIZE:
viewportRenderer->setViewportSize(LOWORD(lParam), HIWORD(lParam));
break;
case WM_ERASEBKGND:
// reduce flickering
return TRUE;
case WM_PAINT: {
RECT updateRect;
bool hasUpdateRect = GetUpdateRect(hWnd, &updateRect, false);
hdc = BeginPaint(hWnd, &ps);
viewportRenderer->render(hdc, hasUpdateRect ? &updateRect : NULL);
EndPaint(hWnd, &ps);
break;
}
case WM_USER_TILE_READY:
hdc = BeginPaint(hWnd, &ps);
// TODO: render only tile(s) that have finished downloading
InvalidateRect(hWnd, NULL, FALSE);
EndPaint(hWnd, &ps);
break;
case WM_MOUSEMOVE:
if (viewportRenderer->mouseMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))) {
InvalidateRect(hWnd, NULL, FALSE);
}
SendMessage(GetParent(hWnd), WM_MAP_LONLAT_UPDATE, 0, lParam);
break;
case WM_MAP_GET_LONLAT:
lonLat = (LonLat*)wParam;
viewportRenderer->getLonLat(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &(lonLat->lon), &(lonLat->lat));
break;
case WM_LBUTTONDOWN:
viewportRenderer->startDragging(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
SetCapture(hWnd);
break;
case WM_LBUTTONUP:
viewportRenderer->endDragging(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
InvalidateRect(hWnd, NULL, FALSE);
ReleaseCapture();
break;
case WM_RBUTTONDOWN: {
viewportRenderer->getLonLat(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &clickLonLat.lon, &clickLonLat.lat);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, IDM_COPY_LON_LAT, "Copy coordinates to clipboard (Format: lat lon)");
POINT absolutePosition;
GetCursorPos(&absolutePosition);
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, absolutePosition.x, absolutePosition.y, 0, hWnd, NULL);
DestroyMenu(hMenu);
break;
}
case WM_MOUSEWHEEL:
// TODO: Pass position and adjust new center; coords may be shifted when widget is not at 0;0
if ((short)HIWORD(wParam) > 0) {
viewportRenderer->zoomIn();
} else {
viewportRenderer->zoomOut();
}
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_MAP_ZOOM_IN:
viewportRenderer->zoomIn();
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_MAP_ZOOM_OUT:
viewportRenderer->zoomOut();
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_MAP_MOVE_X:
viewportRenderer->setOffset(wParam, 0);
viewportRenderer->moveToOffset();
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_MAP_MOVE_Y:
viewportRenderer->setOffset(0, wParam);
viewportRenderer->moveToOffset();
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_DESTROY:
delete viewportRenderer;
renderers.erase(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} catch (char const* e) {
MessageBox(NULL, e, TEXT("winmapviewer"), MB_OK);
std::cerr << "Exception caught in map control window procedure: " << e << std::endl;
std::exit(EXIT_FAILURE);
}
return FALSE;
}
void RegisterMapControl(HINSTANCE hInstance) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)MapWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("MapControl");
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex)) {
throw "Error registering map control";
}
}
HWND CreateMapWindow(int x, int y, int width, int height, HWND hWnd, HINSTANCE hInstance) {
return CreateWindowEx(
0,
TEXT("MapControl"),
NULL,
WS_CHILD | WS_VISIBLE,
x,
y,
width,
height,
hWnd,
NULL,
hInstance,
NULL
);
}
void putTextIntoClipboard(char* text) {
if (!OpenClipboard(NULL)) {
return;
}
EmptyClipboard();
size_t size = strlen(text) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
if (hMem) {
char* pMem = (char*)GlobalLock(hMem);
if (pMem) {
memcpy(pMem, text, size);
GlobalUnlock(hMem);
SetClipboardData(CF_TEXT, hMem);
}
}
CloseClipboard();
}