aboutsummaryrefslogtreecommitdiff
path: root/c/main.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-31 19:56:53 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-31 19:58:54 +0200
commitd9e1caa37e53c7dbc42b1fc652efc23a40c47c42 (patch)
treed2a1608a3c88fef98af9a356a116af3551ea5680 /c/main.cpp
parentd9bafcecfd60f38f98bca8a1705f6007b39e18a2 (diff)
downloadEpisodeBrowser-d9e1caa37e53c7dbc42b1fc652efc23a40c47c42.tar.gz
Limit use of Hungarian notation.
I don't hate Hungarian notation. It has some very nice qualities. But it also adds a lot of typing. That said, not using it feels a bit... unsafe. I might go back on this decision. We'll see.
Diffstat (limited to 'c/main.cpp')
-rw-r--r--c/main.cpp225
1 files changed, 111 insertions, 114 deletions
diff --git a/c/main.cpp b/c/main.cpp
index 7af5a0b..f896ef3 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -13,14 +13,14 @@
/* Looked-up constants. */
int g_bThemes;
-int g_iDPI = 96;
+int g_dpi = 96;
/* Fonts. */
HFONT g_hfNormal;
HFONT g_hfBold;
/* Menus. */
-HMENU g_hPopupMenu;
+HMENU g_hMenuPopup;
/* Windows. */
HWND g_hWndFocus;
@@ -28,13 +28,13 @@ HWND g_hWnd;
HWND g_hWndStatus;
/* Child window objects. */
-DataListView* g_pDlv;
-EpisodeListView* g_pElv;
+DataListView* g_dlv;
+EpisodeListView* g_elv;
/* View settings. */
int g_bViewWatched = 1;
int g_bViewTVOriginal = 1;
-char g_szLimitScreenwriter[64];
+char g_currentScreenwriter[64];
static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
@@ -46,28 +46,28 @@ static void UpdateTheme();
void OnTerminate() noexcept
{
- const wchar_t* wszWhat = L"an exception";
- wstring_owner wsoWhy;
+ const wchar_t* what = L"an exception";
+ wstring_owner why;
try {
std::rethrow_exception(std::current_exception());
} catch (const term_t& t) {
- wszWhat = L"a Prolog exception";
- try { wsoWhy = PlString(t); } catch (...) {}
+ what = L"a Prolog exception";
+ try { why = PlString(t); } catch (...) {}
} catch (const Win32Error& e) {
- wszWhat = L"a Windows error";
- try { wsoWhy = WsoCopy(e.WhatW()); } catch (...) {}
+ what = L"a Windows error";
+ try { why = WsoCopy(e.WhatW()); } catch (...) {}
} catch (const std::exception& e) {
- try { wsoWhy = WsoFromSz(e.what()); } catch (...) {}
+ try { why = WsoFromSz(e.what()); } catch (...) {}
} catch (...) {}
- wchar_t wsz[256] = {0};
- if (wsoWhy)
- wszf(wsz, L"Episode Browser was terminated due to %s: %s", wszWhat, wsoWhy.p);
+ wchar_t msg[256] = {0};
+ if (why)
+ wszf(msg, L"Episode Browser was terminated due to %s: %s", what, why.p);
else
- wszf(wsz, L"Episode Browser was terminated due to %s.", wszWhat);
+ wszf(msg, L"Episode Browser was terminated due to %s.", what);
- MessageBox(g_hWnd, wsz, L"Fatal Error", MB_ICONERROR);
+ MessageBox(g_hWnd, msg, L"Fatal Error", MB_ICONERROR);
_Exit(1);
}
@@ -89,8 +89,8 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
icc.dwICC = ICC_WIN95_CLASSES;
require(InitCommonControlsEx(&icc));
- g_hPopupMenu = require(LoadMenu((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU)));
- g_hPopupMenu = require(GetSubMenu(g_hPopupMenu, 0));
+ g_hMenuPopup = require(LoadMenu((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU)));
+ g_hMenuPopup = require(GetSubMenu(g_hMenuPopup, 0));
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
@@ -131,8 +131,8 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
/* Populate episode list view. */
Pl("track_episodes","update_tracked_episodes");
- g_pElv->Update();
- g_pElv->RestoreFocus();
+ g_elv->Update();
+ g_elv->RestoreFocus();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
@@ -157,16 +157,16 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
g_hWnd = (HWND)wParam;
/* Look up constants. */
- if (auto opLib = maybe_make<Library>(L"User32.dll");
- auto GetDpiForWindow = opLib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
- g_iDPI = GetDpiForWindow(g_hWnd);
+ if (auto lib = maybe_make<Library>(L"User32.dll");
+ auto GetDpiForWindow = lib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
+ g_dpi = GetDpiForWindow(g_hWnd);
- if (auto opLib = maybe_make<Library>(L"uxtheme.dll");
- opLib->GetProcAddress<void>("SetWindowTheme"))
+ if (auto lib = maybe_make<Library>(L"uxtheme.dll");
+ lib->GetProcAddress<void>("SetWindowTheme"))
g_bThemes = 1;
- if (auto opLib = maybe_make<Library>(L"User32.dll");
- opLib->GetProcAddress<void>("SystemParametersInfo" WA)) {
+ if (auto lib = maybe_make<Library>(L"User32.dll");
+ lib->GetProcAddress<void>("SystemParametersInfo" WA)) {
NONCLIENTMETRICS m;
m.cbSize = sizeof(NONCLIENTMETRICS);
require(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
@@ -181,15 +181,15 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
g_hfBold = require(CreateFontIndirect(&lf));
/* Create child windows. */
- g_pDlv = new DataListView(g_hWnd);
- g_pElv = new EpisodeListView(g_hWnd);
+ g_dlv = new DataListView(g_hWnd);
+ g_elv = new EpisodeListView(g_hWnd);
/* Get saved view settings. */
- char* sz;
+ char* s;
Pl("cfg","get_view_watched",&g_bViewWatched);
Pl("cfg","get_view_tv_original",&g_bViewTVOriginal);
- if (Pl("cfg","get_limit_screenwriter",&sz))
- strcpy_s(g_szLimitScreenwriter, sizeof(g_szLimitScreenwriter), sz);
+ if (Pl("cfg","get_limit_screenwriter",&s))
+ strcpy_s(g_currentScreenwriter, sizeof(g_currentScreenwriter), s);
return 0;
}
@@ -199,7 +199,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
case WM_CREATE:
UpdateTheme();
SetWindowPos(hWnd, NULL, -1, -1, Dpi(510), Dpi(412), SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE);
- SetFocus(g_pElv->hWnd);
+ SetFocus(g_elv->hWnd);
/* Set menu item checkmarks according to saved settings. */
CheckMenuItem(GetMenu(hWnd), IDM_VIEW_WATCHED,
@@ -207,13 +207,13 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
CheckMenuItem(GetMenu(hWnd), IDM_VIEW_TV_ORIGINAL,
g_bViewTVOriginal? MF_CHECKED: MF_UNCHECKED);
CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS,
- g_szLimitScreenwriter[0]? MF_UNCHECKED: MF_CHECKED);
+ g_currentScreenwriter[0]? MF_UNCHECKED: MF_CHECKED);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
- g_pElv->SaveFocus();
+ g_elv->SaveFocus();
PostQuitMessage(0);
break;
case WM_SIZE:
@@ -221,30 +221,27 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
UpdateLayout(LOWORD(lParam), HIWORD(lParam));
break;
case WM_GETMINMAXINFO:
- {
- MINMAXINFO* const pMMI = (MINMAXINFO*)lParam;
- pMMI->ptMinTrackSize.x = Dpi(220);
- pMMI->ptMinTrackSize.y = Dpi(220);
+ ((MINMAXINFO*)lParam)->ptMinTrackSize.x = Dpi(220);
+ ((MINMAXINFO*)lParam)->ptMinTrackSize.y = Dpi(220);
break;
- }
case WM_THEMECHANGED:
UpdateTheme();
UpdateLayout();
break;
case 0x02E0: /* WM_DPICHANGED */
{
- const RECT* const lpr = (RECT*)lParam;
+ const RECT* const r = (RECT*)lParam;
/* Update DPI and cached metrics. */
- g_iDPI = HIWORD(wParam);
+ g_dpi = HIWORD(wParam);
Metric<SM_CXVSCROLL> = GetSystemMetrics(SM_CXVSCROLL);
prefer(SetWindowPos(hWnd, (HWND)NULL,
- lpr->left, lpr->top,
- lpr->right-lpr->left,
- lpr->bottom-lpr->top,
+ r->left, r->top,
+ r->right-r->left,
+ r->bottom-r->top,
SWP_NOZORDER|SWP_NOACTIVATE));
- UpdateLayout(lpr->right-lpr->left, lpr->bottom-lpr->top);
+ UpdateLayout(r->right-r->left, r->bottom-r->top);
break;
}
case WM_ACTIVATE:
@@ -256,35 +253,35 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
case WA_CLICKACTIVE:
SetFocus(g_hWndFocus);
Pl("track_episodes","update_tracked_episodes");
- g_pElv->Redraw();
+ g_elv->Redraw();
}
break;
case WM_NOTIFY:
switch (((NMHDR*)lParam)->idFrom) {
case IDC_EPISODELISTVIEW:
- return g_pElv->HandleNotify(lParam);
+ return g_elv->HandleNotify(lParam);
}
break;
case WM_COMMAND:
{
- const unsigned short wCommand = LOWORD(wParam);
- switch (ID_GROUP(wCommand)) {
+ const unsigned short command = LOWORD(wParam);
+ switch (ID_GROUP(command)) {
case IDG_MENU:
- WndProcMainMenu(hWnd, wCommand);
+ WndProcMainMenu(hWnd, command);
break;
case IDG_CTX:
- WndProcContextMenu(hWnd, wCommand);
+ WndProcContextMenu(hWnd, command);
break;
}
break;
}
case WM_MENUSELECT:
{
- /* Look up status bar help for menu command. The help
+ /* Look up status bar tip for menu command. The tip
* strings are stored in arrays, whose indices
* correspond to the IDM_ values (see resource.h). */
- const wchar_t* aWszMenu[] = {
+ const wchar_t* vTipMenu[] = {
/*IDM_FILE_EXIT*/L"Close Episode Browser.",
/*IDM_FILE_REFRESH*/L"Quickly refresh episode list.",
/*IDM_FILE_FETCH_DATA*/L"Fetch episode data from the web (may take a few seconds).",
@@ -296,11 +293,11 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
/*IDM_VIEW_TV_ORIGINAL*/(g_bViewTVOriginal?
L"Click to hide TV original episodes.":
L"Click to show TV original episodes."),
- /*IDM_VIEW_OTHERS*/(g_szLimitScreenwriter?
+ /*IDM_VIEW_OTHERS*/(g_currentScreenwriter?
L"Click to hide episodes by other screenwriters.":
L"Click to show episodes by other screenwriters.")
};
- const wchar_t* aWszCtx[] = {
+ const wchar_t* vTipCtx[] = {
/*IDM_WATCH_LOCALLY*/L"Open local copy of episode, if available.",
/*IDM_WATCH_ONLINE*/L"Open episode in the web browser.",
/*IDM_TOGGLE*/L"Toggle watched/unwatched status.",
@@ -320,14 +317,14 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
/*IDM_RATE10*/L"Rate episode 10/10."
};
- const unsigned short wCommand = LOWORD(wParam);
- const unsigned short wGroup = ID_GROUP(wCommand);
- const wchar_t* wsz = {0};
- if (wGroup) {
- const wchar_t** aWsz = wGroup==IDG_MENU? aWszMenu: aWszCtx;
- wsz = aWsz[ID_INDEX(wCommand)];
+ const unsigned short command = LOWORD(wParam);
+ const unsigned short group = ID_GROUP(command);
+ const wchar_t* tip = {0};
+ if (group) {
+ const wchar_t** const vTip = group==IDG_MENU? vTipMenu: vTipCtx;
+ tip = vTip[ID_INDEX(command)];
}
- SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(0,0), (LPARAM)wsz);
+ SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(0,0), (LPARAM)tip);
break;
}
default:
@@ -338,14 +335,14 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam,
}
/* Process main menu commands. */
-void WndProcMainMenu(const HWND hWnd, unsigned short wCommand)
+void WndProcMainMenu(const HWND hWnd, unsigned short command)
{
- switch (wCommand) {
+ switch (command) {
case IDM_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case IDM_FILE_REFRESH:
- g_pElv->Update();
+ g_elv->Update();
break;
case IDM_FILE_FETCH_DATA:
WaitFor("episode_data","update_episode_data");
@@ -366,46 +363,46 @@ void WndProcMainMenu(const HWND hWnd, unsigned short wCommand)
g_bViewWatched? MF_UNCHECKED: MF_CHECKED);
g_bViewWatched = !g_bViewWatched;
Pl("cfg","set_view_watched",g_bViewWatched);
- g_pElv->Update();
- g_pElv->EnsureFocusVisible();
+ 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);
g_bViewTVOriginal = !g_bViewTVOriginal;
Pl("cfg","set_view_tv_original",g_bViewTVOriginal);
- g_pElv->Update();
- g_pElv->EnsureFocusVisible();
+ g_elv->Update();
+ g_elv->EnsureFocusVisible();
break;
case IDM_VIEW_OTHERS: /* Show/hide other screenwriters. */
- if (g_szLimitScreenwriter[0]) {
+ if (g_currentScreenwriter[0]) {
CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS,
MF_CHECKED);
- g_szLimitScreenwriter[0] = 0;
+ g_currentScreenwriter[0] = 0;
} else {
- const int iEpFocus = ListView_GetNextItem(g_pElv->hWnd, -1, LVNI_FOCUSED);
+ const int iEpFocus = ListView_GetNextItem(g_elv->hWnd, -1, LVNI_FOCUSED);
if (iEpFocus == -1) break;
LVITEM lvi = {LVIF_PARAM, iEpFocus};
- if (!ListView_GetItem(g_pElv->hWnd, &lvi)) break;
+ if (!ListView_GetItem(g_elv->hWnd, &lvi)) break;
- char* sz;
- if (!Pl("episode_data","episode_datum",lvi.lParam,"Screenwriter",&sz))
+ char* s;
+ if (!Pl("episode_data","episode_datum",lvi.lParam,"Screenwriter",&s))
break;
- strcpy_s(g_szLimitScreenwriter,
- sizeof(g_szLimitScreenwriter), sz);
+ strcpy_s(g_currentScreenwriter,
+ sizeof(g_currentScreenwriter), s);
CheckMenuItem(GetMenu(hWnd), IDM_VIEW_OTHERS,
MF_UNCHECKED);
}
- Pl("cfg","set_limit_screenwriter",g_szLimitScreenwriter);
- g_pElv->Update();
- g_pElv->EnsureFocusVisible();
+ Pl("cfg","set_limit_screenwriter",g_currentScreenwriter);
+ g_elv->Update();
+ g_elv->EnsureFocusVisible();
break;
}
}
/* Process context menu commands. */
-void WndProcContextMenu(const HWND, unsigned short wCommand)
+void WndProcContextMenu(const HWND, unsigned short command)
{
int cNotFound = 0;
@@ -413,17 +410,17 @@ void WndProcContextMenu(const HWND, unsigned short wCommand)
* selected command to each one. */
LVITEM lvi = {LVIF_PARAM, -1};
- while (g_pElv->FindNextItem(&lvi, LVNI_SELECTED)) {
+ while (g_elv->FindNextItem(&lvi, LVNI_SELECTED)) {
/* Process rate commands. */
- if (ID_SUBGROUP(wCommand) == IDG_CTX_RATE) {
- Pl("episode_data","rate_episode",lvi.lParam,ID_RATING(wCommand));
- g_pElv->UpdateItem(lvi.iItem, lvi.lParam);
+ if (ID_SUBGROUP(command) == IDG_CTX_RATE) {
+ Pl("episode_data","rate_episode",lvi.lParam,ID_RATING(command));
+ g_elv->UpdateItem(lvi.iItem, lvi.lParam);
continue;
}
/* Process other commands. */
- switch (wCommand) {
+ switch (command) {
case IDM_WATCH_LOCALLY:
if (!Pl("local_episode","open_episode_locally",lvi.lParam))
cNotFound++;
@@ -440,8 +437,8 @@ void WndProcContextMenu(const HWND, unsigned short wCommand)
break;
case IDM_LOOKUP:
Pl("episode_data","retract_episode",lvi.lParam);
- g_pElv->UpdateItem(lvi.iItem, lvi.lParam);
- g_pDlv->ShowEpisode(lvi.lParam);
+ g_elv->UpdateItem(lvi.iItem, lvi.lParam);
+ g_dlv->ShowEpisode(lvi.lParam);
break;
case IDM_WIKI:
Pl("episode_data","open_episode_wiki",lvi.lParam);
@@ -449,38 +446,38 @@ void WndProcContextMenu(const HWND, unsigned short wCommand)
}
}
- g_pElv->Redraw();
+ g_elv->Redraw();
if (cNotFound == 1) {
EBMessageBox(L"Episode could not be opened locally.",
L"Error", MB_ICONWARNING);
} else if (cNotFound) {
- wchar_t wsz[64] = {0};
- wszf(wsz, L"%d episodes could not be opened locally.", cNotFound);
- EBMessageBox(wsz, L"Error", MB_ICONWARNING);
- } else if (ID_SUBGROUP(wCommand) == IDG_CTX_RATE) {
- g_pElv->Sort();
- g_pElv->ShowFocus();
+ wchar_t msg[64] = {0};
+ wszf(msg, L"%d episodes could not be opened locally.", cNotFound);
+ EBMessageBox(msg, L"Error", MB_ICONWARNING);
+ } else if (ID_SUBGROUP(command) == IDG_CTX_RATE) {
+ g_elv->Sort();
+ g_elv->ShowFocus();
}
}
/* Call Prolog predicate in other thread, if available. */
-void WaitFor(const char* szMod, const char* szPred)
+void WaitFor(const char* mod, const char* pred)
{
static atom_t aThread;
static int bActive;
static int iTimer;
- static wstring_owner wsoPred;
+ static wstring_owner activePred;
if (bActive) {
- wchar_t wsz[256] = {0};
- wszf(wsz, L"Another task (%s) is active. "
+ wchar_t msg[256] = {0};
+ wszf(msg, L"Another task (%s) is active. "
L"Do you want to cancel the existing task and start a new one?",
- wsoPred.p);
- if (EBMessageBox(wsz, L"Error", MB_YESNO|MB_ICONWARNING) != IDYES)
+ activePred.p);
+ if (EBMessageBox(msg, L"Error", MB_YESNO|MB_ICONWARNING) != IDYES)
return;
KillTimer(NULL, iTimer);
bActive = 0;
- g_pElv->Update();
+ g_elv->Update();
}
/* The timer procedure animates an ellipsis in the status bar
@@ -498,14 +495,14 @@ void WaitFor(const char* szMod, const char* szPred)
KillTimer(NULL, iTimer);
i = 0;
bActive = 0;
- g_pElv->Update();
+ g_elv->Update();
}
};
- Plx(szMod,"thread_create",szPred,&aThread);
+ Plx(mod,"thread_create",pred,&aThread);
SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)L".");
if (prefer(iTimer = SetTimer(NULL, -1, 500, proc))) {
- wsoPred = WsoFromSz(szPred);
+ activePred = WsoFromSz(pred);
bActive = 1;
}
}
@@ -544,11 +541,11 @@ void UpdateLayout(int w, int h)
/* Resize list views. */
const long pad = IsThemeActive()? Dpi(6): 0; /* Add padding in modern themes. */
- const long cyDlv = rrStatus.top-g_pDlv->Height()-pad;
- require(SetWindowRect(g_pDlv->hWnd, pad, cyDlv, rc.right-pad, rrStatus.top-pad));
- require(SetWindowRect(g_pElv->hWnd, pad, pad, rc.right-pad, cyDlv-pad));
- g_pDlv->ResizeColumns(rc.right-pad-pad);
- g_pElv->ResizeColumns(rc.right-pad-pad);
+ const long cyDlv = rrStatus.top-g_dlv->Height()-pad;
+ require(SetWindowRect(g_dlv->hWnd, pad, cyDlv, rc.right-pad, rrStatus.top-pad));
+ require(SetWindowRect(g_elv->hWnd, pad, pad, rc.right-pad, cyDlv-pad));
+ g_dlv->ResizeColumns(rc.right-pad-pad);
+ g_elv->ResizeColumns(rc.right-pad-pad);
/* Resize status bar parts. */
const int aParts[] = {rc.right-Dpi(55), rc.right};
@@ -564,6 +561,6 @@ void UpdateTheme()
{
if (!g_bThemes) return;
const BOOL bThemeActive = IsThemeActive();
- g_pDlv->UpdateTheme(bThemeActive);
- g_pElv->UpdateTheme(bThemeActive);
+ g_dlv->UpdateTheme(bThemeActive);
+ g_elv->UpdateTheme(bThemeActive);
}