Repository navigation
Expand file tree
/
Copy pathPlaylistTreeView.cpp
More file actions
429 lines (309 loc) · 10.6 KB
/
Copy pathPlaylistTreeView.cpp
File metadata and controls
429 lines (309 loc) · 10.6 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
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
/** $VER: PlaylistsTreeView.cpp (2026.08.04) P. Stuer **/
#include "pch.h"
#include "PlaylistTreeView.h"
#include "Theme.h"
#pragma hdrstop
/// <summary>
/// Gets the text of the specified item.
/// </summary>
bool playlist_tree_view_t::GetText(const GUID & id, std::string & text) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
return __super::GetText(hItem, text);
}
/// <summary>
/// Sets the name of the specified item.
/// </summary>
bool playlist_tree_view_t::SetName(const GUID & id, const std::string & name) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
auto Node = (node_t *) GetData(hItem);
if (Node == nullptr)
return false;
Node->Name = name;
RedrawItem(hItem);
return true;
}
/// <summary>
/// Adds an item.
/// </summary>
node_t * playlist_tree_view_t::AddItem(const GUID & parentId, const GUID & insertAfterId, const GUID & id, const std::string & name, bool isFolder, bool isExpanded, bool isFrozen) const noexcept
{
auto Node = new node_t(id, name, isFolder, isFrozen);
HTREEITEM hParent = FindItem(parentId);
if (hParent == NULL)
hParent = TVI_ROOT;
HTREEITEM hInsertAfter = FindItem(insertAfterId);
if (hInsertAfter == NULL)
hInsertAfter = TVI_LAST;
const UINT State = isExpanded ? TVIS_EXPANDED : 0;
auto hNewItem = __super::AddItem(hParent, hInsertAfter, State, Node);
if (hNewItem == NULL)
{
delete Node;
return nullptr;
}
return Node;
}
/// <summary>
/// Removes the specified item.
/// </summary>
bool playlist_tree_view_t::RemoveItem(const GUID & id) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
return __super::RemoveItem(hItem);
}
/// <summary>
/// Selects the specified id.
/// </summary>
bool playlist_tree_view_t::SelectItem(const GUID & id) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
return SelectItem(hItem);
}
/// <summary>
/// Selects the item with the specified name.
/// </summary>
bool playlist_tree_view_t::SelectItem(const std::string & name) const noexcept
{
bool IsSelected = false;
__super::Walk([&](HTREEITEM hItem, void * context) -> bool
{
auto Node = (const node_t *) GetData(hItem);
if ((Node != nullptr) && (Node->Name == name))
{
IsSelected = SelectItem(hItem);
return false;
}
return true; // Continue walking.
});
return IsSelected;
}
/// <summary>
/// Finds the item with the specified id.
/// </summary>
HTREEITEM playlist_tree_view_t::FindItem(const GUID & id) const noexcept
{
if (id == GUID_NULL)
return NULL;
HTREEITEM hFoundItem = NULL;
__super::Walk([&](HTREEITEM hItem, void * context) -> bool
{
auto Node = (const node_t *) GetData(hItem);
if ((Node != nullptr) && (Node->Id == id))
{
hFoundItem = hItem;
return false;
}
return true; // Continue walking
});
return hFoundItem;
}
/// <summary>
/// Gets the item at the specified point.
/// </summary>
HTREEITEM playlist_tree_view_t::GetHighlightedItem(const POINT & pt) const noexcept
{
auto hItem = __super::GetHighlightedItem(pt);
return hItem;
}
/// <summary>
/// Gets the selected item.
/// </summary>
node_t * playlist_tree_view_t::GetSelectedItem() const noexcept
{
auto hItem = __super::GetSelectedItem();
if (hItem == NULL)
return nullptr;
auto Node = (node_t *) GetData(hItem);
return Node;
}
/// <summary>
/// Refreshes the specified item.
/// </summary>
bool playlist_tree_view_t::RefreshItem(const GUID & id) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
__super::RefreshItem(hItem);
return true;
}
/// <summary>
/// Returns true if the specified item is expanded.
/// </summary>
bool playlist_tree_view_t::IsExpanded(const GUID & id) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return false;
UINT State = 0;
if (!GetState(hItem, State))
return false;
return ((State & TVIS_EXPANDED) == TVIS_EXPANDED);
}
/// <summary>
/// Returns the number of direct children of a node.
/// </summary>
size_t playlist_tree_view_t::GetChildCount(const GUID & id) const noexcept
{
HTREEITEM hItem = FindItem(id);
if (hItem == NULL)
return SIZE_MAX;
return __super::GetChildCount(hItem);
}
/// <summary>
/// Returns true if a drop is allowed on the target.
/// </summary>
bool playlist_tree_view_t::AllowDrop(DropZone dropZone) noexcept
{
if (_hDropTarget == NULL)
return false;
auto Node = (const node_t *) GetData(_hDropTarget);
if (Node == nullptr)
return false;
return Node->IsFolder || (!Node->IsFolder && (dropZone != DropZone::Middle));
}
/// <summary>
/// Draws a treeview item.
/// </summary>
void playlist_tree_view_t::DrawItem(HDC hDC, HTREEITEM hItem, int level, int scrollX, const RECT & rcItem, HIMAGELIST imageList, uint32_t imageWidth, bool hasFolders, bool isHot, bool isFocused, bool isDragImage) const noexcept
{
const auto HasFocus = (::GetFocus() == Get());
// Get information about the item.
wchar_t Text[512] = { };
TVITEMEX tvi
{
.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_STATE | TVIF_CHILDREN,
.hItem = hItem,
.stateMask = 0xFF,
.pszText = Text,
.cchTextMax = _countof(Text),
};
TreeView_GetItem(Get(), &tvi);
const auto IsSelected = ((tvi.state & TVIS_SELECTED) != 0) & !isDragImage;
const auto IsHighlighted = ((tvi.state & TVIS_DROPHILITED) != 0) & !isDragImage;
// Calculate the start position of the item content.
const auto Indent = TreeView_GetIndent(Get());
RECT rc = rcItem;
rc.left += (Indent * level) - scrollX;
// Calculate the image width including horizontal padding.
const LONG ImageWidth = (1 + (LONG) imageWidth + 1) + 3;
// Get bounding rectangle of the item text.
RECT rcText;
TreeView_GetItemRect(Get(), hItem, &rcText, TRUE);
const LONG TextWidth = rcText.right - rcText.left;
const LONG TextHeight = rcText.bottom - rcText.top;
// Draw a chevron if folders are being used.
if (hasFolders)
{
// Draw a chevron if the folder has children.
if (tvi.cChildren != 0)
{
SIZE GlyphSize = { (LONG) imageWidth, (LONG) imageWidth };
HTHEME hTheme = ::OpenThemeData(Get(), L"TREEVIEW");
if (hTheme)
{
::GetThemePartSize(hTheme, NULL, TVP_GLYPH, GLPS_CLOSED, NULL, TS_TRUE, &GlyphSize);
::CloseThemeData(hTheme);
}
RECT rcChevron = { rc.left, rc.top, rcChevron.left + GlyphSize.cx, rc.bottom };
const auto hOldFont = ::SelectObject(hDC, _Theme.GetIconFont());
// const wchar_t * ChevronLeft = L"\uE76B";
const wchar_t * ChevronRight = L"\uE76C";
const wchar_t * ChevronDown = L"\uE70D";
// const wchar_t * ChevronUp = L"\uE70E";
const DTTOPTS Options =
{
.dwSize = sizeof(Options),
.dwFlags = DTT_TEXTCOLOR,
.crText = _Theme.GetWindowTextColor()
};
constexpr DWORD Flags = DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX;
::DrawThemeTextEx(_Theme.GetTextStyle(), hDC, 0, 0, (tvi.state & TVIS_EXPANDED) ? ChevronDown : ChevronRight, -1, Flags, &rcChevron, &Options);
::SelectObject(hDC, hOldFont);
}
rc.left += TextHeight;
}
// Draw the background.
if (!isDragImage)
{
rc.right = rc.left + ImageWidth + TextWidth;
if (IsSelected)
{
const auto & hBrush = HasFocus ? _Theme.GetSelectionBrush() : _Theme.GetInactiveSelectionBrush();
::FillRect(hDC, &rc, hBrush);
// Draw the focus rectangle.
if (isFocused)
{
const auto & hPen = _Theme.GetHighlightPen();
const auto hOldBrush = ::SelectObject(hDC, hBrush);
const auto hOldPen = ::SelectObject(hDC, hPen);
::RoundRect(hDC, rc.left, rc.top, rc.right, rc.bottom, 2, 2);
::SelectObject(hDC, hOldPen);
::SelectObject(hDC, hOldBrush);
}
}
else
if (isHot || IsHighlighted)
{
const auto & hBrush = _Theme.GetHighlightBrush();
::FillRect(hDC, &rc, hBrush);
}
}
// Draw the image.
{
const LONG dx = 1;
const LONG dy = (TextHeight - (LONG) imageWidth) / 2;
::ImageList_Draw(imageList, tvi.iImage, hDC, rc.left + dx, rc.top + dy, ILD_TRANSPARENT);
rc.left += ImageWidth;
}
// Draw the text.
{
const COLORREF Color = IsSelected ? (HasFocus ? _Theme.GetSelectionTextColor() : _Theme.GetInactiveSelectionTextColor()) : ((isHot || IsHighlighted) ? _Theme.GetHighlightTextColor() : _Theme.GetWindowTextColor());
const DTTOPTS Options =
{
.dwSize = sizeof(Options),
.dwFlags = DTT_TEXTCOLOR,
.crText = Color
};
rc.right = rc.left + TextWidth;
const auto hOldFont = ::SelectObject(hDC, _Theme.GetPlaylistFont());
constexpr DWORD Flags = DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_EXPANDTABS | DT_NOPREFIX;
::DrawThemeTextEx(_Theme.GetTextStyle(), hDC, 0, 0, Text, -1, Flags, &rc, &Options);
::SelectObject(hDC, hOldFont);
}
}
/// <summary>
/// Calculates the dimensions of a drag image.
/// </summary>
void playlist_tree_view_t::MeasureDragImage(HTREEITEM hItem, RECT & rect) const noexcept
{
// Get the rectangle of the text part of the item.
TreeView_GetItemRect(Get(), hItem, &rect, TRUE);
// Add room for the icon.
const auto hImageList = GetNormalImageList();
int IconWidth, IconHeight;
::ImageList_GetIconSize(hImageList, &IconWidth, &IconHeight);
rect.left -= 1 + IconWidth + 1 + 3;
}
/// <summary>
/// Draws a treeview item.
/// </summary>
void playlist_tree_view_t::DrawDragImage(HDC hDC, HTREEITEM hItem, const RECT & rc) const noexcept
{
RECT rcItem = { 0, 0, rc.right - rc.left, rc.bottom - rc.top };
HIMAGELIST hImageList = GetNormalImageList();
int IconWidth, IconHeight;
::ImageList_GetIconSize(hImageList, &IconWidth, &IconHeight);
::FillRect(hDC, &rcItem, _Theme.GetWindowBrush());
DrawItem(hDC, hItem, 0, 0, rcItem, hImageList, (uint32_t) IconWidth, false, false, false, true);
}