diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-29 21:20:44 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-29 21:20:44 +0200 |
commit | 2b8b6ac284dca2bd22514d293519cb877a1079d8 (patch) | |
tree | cf1a212a5be583d52d4965508218be358f6879e4 | |
parent | c883d9cf5673fe0af8d69120b048d642e122bdbb (diff) | |
download | EpisodeBrowser-2b8b6ac284dca2bd22514d293519cb877a1079d8.tar.gz |
Add ListView::FindNextItem.
This makes it much more ergonomic and less error-prone to look up list
view items.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | c/episodelistview.cpp | 36 | ||||
-rw-r--r-- | c/listview.cpp | 8 | ||||
-rw-r--r-- | c/listview.h | 2 | ||||
-rw-r--r-- | c/main.cpp | 7 |
5 files changed, 24 insertions, 31 deletions
@@ -4,7 +4,7 @@ PL = pl/cfg.pl pl/episode_data.pl pl/local_episodes.pl pl/track_episodes.pl CL = swipl-ld CLFLAGS = -DUNICODE -D_UNICODE -CLFLAGS += -cc-options,-std=c++17 -Wall -Wextra -Wpedantic -O2 +CLFLAGS += -cc-options,-std=c++17 -Wall -Wextra -Wpedantic -Wno-missing-field-initializers -O2 CLFLAGS += -ld-options,-mwindows,-static-libstdc++ -lcomctl32 -luxtheme all: showdeps b/$(EXE) diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 6ba97cd..e23e5d8 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -108,11 +108,8 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) } case NM_RETURN: /* Open all selected episodes. */ { - LVITEM lvi; - lvi.mask = LVIF_PARAM; - lvi.iItem = -1; - while ((lvi.iItem = ListView_GetNextItem( - hWnd, lvi.iItem, LVNI_SELECTED)) != -1) { + LVITEM lvi = {LVIF_PARAM, -1}; + while (FindNextItem(&lvi, LVNI_SELECTED)) { if (!ListView_GetItem(hWnd, &lvi)) goto b; if (!Pl("local_episodes","open_episode_locally", lvi.lParam)) @@ -187,10 +184,8 @@ s: ListView_SetItemState(hWnd, -1, LVIF_STATE, LVIS_SELECTED); void EpisodeListView::SaveFocus() { - LVITEM lvi; - lvi.mask = LVIF_PARAM; - if ((lvi.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1 - && ListView_GetItem(hWnd, &lvi)) + LVITEM lvi = {LVIF_PARAM, -1}; + if (FindNextItem(&lvi, LVNI_FOCUSED)) Pl("cfg","set_focus",lvi.lParam); } @@ -205,12 +200,8 @@ void EpisodeListView::SetTop(const int iItem) void EpisodeListView::SelectUnwatched(int iDir) { /* Get focused episode. */ - LVITEM lviFocus; - lviFocus.mask = LVIF_PARAM; - if ((lviFocus.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1 - && ListView_GetItem(hWnd, &lviFocus)) - ; - else + LVITEM lviFocus = {LVIF_PARAM, -1}; + if (!FindNextItem(&lviFocus, LVNI_FOCUSED)) return; LVFINDINFO lvfi; @@ -316,23 +307,18 @@ void EpisodeListView::Update() int iItemMark; static std::vector<int> vEpSel; { - LVITEM lvi; - lvi.mask = LVIF_PARAM; - lvi.iItem = -1; vEpSel.clear(); - while ((lvi.iItem = ListView_GetNextItem(hWnd, lvi.iItem, LVNI_SELECTED)) != -1) - if (ListView_GetItem(hWnd, &lvi)) - vEpSel.push_back(lvi.lParam); + LVITEM lvi = {LVIF_PARAM, -1}; + while (FindNextItem(&lvi, LVNI_SELECTED)) + vEpSel.push_back(lvi.lParam); iItemMark = ListView_GetSelectionMark(hWnd); } /* Save focus. */ int iEpFocus = 0; { - LVITEM lvi; - lvi.mask = LVIF_PARAM; - if ((lvi.iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED)) != -1 - && ListView_GetItem(hWnd, &lvi)) + LVITEM lvi = {LVIF_PARAM, -1}; + if (FindNextItem(&lvi, LVNI_FOCUSED)) iEpFocus = lvi.lParam; } diff --git a/c/listview.cpp b/c/listview.cpp index 62dfba8..b4a61b4 100644 --- a/c/listview.cpp +++ b/c/listview.cpp @@ -29,6 +29,14 @@ ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle SendMessage(hWnd, WM_SETFONT, (WPARAM)g_hfNormal, MAKELPARAM(FALSE, 0)); } +/* Retrieve next matching list view item. */ +bool ListView::FindNextItem(LVITEM* const lvi, const LPARAM lParam) +{ + if ((lvi->iItem = ListView_GetNextItem(hWnd, lvi->iItem, lParam)) == -1) + return false; + return ListView_GetItem(hWnd, lvi); +} + /* Naively calculate height of list view. */ int ListView::Height(int bHeader) { diff --git a/c/listview.h b/c/listview.h index 478819f..858b156 100644 --- a/c/listview.h +++ b/c/listview.h @@ -2,12 +2,14 @@ #define LISTVIEW_H #include <windows.h> +#include <commctrl.h> struct ListView { HWND hWnd; ListView(HWND hWndParent, HMENU hMenu, DWORD dwStyle); + bool FindNextItem(LVITEM* lvi, LPARAM lParam); int Height(int bHeader = -1); virtual void ResizeColumns(int w); virtual void UpdateTheme(BOOL bThemeActive); @@ -406,16 +406,13 @@ void WndProcMainMenu(const HWND hWnd, unsigned short wCommand) /* Process context menu commands. */ void WndProcContextMenu(const HWND, unsigned short wCommand) { - LVITEM lvi; int cNotFound = 0; /* Look through selected items, applying the * selected command to each one. */ - lvi.mask = LVIF_PARAM; - lvi.iItem = -1; - while ((lvi.iItem = ListView_GetNextItem(g_pElv->hWnd, lvi.iItem, LVNI_SELECTED)) != -1 - && ListView_GetItem(g_pElv->hWnd, &lvi)) { + LVITEM lvi = {LVIF_PARAM, -1}; + while (g_pElv->FindNextItem(&lvi, LVNI_SELECTED)) { /* Process rate commands. */ if (ID_SUBGROUP(wCommand) == IDG_CTX_RATE) { |