aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-20 21:27:30 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-20 21:27:30 +0200
commit561ee240477e348efcd3670a5481ccb538d6724b (patch)
tree0bfee883963a390c657e627a2184068826168d6f
parent3f842c733568aa9068aa83fad52540eb98f334b1 (diff)
downloadEpisodeBrowser-561ee240477e348efcd3670a5481ccb538d6724b.tar.gz
Simplify UpdateLayout.
-rw-r--r--c/common.h25
-rw-r--r--c/datalistview.cpp10
-rw-r--r--c/datalistview.h1
-rw-r--r--c/episodelistview.cpp12
-rw-r--r--c/episodelistview.h1
-rw-r--r--c/listview.cpp2
-rw-r--r--c/listview.h1
-rw-r--r--c/main.cpp34
8 files changed, 63 insertions, 23 deletions
diff --git a/c/common.h b/c/common.h
index 01083b8..0343914 100644
--- a/c/common.h
+++ b/c/common.h
@@ -56,7 +56,7 @@ std::optional<T> maybe_make(U... xs)
/* Call Windows API function, throwing error on NULL. */
template <typename T>
-inline T require(T x)
+inline T require(const T x)
{
if (!x) throw Win32Error();
return x;
@@ -64,7 +64,7 @@ inline T require(T x)
/* Call Windows API function, showing a warning on NULL. */
template <typename T>
-inline T prefer(T x)
+inline T prefer(const T x)
{
if (!x) {
EBMessageBox(Win32Error().what<TCHAR>(),
@@ -88,4 +88,25 @@ inline int Cmp(const int a, const int b)
return -1;
}
+/* Get window rectangle relative to parent. */
+inline BOOL GetRelativeRect(const HWND hWnd, RECT* const pRr)
+{
+ if (!GetClientRect(hWnd, pRr)) return 0;
+ return MapWindowPoints(hWnd, GetParent(hWnd), (POINT*)pRr, 2);
+}
+
+inline BOOL SetWindowRect(const HWND hWnd,
+ const long left, const long top, const long right, const long bottom)
+{
+ return MoveWindow(hWnd,
+ left, top,
+ right-left, bottom-top,
+ TRUE);
+}
+
+inline BOOL SetWindowRect(const HWND hWnd, const RECT r)
+{
+ return SetWindowRect(hWnd, r.left, r.top, r.right, r.bottom);
+}
+
#endif
diff --git a/c/datalistview.cpp b/c/datalistview.cpp
index eace820..c191901 100644
--- a/c/datalistview.cpp
+++ b/c/datalistview.cpp
@@ -29,6 +29,16 @@ DataListView::DataListView(const HWND hWndParent)
ListView_InsertColumn(hWnd, DLVSIVALUE, &lvc);
}
+void DataListView::ResizeColumns(RECT& rcParent)
+{
+ extern int g_cxVScroll;
+ ListView_SetColumnWidth(hWnd, DLVSIKEY, LVSCW_AUTOSIZE);
+
+ const int cxColumn = ListView_GetColumnWidth(hWnd, 0)+4;
+ ListView_SetColumnWidth(hWnd, DLVSIKEY, cxColumn);
+ ListView_SetColumnWidth(hWnd, DLVSIVALUE, rcParent.right-cxColumn-g_cxVScroll-4);
+}
+
void DataListView::ShowEpisode(const int iEpisode)
{
extern EpisodeListView* const g_pElv;
diff --git a/c/datalistview.h b/c/datalistview.h
index 4ff61fd..ad35f25 100644
--- a/c/datalistview.h
+++ b/c/datalistview.h
@@ -9,6 +9,7 @@
struct DataListView : public ListView
{
DataListView(HWND hWndParent);
+ void ResizeColumns(RECT& rcParent) override;
void ShowEpisode(int iEpisode);
};
diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp
index 676f375..6f12a0f 100644
--- a/c/episodelistview.cpp
+++ b/c/episodelistview.cpp
@@ -141,6 +141,18 @@ void EpisodeListView::Redraw()
RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|RDW_ALLCHILDREN);
}
+void EpisodeListView::ResizeColumns(RECT& rcParent)
+{
+ extern int g_cxVScroll;
+ ListView_SetColumnWidth(hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE);
+
+ int cxColumn = ListView_GetColumnWidth(hWnd, ELVSIEPISODE)+4;
+ ListView_SetColumnWidth(hWnd, ELVSIEPISODE, cxColumn);
+
+ cxColumn += ListView_GetColumnWidth(hWnd, ELVSIRATING);
+ ListView_SetColumnWidth(hWnd, ELVSITITLE, rcParent.right-cxColumn-g_cxVScroll-4);
+}
+
/* Select previously focused episode. */
void EpisodeListView::RestoreFocus()
{
diff --git a/c/episodelistview.h b/c/episodelistview.h
index 48e7194..3b32b6f 100644
--- a/c/episodelistview.h
+++ b/c/episodelistview.h
@@ -16,6 +16,7 @@ struct EpisodeListView : public ListView
void EnsureFocusVisible();
LRESULT HandleNotify(LPARAM lParam);
void Redraw();
+ void ResizeColumns(RECT& rcParent) override;
void RestoreFocus();
void SaveFocus();
void SetTop(int iItem);
diff --git a/c/listview.cpp b/c/listview.cpp
index 319433a..72c2c60 100644
--- a/c/listview.cpp
+++ b/c/listview.cpp
@@ -38,6 +38,8 @@ int ListView::Height(int bHeader)
return iCount? Dpi(bHeader? 27: 4)+iCount*Dpi(19): 0;
}
+void ListView::ResizeColumns(RECT&) {}
+
void ListView::UpdateTheme(const BOOL bThemeActive)
{
DWORD dwStyle;
diff --git a/c/listview.h b/c/listview.h
index 31ecc79..6254c2d 100644
--- a/c/listview.h
+++ b/c/listview.h
@@ -9,6 +9,7 @@ struct ListView
ListView(HWND hWndParent, HMENU hMenu, DWORD dwStyle);
int Height(int bHeader = -1);
+ virtual void ResizeColumns(RECT& rcParent);
virtual void UpdateTheme(BOOL bThemeActive);
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
protected:
diff --git a/c/main.cpp b/c/main.cpp
index 219f4b8..3746bb6 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -17,6 +17,7 @@ int g_bThread;
/* Looked-up constants. */
int g_bThemes;
+int g_cxVScroll;
int g_iDPI = 96;
/* Fonts. */
@@ -159,6 +160,8 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
g_hWnd = (HWND)wParam;
/* Look up constants. */
+ g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
+
if (auto opLib = maybe_make<Library>(TEXT("User32.dll")))
if (auto GetDpiForWindow = opLib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
g_iDPI = GetDpiForWindow(g_hWnd);
@@ -502,32 +505,21 @@ INT_PTR CALLBACK AboutDlgProc(const HWND hWnd, const UINT uMsg, const WPARAM wPa
void UpdateLayout()
{
- int cxColumn, cyDlv, yStatus;
- RECT rc, rcStatus;
- static int cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
+ if (!g_hWndStatus) return;
- GetClientRect(g_hWnd, &rc);
- GetClientRect(g_hWndStatus, &rcStatus);
- yStatus = rcStatus.bottom-rcStatus.top;
+ RECT rc, rrStatus;
+ require(GetClientRect(g_hWnd, &rc));
+ require(GetRelativeRect(g_hWndStatus, &rrStatus));
+ /* Resize list views. */
SendMessage(g_pDlv->hWnd, WM_SETREDRAW, FALSE, 0);
SendMessage(g_pElv->hWnd, WM_SETREDRAW, FALSE, 0);
- /* Resize data list view. */
- cyDlv = rc.bottom-yStatus-g_pDlv->Height();
- 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(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);
- cxColumn += ListView_GetColumnWidth(g_pElv->hWnd, ELVSIRATING);
- ListView_SetColumnWidth(g_pElv->hWnd, ELVSITITLE, rc.right-cxColumn-cxVScroll-4);
+ const long cyDlv = rrStatus.top-g_pDlv->Height();
+ require(SetWindowRect(g_pDlv->hWnd, 0, cyDlv, rc.right, rrStatus.top));
+ require(SetWindowRect(g_pElv->hWnd, 0, 0, rc.right, cyDlv+IsThemeActive()));
+ g_pDlv->ResizeColumns(rc);
+ g_pElv->ResizeColumns(rc);
SendMessage(g_pElv->hWnd, WM_SETREDRAW, TRUE, 0);
SendMessage(g_pDlv->hWnd, WM_SETREDRAW, TRUE, 0);