-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchDialog.cpp
More file actions
207 lines (166 loc) · 6.09 KB
/
SearchDialog.cpp
File metadata and controls
207 lines (166 loc) · 6.09 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
// windows.h is required to be included *before* commctrl.h
// clang-format off
#include <windows.h>
#include <commctrl.h>
// clang-format on
#include <iostream>
#include <sstream>
#include <string>
#include <windowsx.h>
#include "Common.h"
#include "SearchDialog.h"
#include "SearchProvider.h"
#include "resource.h"
#define IDM_OPEN_IN_OSM 10002
HWND hMainWindow = NULL;
SearchProvider* searchProvider = NULL;
// TODO: ownership? initialization?
std::vector<SearchResult> searchResult;
SearchResult clickedSearchResult;
void updateResultList(HWND hListView) {
int i = 0;
for (std::vector<SearchResult>::iterator it = searchResult.begin(); it != searchResult.end(); ++it) {
LVITEMW entry = {0};
entry.mask = LVIF_TEXT;
entry.iItem = i++;
entry.iSubItem = 0;
entry.pszText = const_cast<wchar_t*>(it->m_displayName.c_str());
SendMessageW(hListView, LVM_INSERTITEMW, 0, reinterpret_cast<LPARAM>(&entry));
std::wstringstream latText;
latText << it->m_lonLat.lat;
entry.iSubItem = 1;
entry.pszText = const_cast<wchar_t*>(latText.str().c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
std::wstringstream lonText;
lonText << it->m_lonLat.lon;
entry.iSubItem = 2;
entry.pszText = const_cast<wchar_t*>(lonText.str().c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
entry.iSubItem = 3;
entry.pszText = const_cast<wchar_t*>(it->m_class.c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
entry.iSubItem = 4;
entry.pszText = const_cast<wchar_t*>(it->m_type.c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
entry.iSubItem = 5;
entry.pszText = const_cast<wchar_t*>(it->m_osmType.c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
entry.iSubItem = 6;
entry.pszText = const_cast<wchar_t*>(it->m_osmId.c_str());
SendMessageW(hListView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&entry));
}
if (i > 0) {
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 0, 350);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 2, LVSCW_AUTOSIZE);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 3, LVSCW_AUTOSIZE);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 4, LVSCW_AUTOSIZE);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 5, LVSCW_AUTOSIZE);
SendMessageW(hListView, LVM_SETCOLUMNWIDTH, 6, LVSCW_AUTOSIZE);
}
}
void selectItem(LPNMITEMACTIVATE item) {
int row = item->iItem;
int col = item->iSubItem;
if (row >= 0) {
SearchResult selectedResult = searchResult.at(row);
SendMessage(hMainWindow, WM_SEARCH_SET_LONLAT, 0, reinterpret_cast<LPARAM>(&selectedResult.m_lonLat));
}
}
LRESULT CALLBACK SearchDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
HWND hListView = GetDlgItem(hDlg, IDC_SEARCH_RESULTS);
switch (message) {
case WM_INITDIALOG: {
if (searchProvider == NULL) {
searchProvider = new SearchProvider();
}
LVCOLUMNW col = {0};
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.pszText = const_cast<wchar_t*>(L"Name");
col.cx = 120;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 0, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"Lat");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 1, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"Lon");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 2, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"Class");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 3, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"Type");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 4, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"OSM Type");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 5, reinterpret_cast<LPARAM>(&col));
col.pszText = const_cast<wchar_t*>(L"OSM ID");
col.cx = 30;
SendMessageW(hListView, LVM_INSERTCOLUMNW, 6, reinterpret_cast<LPARAM>(&col));
updateResultList(hListView);
return TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK) {
ListView_DeleteAllItems(hListView);
HWND hInputField = GetDlgItem(hDlg, IDC_DLG_LOCATIONNAME);
int length = GetWindowTextLength(hInputField) + 1;
std::wstring locationName;
locationName.resize(length);
GetWindowTextW(hInputField, &locationName[0], length);
// TODO: do we have to free the previous searchResult?
searchResult = searchProvider->search(locationName);
updateResultList(hListView);
return TRUE;
}
if (LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
delete searchProvider;
searchProvider = NULL;
return TRUE;
}
if (LOWORD(wParam) == IDM_OPEN_IN_OSM) {
std::stringstream url;
url << "https://www.openstreetmap.org/"
<< urlEncode(clickedSearchResult.m_osmType)
<< "/"
<< urlEncode(clickedSearchResult.m_osmId);
ShellExecute(
NULL,
"open",
url.str().c_str(),
NULL,
NULL,
SW_SHOWNORMAL
);
}
break;
case WM_CONTEXTMENU: {
POINT point = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
LVHITTESTINFO hitTest = {0};
hitTest.pt = point;
ScreenToClient(hListView, &hitTest.pt);
int index = ListView_HitTest(hListView, &hitTest);
if (index != -1 && (hitTest.flags & LVHT_ONITEM)) {
clickedSearchResult = searchResult.at(index);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, IDM_OPEN_IN_OSM, "Open OSM object in browser");
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hDlg, NULL);
DestroyMenu(hMenu);
}
break;
}
case WM_NOTIFY:
LPNMHDR hdr = reinterpret_cast<LPNMHDR>(lParam);
if (hdr->hwndFrom == hListView && hdr->code == LVN_ITEMACTIVATE) {
selectItem((LPNMITEMACTIVATE) lParam);
}
break;
}
return FALSE;
}
void search(HINSTANCE hInst, HWND hWnd) {
// Not sure why I need to store it and why GetParent(hDlg) does not return this hWnd...
hMainWindow = hWnd;
DialogBox(hInst, (LPCTSTR)IDD_SEARCH, hWnd, (DLGPROC)SearchDialog);
}