From cf8707bf2ede99d8543d806355eee088bdc1376b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 6 Aug 2022 01:21:08 +0200 Subject: Use typedefs when interacting with Win32 API. It's not like they're ever going to change the definition of WORD (knock on wood) -- but I guess it's proper to use them as if their definitions might change. --- README | 55 ++++++++++++++++--------------------------------------- c/debug.cpp | 4 ++-- c/debug.h | 4 +++- c/listview.cpp | 4 ++-- c/listview.h | 2 +- c/main.cpp | 18 +++++++++--------- c/win.cpp | 4 ++-- c/win.h | 15 +++++++-------- 8 files changed, 42 insertions(+), 64 deletions(-) diff --git a/README b/README index 4fe5ba0..49be3fd 100644 --- a/README +++ b/README @@ -30,17 +30,24 @@ Frontend ~~~~~~~~ The graphical interface is implemented in C++ using the Win32 API (see the c folder). The source code is spread across a small number of -files. For an overview of the classes/functions provided by each file, -see defs.h. In summary: +files. In summary: main.cpp Entry point and initialization. Contains the main event handler. -listview.cpp Creates and handles the shared aspects of the two list views. -episodelistview.cpp Defines the interface to the episode list view. -datalistview.cpp Defines the interface to the data list view. -pl.cpp Interface to Prolog backend. -common.cpp Some useful functions that don't fit elsewhere. - -In addition, defs.h itself defines a variety of macros and templates. +listview.* Creates and handles the shared aspects of the two list views. +episodelistview.* Defines the interface to the episode list view. +datalistview.* Defines the interface to the data list view. +layout.* Handles the placement of child windows. +pl.* Interface to Prolog backend. +win.* Helpers for interacting with the Win32 API. +wcharptr.* Defines WcharPtr, a class that owns a wchar_t*. +util.h Simple utility functions. +debug.* Helpers for debugging. + +The code operates under the assumption that "raw" pointers are +non-owning -- i.e., their lifetime is managed by somebody else, such +as Windows or Prolog. When strings need to be heap-allocated, like +when converting from a narrow to a wide string, they are put within a +WcharPtr object, which manages the lifetime of the string. Building ~~~~~~~~ @@ -53,33 +60,3 @@ following additional programs are required: To build b/EpisodeBrowser.exe, issue `make' in the root directory of the project. - -Hacking -~~~~~~~ -Following is a summary of some coding conventions used in the project. - - ... TYPES ... - -Here are some general guidelines for choosing what types to use: - -1) Don't use Windows-specific types that "hide" pointers. -2) Where there is a Windows-specific type with the same name and - function as a standard type (e.g., CHAR = char), prefer the - standard type. -3) Otherwise, prefer the Windows-specific type when interacting with - the Windows API. - -For example, prefer... - - - char* over LPSTR - - char const* over LPCSTR - - LVITEM* over LPLVITEM - - int over INT - - DWORD over unsigned long - (but only when interacting with Windows) - -Note... - - - that the second rule above does not apply to BOOL, which should not - be replaced with bool, as BOOL is an integer that may have other - values than 1 and 0. diff --git a/c/debug.cpp b/c/debug.cpp index 35f8933..6b5baff 100644 --- a/c/debug.cpp +++ b/c/debug.cpp @@ -66,9 +66,9 @@ Benchmark::~Benchmark() } } -const char* WmName(const unsigned uMsg) +const char* WmName(const UINT uMsg) { - static const unsigned vKey[] = { + static const UINT vKey[] = { 0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, diff --git a/c/debug.h b/c/debug.h index f58c700..e9cefa3 100644 --- a/c/debug.h +++ b/c/debug.h @@ -1,6 +1,8 @@ #ifndef DEBUG_H #define DEBUG_H +#include + /* Benchmark objects measure and print the time in microseconds * between construction and destruction. Given a unique id, they also * calculate a running average of the last `avgmax' instances of @@ -21,6 +23,6 @@ struct Benchmark }; /* Return name of given window message (WM_*). */ -const char* WmName(unsigned uMsg); +const char* WmName(UINT uMsg); #endif diff --git a/c/listview.cpp b/c/listview.cpp index 8224c0a..9ca088c 100644 --- a/c/listview.cpp +++ b/c/listview.cpp @@ -37,11 +37,11 @@ int ListView::Height() void ListView::ResizeColumns(int) {} -void ListView::UpdateTheme(const BOOL bThemeActive) +void ListView::UpdateTheme(const bool bThemeActive) { const wchar_t* theme; WORD action; - extern BOOL (*SetWindowTheme)(HWND, LPCWSTR, LPCWSTR); + extern BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*); if (!SetWindowTheme) return; if (bThemeActive) { diff --git a/c/listview.h b/c/listview.h index 008f995..b0e49ee 100644 --- a/c/listview.h +++ b/c/listview.h @@ -17,7 +17,7 @@ struct ListView * by default). */ virtual void ResizeColumns(int w); /* Enable/disable "modern" theme. */ - virtual void UpdateTheme(BOOL bThemeActive); + virtual void UpdateTheme(bool bThemeActive); virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); protected: WNDPROC m_proc0; diff --git a/c/main.cpp b/c/main.cpp index a672c51..a06002a 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -49,16 +49,16 @@ char g_limitScreenwriter[64]; /* Optional Windows functions. */ BOOL (*IsThemeActive)(); -BOOL (*SetWindowTheme)(HWND, LPCWSTR, LPCWSTR); +BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*); /* Initialize important global state on parent window creation. */ static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM); /* Process parent window commands. */ static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* Process main menu commands. */ -static void HandleMainMenu(HWND, unsigned short); +static void HandleMainMenu(HWND, WORD); /* Process context menu commands. */ -static void HandleContextMenu(HWND, unsigned short); +static void HandleContextMenu(HWND, WORD); /* Call Prolog predicate in other thread, if available. */ static void WaitFor(const char*, const char*); /* Handle messages to Help > About dialog. */ @@ -291,7 +291,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, break; case WM_COMMAND: { - const unsigned short command = LOWORD(wParam); + const WORD command = LOWORD(wParam); switch (ID_GROUP(command)) { case IDG_MENU: HandleMainMenu(hWnd, command); @@ -344,8 +344,8 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, /*IDM_RATE10*/L"Rate episode 10/10." }; - const unsigned short command = LOWORD(wParam); - const unsigned short group = ID_GROUP(command); + const WORD command = LOWORD(wParam); + const WORD group = ID_GROUP(command); const wchar_t* tip = {0}; if (group) { const wchar_t** const vTip = group==IDG_MENU? vTipMenu: vTipCtx; @@ -373,7 +373,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, return 0; } -void HandleMainMenu(const HWND hWnd, unsigned short command) +void HandleMainMenu(const HWND hWnd, WORD command) { switch (command) { case IDM_FILE_EXIT: @@ -429,7 +429,7 @@ void HandleMainMenu(const HWND hWnd, unsigned short command) } } -void HandleContextMenu(const HWND, unsigned short command) +void HandleContextMenu(const HWND, WORD command) { int cNotFound = 0; @@ -556,7 +556,7 @@ INT_PTR CALLBACK AboutDlgProc(const HWND hWnd, const UINT uMsg, const WPARAM wPa void UpdateTheme() { if (!IsThemeActive) return; - const BOOL bThemeActive = IsThemeActive(); + const bool bThemeActive = IsThemeActive(); g_dlv->UpdateTheme(bThemeActive); g_elv->UpdateTheme(bThemeActive); } diff --git a/c/win.cpp b/c/win.cpp index 19cfaab..593b340 100644 --- a/c/win.cpp +++ b/c/win.cpp @@ -65,7 +65,7 @@ Library::~Library() FreeLibrary(m_hModule); } -int EBMessageBox(const wchar_t* const wszText, const wchar_t* const wszCaption, const unsigned uType) +int EBMessageBox(const wchar_t* const wszText, const wchar_t* const wszCaption, const UINT uType) { extern HWND g_hWnd; @@ -97,7 +97,7 @@ int EBMessageBox(const wchar_t* const wszText, const wchar_t* const wszCaption, return r; } -int GetRelativeCursorPos(HWND hWnd, POINT* pt) +int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) { RECT rc; if (!GetClientRect(hWnd, &rc)) return 0; diff --git a/c/win.h b/c/win.h index d36cbdf..ff38ce0 100644 --- a/c/win.h +++ b/c/win.h @@ -5,9 +5,11 @@ #include /* Display message box centered in main window. */ -int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); +int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, UINT uType); /* Retrieve mouse position relative to given window's client area. */ int GetRelativeCursorPos(HWND hWnd, POINT* pt); +/* Cached values from GetSystemMetrics. */ +template auto Metric = GetSystemMetrics(I); /* Exception for Windows API errors. */ struct Win32Error : public std::exception @@ -45,10 +47,6 @@ T* Library::GetProcAddress(const char* const szProc) noexcept return (T*)(void*)::GetProcAddress(m_hModule, szProc); } -/* Variable template for caching values from GetSystemMetrics. */ -template -auto Metric = GetSystemMetrics(I); - /* Check result of Windows API call, throwing error on null. */ template inline T Require(const T x) @@ -97,7 +95,7 @@ inline BOOL SetWindowRect(const HWND hWnd, const RECT r) } /* The following functions call uxtheme.dll functions, if uxtheme.dll - * exists, and otherwise do nothing.. */ + * exists, and otherwise do nothing. */ inline BOOL EBIsThemeActive() { @@ -105,9 +103,10 @@ inline BOOL EBIsThemeActive() return IsThemeActive? IsThemeActive(): 0; } -inline BOOL EBSetWindowTheme(HWND hWnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList) +inline BOOL EBSetWindowTheme(const HWND hWnd, const wchar_t* const pszSubAppName, + const wchar_t* const pszSubIdList) { - extern BOOL (*SetWindowTheme)(HWND, LPCWSTR, LPCWSTR); + extern BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*); return SetWindowTheme? SetWindowTheme(hWnd, pszSubAppName, pszSubIdList): 0; } -- cgit v1.2.3