Debian Codegraph @

phoenix/windows/widget/list-view.cpp

raw
1 namespace phoenix {
2
3 unsigned ListView_GetColumnCount(HWND hwnd) {
4 unsigned count = 0;
5 LVCOLUMN column;
6 column.mask = LVCF_WIDTH;
7 while(ListView_GetColumn(hwnd, count++, &column));
8 return --count;
9 }
10
11 void ListView_SetImage(HWND hwnd, HIMAGELIST imageList, unsigned row, unsigned column, unsigned imageID) {
12 //if this is the first image assigned, set image list now
13 //do not set sooner, or image blocks will appear in a list with no images
14 if(ListView_GetImageList(hwnd, LVSIL_SMALL) != imageList) {
15 ListView_SetImageList(hwnd, imageList, LVSIL_SMALL);
16 }
17
18 LVITEM item;
19 item.mask = LVIF_IMAGE;
20 item.iItem = row;
21 item.iSubItem = column;
22 item.iImage = imageID;
23 ListView_SetItem(hwnd, &item);
24 }
25
26 void pListView::append(const lstring& list) {
27 wchar_t empty[] = L"";
28 unsigned row = ListView_GetItemCount(hwnd);
29 LVITEM item;
30 item.mask = LVIF_TEXT;
31 item.iItem = row;
32 item.iSubItem = 0;
33 item.pszText = empty;
34 locked = true;
35 ListView_InsertItem(hwnd, &item);
36 locked = false;
37 for(unsigned column = 0; column < list.size(); column++) {
38 utf16_t wtext(list(column, ""));
39 ListView_SetItemText(hwnd, row, column, wtext);
40 }
41 }
42
43 void pListView::autoSizeColumns() {
44 unsigned columns = ListView_GetColumnCount(hwnd);
45 for(unsigned n = 0; n < columns; n++) {
46 ListView_SetColumnWidth(hwnd, n, LVSCW_AUTOSIZE_USEHEADER);
47 }
48 }
49
50 void pListView::remove(unsigned selection) {
51 ListView_DeleteItem(hwnd, selection);
52 }
53
54 void pListView::reset() {
55 ListView_DeleteAllItems(hwnd);
56 buildImageList(); //free previously allocated images
57 }
58
59 void pListView::setCheckable(bool checkable) {
60 ListView_SetExtendedListViewStyle(hwnd, LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES | (checkable ? LVS_EX_CHECKBOXES : 0));
61 }
62
63 void pListView::setChecked(unsigned selection, bool checked) {
64 locked = true;
65 ListView_SetCheckState(hwnd, selection, checked);
66 locked = false;
67 }
68
69 void pListView::setGeometry(Geometry geometry) {
70 pWidget::setGeometry(geometry);
71 autoSizeColumns();
72 }
73
74 void pListView::setHeaderText(const lstring& list) {
75 while(ListView_DeleteColumn(hwnd, 0));
76
77 lstring headers = list;
78 if(headers.size() == 0) headers.append(""); //must have at least one column
79
80 for(unsigned n = 0; n < headers.size(); n++) {
81 LVCOLUMN column;
82 column.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM;
83 column.fmt = LVCFMT_LEFT;
84 column.iSubItem = n;
85 utf16_t headerText(headers[n]);
86 column.pszText = headerText;
87 ListView_InsertColumn(hwnd, n, &column);
88 }
89 autoSizeColumns();
90 }
91
92 void pListView::setHeaderVisible(bool visible) {
93 SetWindowLong(
94 hwnd, GWL_STYLE,
95 (GetWindowLong(hwnd, GWL_STYLE) & ~LVS_NOCOLUMNHEADER) |
96 (visible ? 0 : LVS_NOCOLUMNHEADER)
97 );
98 }
99
100 void pListView::setImage(unsigned selection, unsigned position, const image& image) {
101 //assign existing image
102 for(unsigned n = 0; n < images.size(); n++) {
103 if(images[n] == image) {
104 imageMap(selection)(position) = n;
105 return ListView_SetImage(hwnd, imageList, selection, position, n);
106 }
107 }
108
109 //append and assign new image
110 imageMap(selection)(position) = images.size();
111 images.append(image);
112 ImageList_Append(imageList, image, 15);
113 ListView_SetImage(hwnd, imageList, selection, position, imageMap(selection)(position));
114 }
115
116 void pListView::setSelected(bool selected) {
117 locked = true;
118 lostFocus = false;
119 if(selected == false) {
120 ListView_SetItemState(hwnd, -1, 0, LVIS_FOCUSED | LVIS_SELECTED);
121 } else {
122 setSelection(listView.state.selection);
123 }
124 locked = false;
125 }
126
127 void pListView::setSelection(unsigned selection) {
128 locked = true;
129 lostFocus = false;
130 ListView_SetItemState(hwnd, selection, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
131 locked = false;
132 }
133
134 void pListView::setText(unsigned selection, unsigned position, string text) {
135 utf16_t wtext(text);
136 ListView_SetItemText(hwnd, selection, position, wtext);
137 }
138
139 void pListView::constructor() {
140 lostFocus = false;
141 hwnd = CreateWindowEx(
142 WS_EX_CLIENTEDGE, WC_LISTVIEW, L"",
143 WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_NOCOLUMNHEADER,
144 0, 0, 0, 0, parentHwnd, (HMENU)id, GetModuleHandle(0), 0
145 );
146 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&listView);
147 setDefaultFont();
148 setHeaderText(listView.state.headerText);
149 setHeaderVisible(listView.state.headerVisible);
150 setCheckable(listView.state.checkable);
151 for(auto& text : listView.state.text) append(text);
152 for(unsigned n = 0; n < listView.state.checked.size(); n++) setChecked(n, listView.state.checked[n]);
153 buildImageList();
154 if(listView.state.selected) setSelection(listView.state.selection);
155 autoSizeColumns();
156 synchronize();
157 }
158
159 void pListView::destructor() {
160 DestroyWindow(hwnd);
161 }
162
163 void pListView::orphan() {
164 destructor();
165 constructor();
166 }
167
168 void pListView::buildImageList() {
169 auto& list = listView.state.image;
170 unsigned columns = listView.state.text.size();
171 unsigned rows = max(1u, listView.state.headerText.size());
172
173 ListView_SetImageList(hwnd, NULL, LVSIL_SMALL);
174 if(imageList) ImageList_Destroy(imageList);
175 imageList = ImageList_Create(15, 15, ILC_COLOR32, 1, 0);
176
177 imageMap.reset();
178 images.reset();
179 images.append(nall::image()); //empty icon for cells without an image assigned (I_IMAGENONE does not work)
180
181 //create a vector of unique images from all images used (many cells may use the same image)
182 for(unsigned y = 0; y < list.size(); y++) {
183 for(unsigned x = 0; x < list[y].size(); x++) {
184 bool found = false;
185 for(unsigned z = 0; z < images.size(); z++) {
186 if(list[y][x] == images[z]) {
187 found = true;
188 imageMap(y)(x) = z;
189 break;
190 }
191 }
192
193 if(found == false) {
194 imageMap(y)(x) = images.size();
195 images.append(list[y][x]);
196 }
197 }
198 }
199
200 //build image list
201 for(auto& imageItem : images) ImageList_Append(imageList, imageItem, 15);
202 if(images.size() <= 1) return;
203
204 //set images for all cells
205 for(unsigned y = 0; y < columns; y++) {
206 for(unsigned x = 0; x < rows; x++) {
207 ListView_SetImage(hwnd, imageList, y, x, imageMap(y)(x));
208 }
209 }
210 }
211
212 void pListView::onActivate(LPARAM lparam) {
213 LPNMLISTVIEW nmlistview = (LPNMLISTVIEW)lparam;
214 if(listView.state.text.empty() || !listView.state.selected) return;
215 //LVN_ITEMACTIVATE is not re-entrant until DispatchMessage() completes
216 //if(listView.onActivate) listView.onActivate();
217 PostMessage(parentHwnd, WM_APP + AppMessage::ListView_onActivate, 0, (LPARAM)&listView);
218 }
219
220 void pListView::onChange(LPARAM lparam) {
221 LPNMLISTVIEW nmlistview = (LPNMLISTVIEW)lparam;
222 if(!(nmlistview->uChanged & LVIF_STATE)) return;
223
224 unsigned selection = nmlistview->iItem;
225 unsigned imagemask = ((nmlistview->uNewState & LVIS_STATEIMAGEMASK) >> 12) - 1;
226 if(imagemask == 0 || imagemask == 1) {
227 if(!locked) {
228 listView.state.checked[selection] = !listView.state.checked[selection];
229 if(listView.onToggle) listView.onToggle(selection);
230 }
231 } else if((nmlistview->uOldState & LVIS_FOCUSED) && !(nmlistview->uNewState & LVIS_FOCUSED)) {
232 lostFocus = true;
233 listView.state.selected = false;
234 listView.state.selection = 0;
235 } else if(!(nmlistview->uOldState & LVIS_SELECTED) && (nmlistview->uNewState & LVIS_SELECTED)) {
236 lostFocus = false;
237 listView.state.selected = true;
238 listView.state.selection = selection;
239 if(!locked && listView.onChange) listView.onChange();
240 } else if(!lostFocus && !listView.state.selected) {
241 lostFocus = false;
242 listView.state.selected = false;
243 listView.state.selection = 0;
244 if(!locked && listView.onChange) listView.onChange();
245 } else if(listView.selected() && ListView_GetSelectedCount(hwnd) == 0) {
246 listView.state.selected = false;
247 listView.state.selection = 0;
248 if(!locked && listView.onChange) listView.onChange();
249 }
250 }
251
252 LRESULT pListView::onCustomDraw(LPARAM lparam) {
253 LPNMLVCUSTOMDRAW lvcd = (LPNMLVCUSTOMDRAW)lparam;
254
255 switch(lvcd->nmcd.dwDrawStage) {
256 case CDDS_PREPAINT:
257 return CDRF_NOTIFYITEMDRAW;
258 case CDDS_ITEMPREPAINT:
259 if(listView.state.headerText.size() >= 2) {
260 //draw alternating row colors of there are two or more columns
261 if(lvcd->nmcd.dwItemSpec % 2) lvcd->clrTextBk = GetSysColor(COLOR_WINDOW) ^ 0x070707;
262 }
263 return CDRF_DODEFAULT;
264 default:
265 return CDRF_DODEFAULT;
266 }
267 }
268
269 }