From 94119478207034b767c1d0a4fa5d8950f7b07b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Thu, 4 Aug 2022 18:18:37 +0200 Subject: Use C++ casts, nullptr. --- c/main.cpp | 84 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 46 deletions(-) (limited to 'c/main.cpp') diff --git a/c/main.cpp b/c/main.cpp index f428d49..b8303b5 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -20,8 +20,8 @@ int g_dpi = 96; /* Cursors. */ -HCURSOR g_hcArrow = LoadCursor(NULL, IDC_ARROW); -HCURSOR g_hcSizeNs = LoadCursor(NULL, IDC_SIZENS); +HCURSOR g_hcArrow = LoadCursor(nullptr, IDC_ARROW); +HCURSOR g_hcSizeNs = LoadCursor(nullptr, IDC_SIZENS); /* Fonts. */ HFONT g_hfNormal; @@ -100,19 +100,19 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons std::set_terminate(OnTerminate); /* Initialize Prolog. */ - char* argv[] = {const_cast("EpisodeBrowser"), NULL}; + char* argv[] = {const_cast("EpisodeBrowser"), nullptr}; if (!PL_initialise(1, argv)) throw std::runtime_error("Could not initialize Prolog."); if (!Pl("track_episodes","attach") || !Pl("episode_data","attach")) throw std::runtime_error("Could not attach databases."); - /* Initialize common controls, load menu and register window class. */ INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_WIN95_CLASSES; Require(InitCommonControlsEx(&icc)); - g_hMenuPopup = Require(LoadMenu((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU))); + /* Load context menu. */ + g_hMenuPopup = Require(LoadMenu(nullptr, MAKEINTRESOURCE(IDR_POPUPMENU))); g_hMenuPopup = Require(GetSubMenu(g_hMenuPopup, 0)); WNDCLASSEX wc; @@ -120,35 +120,34 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; - wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); wc.hCursor = g_hcArrow; - wc.hbrBackground = (HBRUSH)COLOR_WINDOW; + wc.hbrBackground = reinterpret_cast(COLOR_WINDOW); wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wc.lpszClassName = L"Episode Browser"; - wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); + wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); 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(SetWindowsHookEx(WH_CBT, CBTProc, - (HINSTANCE)NULL, GetCurrentThreadId())); + const HHOOK hHook = Require(SetWindowsHookEx(WH_CBT, CBTProc, nullptr, GetCurrentThreadId())); const HWND hWnd = Require(CreateWindowEx( 0, L"Episode Browser", L"Episode Browser", WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, - (HWND)NULL, (HMENU)NULL, hInstance, (void*)NULL)); + nullptr, nullptr, hInstance, nullptr)); Require(UnhookWindowsHookEx(hHook)); g_hWndStatus = Require(CreateWindowEx( 0, STATUSCLASSNAME, - (const wchar_t*)NULL, + nullptr, WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0, 0, 0, 0, - hWnd, (HMENU)IDR_STATUS, hInstance, (void*)NULL)); + hWnd, reinterpret_cast(IDR_STATUS), hInstance, nullptr)); ShowWindow(hWnd, nCmdShow); @@ -158,7 +157,7 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons g_elv->RestoreFocus(); MSG msg; - while (GetMessage(&msg, NULL, 0, 0) > 0) { + while (GetMessage(&msg, nullptr, 0, 0) > 0) { if (IsDialogMessage(hWnd, &msg)) continue; TranslateMessage(&msg); DispatchMessage(&msg); @@ -177,24 +176,23 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR * window -- before WndProc! This is important, as it * initializes global variables that are used by WndProc. */ - g_hWnd = (HWND)wParam; + g_hWnd = reinterpret_cast(wParam); /* Look up DPI. */ if (auto lib = Library::Maybe(L"User32.dll"); auto GetDpiForWindow = lib->GetProcAddress("GetDpiForWindow")) g_dpi = GetDpiForWindow(g_hWnd); - /* Load fonts. */ + /* Load normal font. */ if (auto lib = Library::Maybe(L"User32.dll"); lib->GetProcAddress("SystemParametersInfoW")) { - NONCLIENTMETRICS m; - m.cbSize = sizeof(NONCLIENTMETRICS); - Require(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, - sizeof(NONCLIENTMETRICS), &m, 0)); + NONCLIENTMETRICS m = {sizeof(NONCLIENTMETRICS)}; + Require(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0)); g_hfNormal = Require(CreateFontIndirect(&m.lfMessageFont)); } else g_hfNormal = static_cast(Require(GetStockObject(DEFAULT_GUI_FONT))); + /* Load bold font. */ LOGFONT lf; Require(GetObject(g_hfNormal, sizeof(LOGFONT), &lf)); lf.lfWeight = FW_BOLD; @@ -230,16 +228,13 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, switch (uMsg) { case WM_CREATE: UpdateTheme(); - SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(412), SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE); + SetWindowPos(hWnd, nullptr, -1, -1, Dpi(510), Dpi(412), SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE); SetFocus(g_elv->hWnd); /* Set menu item checkmarks according to saved settings. */ - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, - g_bViewWatched? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, - g_bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, - g_limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_bViewWatched? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_bViewTVOriginal? MF_CHECKED: MF_UNCHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS, g_limitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED); break; case WM_CLOSE: DestroyWindow(hWnd); @@ -253,8 +248,8 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, UpdateLayout(LOWORD(lParam), HIWORD(lParam)); break; case WM_GETMINMAXINFO: - ((MINMAXINFO*)lParam)->ptMinTrackSize.x = Dpi(220); - ((MINMAXINFO*)lParam)->ptMinTrackSize.y = Dpi(220); + reinterpret_cast(lParam)->ptMinTrackSize.x = Dpi(220); + reinterpret_cast(lParam)->ptMinTrackSize.y = Dpi(220); break; case WM_THEMECHANGED: UpdateTheme(); @@ -262,13 +257,13 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, break; case 0x02E0: /* WM_DPICHANGED */ { - const RECT* const r = (RECT*)lParam; + const RECT* const r = reinterpret_cast(lParam); /* Update DPI and cached metrics. */ g_dpi = HIWORD(wParam); Metric = GetSystemMetrics(SM_CXVSCROLL); - Prefer(SetWindowPos(hWnd, (HWND)NULL, + Prefer(SetWindowPos(hWnd, nullptr, r->left, r->top, r->right-r->left, r->bottom-r->top, @@ -289,7 +284,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, } break; case WM_NOTIFY: - switch (((NMHDR*)lParam)->idFrom) { + switch (reinterpret_cast(lParam)->idFrom) { case IDC_EPISODELISTVIEW: return g_elv->HandleNotify(lParam); } @@ -356,7 +351,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const wchar_t** const vTip = group==IDG_MENU? vTipMenu: vTipCtx; tip = vTip[ID_INDEX(command)]; } - SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(0,0), (LPARAM)tip); + SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(0,0), reinterpret_cast(tip)); break; } case WM_LBUTTONDOWN: @@ -365,7 +360,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, case WM_SETCURSOR: if (!g_dragDlv.HandleMove()) { /* Use default cursor. */ - if ((HWND)wParam == hWnd) + if (reinterpret_cast(wParam) == hWnd) return DefWindowProc(hWnd, uMsg, wParam, lParam); else return 0; @@ -395,23 +390,20 @@ void HandleMainMenu(const HWND hWnd, unsigned short command) break; case IDM_FILE_ABOUT: DialogBox( - GetModuleHandle(NULL), + GetModuleHandle(nullptr), MAKEINTRESOURCE(IDD_ABOUT), hWnd, - AboutDlgProc - ); + AboutDlgProc); break; case IDM_VIEW_WATCHED: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, - g_bViewWatched? MF_UNCHECKED: MF_CHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED, g_bViewWatched? MF_UNCHECKED: MF_CHECKED); g_bViewWatched = !g_bViewWatched; Pl("cfg","set_view_watched",g_bViewWatched); g_elv->Update(); g_elv->EnsureFocusVisible(); break; case IDM_VIEW_TV_ORIGINAL: - CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, - g_bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); + CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL, g_bViewTVOriginal? MF_UNCHECKED: MF_CHECKED); g_bViewTVOriginal = !g_bViewTVOriginal; Pl("cfg","set_view_tv_original",g_bViewTVOriginal); g_elv->Update(); @@ -509,7 +501,7 @@ void WaitFor(const char* mod, const char* pred) static_cast(activePred)); if (EBMessageBox(msg, L"Error", MB_YESNO|MB_ICONWARNING) != IDYES) return; - KillTimer(NULL, iTimer); + KillTimer(nullptr, iTimer); bActive = 0; g_elv->Update(); } @@ -524,9 +516,9 @@ void WaitFor(const char* mod, const char* pred) if (Pl("episode_data","thread_running",aThread)) { i = (i+1)%4; SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), - (LPARAM)(i==0? L".": i==1? L"..": i==2? L"...": L"")); + reinterpret_cast(i==0? L".": i==1? L"..": i==2? L"...": L"")); } else { - KillTimer(NULL, iTimer); + KillTimer(nullptr, iTimer); i = 0; bActive = 0; g_elv->Update(); @@ -534,8 +526,8 @@ void WaitFor(const char* mod, const char* pred) }; Plx(mod,"thread_create",pred,&aThread); - SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)L"."); - if (Prefer(iTimer = SetTimer(NULL, -1, 500, proc))) { + SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), reinterpret_cast(L".")); + if (Prefer(iTimer = SetTimer(nullptr, -1, 500, proc))) { activePred = WcharPtr::FromNarrow(pred); bActive = 1; } -- cgit v1.2.3