From 3f842c733568aa9068aa83fad52540eb98f334b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 20 Jul 2022 20:25:59 +0200 Subject: Simplify require, prefer. --- c/common.cpp | 4 ++-- c/common.h | 21 ++++++++++----------- c/episodelistview.cpp | 6 +++--- c/listview.cpp | 6 +++--- c/main.cpp | 46 +++++++++++++++++++++++----------------------- 5 files changed, 41 insertions(+), 42 deletions(-) (limited to 'c') diff --git a/c/common.cpp b/c/common.cpp index 25ff899..4ce95a0 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -84,8 +84,8 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR int EBMessageBox(const TCHAR* const tszText, const TCHAR* const tszCaption, const unsigned uType) { extern HWND g_hWnd; - HHOOK hHook = require(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId()); + HHOOK hHook = require(SetWindowsHookEx(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId())); int r = MessageBox(g_hWnd, tszText, tszCaption, uType); - require(hHook); + require(UnhookWindowsHookEx(hHook)); return r; } diff --git a/c/common.h b/c/common.h index 24c98c8..01083b8 100644 --- a/c/common.h +++ b/c/common.h @@ -55,24 +55,23 @@ std::optional maybe_make(U... xs) } /* Call Windows API function, throwing error on NULL. */ -template -inline auto require(T... xs) +template +inline T require(T x) { - if (auto r = F(xs...)) return r; - else throw Win32Error(); + if (!x) throw Win32Error(); + return x; } /* Call Windows API function, showing a warning on NULL. */ -template -inline auto prefer(T... xs) +template +inline T prefer(T x) { - if (auto r = F(xs...)) - return r; - else { + if (!x) { EBMessageBox(Win32Error().what(), - TEXT("System Error"), MB_ICONWARNING); - return (decltype(F(std::declval()...)))NULL; + TEXT("System Error"), MB_ICONWARNING); + return (T)NULL; } + return x; } /* Return integer scaled for current DPI. */ diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 5df33a1..676f375 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -90,7 +90,7 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) { extern HFONT g_hfBold; if (!Pl("track_episodes","watched",pLvCd->nmcd.lItemlParam)) { - require(pLvCd->nmcd.hdc, g_hfBold); + require(SelectObject(pLvCd->nmcd.hdc, g_hfBold)); return CDRF_NEWFONT; } break; @@ -125,9 +125,9 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) { extern HMENU g_hPopupMenu; const DWORD dwPos = GetMessagePos(); - require(g_hPopupMenu, TPM_RIGHTBUTTON, + require(TrackPopupMenu(g_hPopupMenu, TPM_RIGHTBUTTON, LOWORD(dwPos), HIWORD(dwPos), 0, - m_hWndParent, (const RECT*)NULL); + m_hWndParent, (const RECT*)NULL)); break; } } diff --git a/c/listview.cpp b/c/listview.cpp index 752ebcb..319433a 100644 --- a/c/listview.cpp +++ b/c/listview.cpp @@ -13,15 +13,15 @@ ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle { m_hWndParent = hWndParent; m_bHeader = !(dwStyle & LVS_NOCOLUMNHEADER); - hWnd = require( + hWnd = require(CreateWindowEx( WS_EX_CLIENTEDGE, WC_LISTVIEW, TEXT(""), dwStyle|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP|LVS_REPORT|LVS_SHOWSELALWAYS, 0, 0, 0, 0, - m_hWndParent, hMenu, GetModuleHandle(NULL), this); + m_hWndParent, hMenu, GetModuleHandle(NULL), this)); - if (require(hWnd, TEXT("this"), (HANDLE)this)) + if (require(SetProp(hWnd, TEXT("this"), (HANDLE)this))) m_prevProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)::WndProc); diff --git a/c/main.cpp b/c/main.cpp index 541f524..219f4b8 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -89,10 +89,10 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_WIN95_CLASSES; - require(&icc); + require(InitCommonControlsEx(&icc)); - g_hPopupMenu = require((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU)); - g_hPopupMenu = require(g_hPopupMenu, 0); + g_hPopupMenu = require(LoadMenu((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU))); + g_hPopupMenu = require(GetSubMenu(g_hPopupMenu, 0)); WNDCLASSEX wc; memset(&wc, 0, sizeof(WNDCLASSEX)); @@ -105,29 +105,29 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wc.lpszClassName = TEXT("Episode Browser"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); - require(&wc); + require(RegisterClassEx(&wc)); /* Create window. A CBT hook is used to initialize important * global variables before any messages are sent to the new * window. It is vital that the hook is set up correctly. */ - const HHOOK hHook = require(WH_CBT, CBTProc, - (HINSTANCE)NULL, GetCurrentThreadId()); - const HWND hWnd = require( + const HHOOK hHook = require(SetWindowsHookEx(WH_CBT, CBTProc, + (HINSTANCE)NULL, GetCurrentThreadId())); + const HWND hWnd = require(CreateWindowEx( 0, TEXT("Episode Browser"), TEXT("Episode Browser"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, - (HWND)NULL, (HMENU)NULL, hInstance, (void*)NULL); - require(hHook); + (HWND)NULL, (HMENU)NULL, hInstance, (void*)NULL)); + require(UnhookWindowsHookEx(hHook)); - g_hWndStatus = require( + g_hWndStatus = require(CreateWindowEx( 0, STATUSCLASSNAME, (const TCHAR*)NULL, WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0, 0, 0, 0, - hWnd, (HMENU)ID_STATUS, hInstance, (void*)NULL); + hWnd, (HMENU)ID_STATUS, hInstance, (void*)NULL)); ShowWindow(hWnd, nCmdShow); @@ -171,17 +171,17 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR if (opLib->GetProcAddress("SystemParametersInfo" WA)) { NONCLIENTMETRICS m; m.cbSize = sizeof(NONCLIENTMETRICS); - require(SPI_GETNONCLIENTMETRICS, - sizeof(NONCLIENTMETRICS), &m, 0); - g_hfNormal = require(&m.lfMessageFont); + require(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, + sizeof(NONCLIENTMETRICS), &m, 0)); + g_hfNormal = require(CreateFontIndirect(&m.lfMessageFont)); } } else - g_hfNormal = static_cast(require(DEFAULT_GUI_FONT)); + g_hfNormal = static_cast(require(GetStockObject(DEFAULT_GUI_FONT))); LOGFONT lf; - require(g_hfNormal, sizeof(LOGFONT), &lf); + require(GetObject(g_hfNormal, sizeof(LOGFONT), &lf)); lf.lfWeight = FW_BOLD; - g_hfBold = require(&lf); + g_hfBold = require(CreateFontIndirect(&lf)); /* Create child windows. */ g_pDlv = new DataListView(g_hWnd); @@ -237,11 +237,11 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, { const RECT* const lpr = (RECT*)lParam; g_iDPI = HIWORD(wParam); - prefer(hWnd, (HWND)NULL, + prefer(SetWindowPos(hWnd, (HWND)NULL, lpr->left, lpr->top, lpr->right-lpr->left, lpr->bottom-lpr->top, - SWP_NOZORDER|SWP_NOACTIVATE); + SWP_NOZORDER|SWP_NOACTIVATE)); UpdateLayout(); break; } @@ -281,7 +281,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, } else { i = 0; g_bThread = 0; - prefer(hWnd, IDT_TIMER); + prefer(KillTimer(hWnd, IDT_TIMER)); g_pElv->Update(); } break; @@ -304,7 +304,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, if (g_bThread) break; Pl("episode_data","thread_create","update_screenwriters",&g_aThread); t: KillTimer(hWnd, IDT_TIMER); - if (!prefer(hWnd, IDT_TIMER, 500, (TIMERPROC)NULL)) break; + if (!prefer(SetTimer(hWnd, IDT_TIMER, 500, (TIMERPROC)NULL))) break; SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)TEXT(".")); g_bThread = 1; break; @@ -515,14 +515,14 @@ void UpdateLayout() /* Resize data list view. */ cyDlv = rc.bottom-yStatus-g_pDlv->Height(); - require(g_pDlv->hWnd, 0, cyDlv, rc.right, rc.bottom-yStatus-cyDlv, TRUE); + require(MoveWindow(g_pDlv->hWnd, 0, cyDlv, rc.right, rc.bottom-yStatus-cyDlv, TRUE)); ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, LVSCW_AUTOSIZE); cxColumn = ListView_GetColumnWidth(g_pDlv->hWnd, 0)+4; ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, cxColumn); ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIVALUE, rc.right-cxColumn-cxVScroll-4); /* Resize episode list view. */ - require(g_pElv->hWnd, 0, 0, rc.right, cyDlv+1, TRUE); + require(MoveWindow(g_pElv->hWnd, 0, 0, rc.right, cyDlv+1, TRUE)); ListView_SetColumnWidth(g_pElv->hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE); cxColumn = ListView_GetColumnWidth(g_pElv->hWnd, ELVSIEPISODE)+4; ListView_SetColumnWidth(g_pElv->hWnd, ELVSIEPISODE, cxColumn); -- cgit v1.2.3