-
Notifications
You must be signed in to change notification settings - Fork 3
/
songlist.cpp
197 lines (174 loc) · 5.69 KB
/
songlist.cpp
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
/* File: songlist.cpp
* Creation Date: February 21st, 2010
* Last Modified Date: February 21st, 2010
* Version: 0.0.1
* Contact: Adam Lamers <adam@millenniumsoftworks.com>
*/
#include "songlist.h"
/* Custom sort instructions to pass to static text compare method. */
typedef struct
{
HWND handle; /** < Handle to list view */
int colIndex; /** < Index of column to sort (zero-indexed) */
bool sortDir; /** < Sort direction (true = ascending, false = descending) */
} ListViewSortInfo;
CSongList::CSongList(HWND Parent) : tracks(NULL), OnAddItem(NULL)
{
for(unsigned int i = 0; i < (sizeof(colSortDirs) / sizeof(bool)); i++)
{
colSortDirs[i] = false; /* Initialize them to false */
}
parent = Parent;
handle = GetDlgItem(parent, IDC_SONGLIST);
SendMessage(handle, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
ListViewAddColumn(handle, 0, "", 23);
ListViewAddColumn(handle, 1, "Name", 234);
ListViewAddColumn(handle, 2, "Artist", 234);
ListViewAddColumn(handle, 3, "Album", 234);
ListViewAddColumn(handle, 4, "Genre", 218);
ContextMenu = CreatePopupMenu();
InsertMenu(ContextMenu, 0, MF_BYPOSITION | MF_STRING, IDM_CONTEXTSAVESONG, "Save Song(s)...");
InsertMenu(ContextMenu, 1, MF_BYPOSITION | MF_STRING, IDM_CONTEXTCHECKSELECTED, "Check Selected Items");
InsertMenu(ContextMenu, 2, MF_BYPOSITION | MF_STRING, IDM_CONTEXTUNCHECKSELECTED, "Uncheck Selected Items");
Scale();
}
void CSongList::Clear()
{
SendMessage(handle, LVM_DELETEALLITEMS, 0, 0L);
}
bool CSongList::AddRow(char *name, char *artist, char *album, char *genre)
{
LVITEM item;
memset(&item, 0, sizeof(LVITEM));
item.mask = LVIF_TEXT;
item.iItem = 0;
item.iSubItem = 0;
item.pszText = "";
if(ListView_InsertItem(handle, &item) == -1) return false;
item.iSubItem = 1;
item.pszText = name;
if(ListView_SetItem(handle, &item) == -1) return false;
item.iSubItem = 2;
item.pszText = artist;
if(ListView_SetItem(handle, &item) == -1) return false;
item.iSubItem = 3;
item.pszText = album;
if(ListView_SetItem(handle, &item) == -1) return false;
item.iSubItem = 4;
item.pszText = genre;
if(ListView_SetItem(handle, &item) == -1) return false;
if(OnAddItem) OnAddItem();
return true;
}
void CSongList::Scale()
{
RECT windowRect;
GetWindowRect(parent, &windowRect);
int width = (windowRect.right - windowRect.left);
int height = (windowRect.bottom - windowRect.top);
SetWindowPos(handle,
HWND_TOP,
0,
25,
width - 16,
height - 103,
SWP_NOZORDER);
}
void CSongList::CheckSelectedItems(bool check)
{
int ItemCount = ListView_GetItemCount(handle);
int i;
for(i = 0; i < ItemCount; i++)
{
LVITEM item;
item.iItem = i;
item.mask = LVIF_STATE;
item.stateMask = LVIS_SELECTED;
if(ListView_GetItem(handle, &item))
{
if(item.state & LVIS_SELECTED)
{
ListView_SetCheckState(handle, item.iItem, check);
}
}
}
}
void CSongList::ShowContextMenu()
{
POINT mouse;
GetCursorPos(&mouse);
if(ListView_GetSelectedCount(handle) > 1)
{
EnableMenuItem(ContextMenu, 1, MF_BYPOSITION | MF_ENABLED);
EnableMenuItem(ContextMenu, 2, MF_BYPOSITION | MF_ENABLED);
}
else
{
EnableMenuItem(ContextMenu, 1, MF_BYPOSITION | MF_GRAYED);
EnableMenuItem(ContextMenu, 2, MF_BYPOSITION | MF_GRAYED);
}
TrackPopupMenu(ContextMenu, TPM_TOPALIGN | TPM_LEFTALIGN, mouse.x, mouse.y, 0, parent, (RECT*)NULL);
}
bool CSongList::Sort(int colIndex)
{
colSortDirs[colIndex] = !colSortDirs[colIndex]; //Reverse sort direction
ListViewSortInfo sortinfo;
sortinfo.handle = handle;
sortinfo.colIndex = colIndex;
sortinfo.sortDir = colSortDirs[colIndex];
ListView_SortItemsEx(handle, &CSongList::SortCallback, &sortinfo);
return false;
}
int CALLBACK CSongList::SortCallback(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
static char buf1[128], buf2[128];
ListViewSortInfo *sortinfo = (ListViewSortInfo*)lParamSort;
ListView_GetItemText(sortinfo->handle, lParam1, sortinfo->colIndex, buf1, sizeof(buf1));
ListView_GetItemText(sortinfo->handle, lParam2, sortinfo->colIndex, buf2, sizeof(buf2));
if(sortinfo->sortDir)
return(-(strcmp(buf1, buf2)));
else
return strcmp(buf1, buf2);
}
HWND CSongList::GetHandle()
{
return handle;
}
void CSongList::ContextSaveSong()
{
std::vector<int> selectedItems = GetSelected();
if(tracks)
{
MessageBox(NULL, ((Itdb_Track*)(tracks->front()))->title, ((Itdb_Track*)(tracks->front()))->ipod_path, MB_OK);
}
}
std::vector<int> CSongList::GetSelected()
{
std::vector<int> ret;
int ItemCount = ListView_GetItemCount(handle);
int i;
for(i = 0; i < ItemCount; i++)
{
LVITEM item;
memset(&item, 0, sizeof(item));
item.iItem = i;
item.mask = LVIF_STATE;
item.stateMask = LVIS_SELECTED;
if(ListView_GetItem(handle, &item))
{
if(item.state == LVIS_SELECTED)
{
ret.push_back(i);
}
}
}
return ret;
}
void CSongList::SetTrackList(std::list<Itdb_Track*> *list)
{
tracks = list;
}
CSongList::~CSongList()
{
if(tracks) delete tracks;
}