Skip to content

Commit 8716120

Browse files
committed
Hopefully make some things nicer in code
1 parent 83798c4 commit 8716120

File tree

7 files changed

+16
-45
lines changed

7 files changed

+16
-45
lines changed

IDropTarget.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdbool.h>
55
#include <shlobj.h>
66

7+
#include "treeview_extension.h"
78
#include "utility.h"
89
#include "bnk-extract/defs.h"
910

@@ -99,17 +100,7 @@ static HRESULT STDMETHODCALLTYPE DragOver(IDropTarget* This, DWORD grfKeyState,
99100

100101
// early return in case DragEnter determined that this data is not drag-drop supported
101102
if (!idropTarget->dragValid) return S_OK;
102-
printf("x: %ld, y: %ld\n", pt.x, pt.y);
103-
// The POINT structure is identical to the POINTL structure.
104-
POINT point = {
105-
.x = pt.x,
106-
.y = pt.y
107-
};
108-
printf("screentoclient return: %d\n", ScreenToClient(treeview, &point));
109-
printf("x: %ld, y: %ld\n", point.x, point.y);
110-
TVHITTESTINFO hitTestInfo = {.pt = point};
111-
HTREEITEM hoveredItem = TreeView_HitTest(treeview, &hitTestInfo);
112-
printf("hovered Item: %p\n", hoveredItem);
103+
HTREEITEM hoveredItem = TreeView_PerformHitTest(pt.x, pt.y, NULL);
113104
if (hoveredItem) *pdwEffect = DROPEFFECT_COPY;
114105

115106
return S_OK;
@@ -167,13 +158,14 @@ static HRESULT STDMETHODCALLTYPE Drop(IDropTarget* This, IDataObject* pDataObj,
167158
UINT fileNameSize = DragQueryFile(hDropInfo, index, NULL, 0);
168159
char fileNameBuffer[fileNameSize + 1];
169160
DragQueryFile(hDropInfo, index, fileNameBuffer, fileNameSize + 1);
170-
uint32_t current_file_id = strtol(rstrstr(fileNameBuffer, "\\") + 1, NULL, 0);
161+
uint32_t current_file_id = strtol(strrchr(fileNameBuffer, '\\') + 1, NULL, 0);
171162
AudioData* wemData = NULL;
172163
printf("wemDataList: %p\n", wemDataList);
173164
printf("length of list: %llu\n", wemDataList->length);
174165
for (uint32_t i = 0; i < wemDataList->length; i++) {
175166
printf("objects[%d]: %u\n", i, wemDataList->objects[i].id);
176167
}
168+
// relies on the fact this list is inherently sorted, which it always is in practice.
177169
find_object_s(wemDataList, wemData, id, current_file_id);
178170
printf("wemData: %p\n", wemData);
179171
if (wemData) {

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object_files := gui.o utility.o IDropTarget.o settings.o treeview_extension.o re
3232
gui.o: IDropTarget.h resource.h settings.h treeview_extension.h utility.h
3333
utility.o: utility.h settings.h treeview_extension.h
3434
settings.o: settings.h resource.h
35-
IDropTarget.o: utility.h
35+
IDropTarget.o: treeview_extension.h utility.h
3636
resource.res: resource.h icon.ico
3737
manifest.res: gui.exe.manifest
3838

gui.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,16 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
151151
{
152152
case NM_RCLICK: {
153153
DWORD position = GetMessagePos();
154-
POINTS mousePosition = MAKEPOINTS(position);
155154
HMENU hPopupMenu = CreatePopupMenu();
156155
InsertMenu(hPopupMenu, 0, MF_BYPOSITION, 1, "Play audio");
157156
InsertMenu(hPopupMenu, 1, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
158157
InsertMenu(hPopupMenu, 2, MF_BYPOSITION, 2, "Extract selection");
159158
InsertMenu(hPopupMenu, 3, MF_BYPOSITION, 3, "Replace wem data");
160-
uint64_t selectedId = TrackPopupMenu(hPopupMenu, TPM_RETURNCMD, mousePosition.x, mousePosition.y, 0, hwnd, NULL);
159+
uint64_t selectedId = TrackPopupMenu(hPopupMenu, TPM_RETURNCMD, GET_X_LPARAM(position), GET_Y_LPARAM(position), 0, hwnd, NULL);
161160
switch (selectedId)
162161
{
163162
case 1:
164-
rightClickedItem = TreeView_PerformHitTest(position, NULL);
163+
rightClickedItem = TreeView_PerformHitTest(GET_X_LPARAM(position), GET_Y_LPARAM(position), NULL);
165164
return SendMessage(PlayAudioButton, BM_CLICK, 0, 0);
166165
case 2:
167166
return SendMessage(ExtractButton, BM_CLICK, 0, 0);
@@ -173,7 +172,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
173172
case NM_CLICK:
174173
if (settings[ID_MULTISELECT_ENABLED-SETTINGS_OFFSET]) {
175174
UINT flags;
176-
HTREEITEM hItem = TreeView_PerformHitTest(GetMessagePos(), &flags);
175+
DWORD position = GetMessagePos();
176+
HTREEITEM hItem = TreeView_PerformHitTest(GET_X_LPARAM(position), GET_Y_LPARAM(position), &flags);
177177
if (hItem && (flags & TVHT_ONITEM)) return HandleMultiSelectionClick(hItem);
178178
}
179179
break;

treeview_extension.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
HWND treeview;
66

7-
HTREEITEM TreeView_PerformHitTest(DWORD screenPosition, UINT* outFlags)
7+
HTREEITEM TreeView_PerformHitTest(int screenX, int screenY, UINT* outFlags)
88
{
9-
POINTS mousePosition = MAKEPOINTS(screenPosition);
10-
POINT point = {.x = mousePosition.x, .y = mousePosition.y};
9+
POINT point = {.x = screenX, .y = screenY};
1110
ScreenToClient(treeview, &point);
1211
TVHITTESTINFO hitTestInfo = {.pt = point};
1312
HTREEITEM hItem = TreeView_HitTest(treeview, &hitTestInfo);

treeview_extension.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern HWND treeview;
55

66
#define TreeView_IsRootItem(hItem) !TreeView_GetParent(treeview, hItem)
77

8-
HTREEITEM TreeView_PerformHitTest(DWORD screenPosition, UINT* outFlags);
8+
HTREEITEM TreeView_PerformHitTest(int screenX, int screenY, UINT* outFlags);
99

1010
void TreeView_ClearAllSelectedItems();
1111

utility.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void SaveBnkOrWpk(HWND window, HTREEITEM root)
186186
};
187187
if (GetSaveFileName(&fileNameInfo)) {
188188
char* selectedFile = fileNameInfo.lpstrFile;
189-
printf("selceted file: \"%s\"\n", selectedFile);
189+
printf("selected file: \"%s\"\n", selectedFile);
190190
if (strstr(selectedFile, ".wpk")) {
191191
write_wpk_file((AudioDataList*) tvItem.lParam, selectedFile);
192192
} else {
@@ -210,13 +210,13 @@ void ReplaceWemData(HWND window)
210210
.hItem = currentItem
211211
};
212212
TreeView_GetItem(treeview, &tvItem);
213-
if (tvItem.lParam && !TreeView_IsRootItem(currentItem)) {
213+
if (tvItem.lParam && !TreeView_IsRootItem(currentItem)) { // add child items only
214214
bool found = false;
215215
for (uint32_t i = 0; i < selectedChildItemsDataList.length; i++) {
216216
if (selectedChildItemsDataList.objects[i] == (AudioData*) tvItem.lParam)
217-
found = true;
217+
found = true; // the same audio files can appear in the treeview multiple times, so don't use them multiple times
218218
}
219-
if (!found) add_object(&selectedChildItemsDataList, &(AudioData*) {(AudioData*) tvItem.lParam}); // add child items only
219+
if (!found) add_object(&selectedChildItemsDataList, &(AudioData*) {(AudioData*) tvItem.lParam});
220220
}
221221
}
222222

@@ -415,21 +415,3 @@ uint8_t* WavFromOgg(ReadableBinaryData* oggData)
415415

416416
return rawPcmDataFromOgg;
417417
}
418-
419-
// https://stackoverflow.com/questions/1634359/is-there-a-reverse-function-for-strstr
420-
char* rstrstr(const char* haystack, const char* needle)
421-
{
422-
if (*needle == '\0')
423-
return (char*) haystack;
424-
425-
char* result = NULL;
426-
while (true) {
427-
char* p = strstr(haystack, needle);
428-
if (p == NULL)
429-
break;
430-
result = p;
431-
haystack = p + 1;
432-
}
433-
434-
return result;
435-
}

utility.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ void* FillProgressBar(void* _args);
2323

2424
uint8_t* WavFromOgg(ReadableBinaryData* oggData);
2525

26-
char* rstrstr(const char* haystack, const char* needle);
27-
2826
#endif

0 commit comments

Comments
 (0)