From 2d95d6b931cd0dfa592974eecc7c03484f8700d9 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Sun, 29 Oct 2023 00:13:40 +0200 Subject: [PATCH] tweaks to drawing about info --- src/HomePage.cpp | 54 ++++++++++++++++------------------- src/utils/WinUtil.cpp | 65 ++++++++++--------------------------------- src/utils/WinUtil.h | 1 - 3 files changed, 39 insertions(+), 81 deletions(-) diff --git a/src/HomePage.cpp b/src/HomePage.cpp index 0b9f0943dacc..8b24c762fdd7 100644 --- a/src/HomePage.cpp +++ b/src/HomePage.cpp @@ -35,14 +35,15 @@ #endif #define ABOUT_LINE_SEP_SIZE 1 -#define ABOUT_BORDER_COL RGB(0, 0, 0) +constexpr COLORREF kAboutBorderCol = RGB(0, 0, 0); -constexpr int ABOUT_LEFT_RIGHT_SPACE_DX = 8; -constexpr int ABOUT_MARGIN_DX = 10; -constexpr int ABOUT_BOX_MARGIN_DY = 6; -constexpr int ABOUT_TXT_DY = 6; -constexpr int ABOUT_RECT_PADDING = 8; -#define kInnerPadding 8 +constexpr int kAboutLeftRightSpaceDx = 8; +constexpr int kAboutMarginDx = 10; +constexpr int kAboutBoxMarginDy = 6; +constexpr int kAboutTxtDy = 6; +constexpr int kAboutRectPadding = 8; + +constexpr int kInnerPadding = 8; constexpr const char* kSumatraTxtFont = "Arial Black"; constexpr int kSumatraTxtFontSize = 24; @@ -193,7 +194,7 @@ static void DrawAppName(HDC hdc, Point pt) { } } -static char* GetAppVersionTemp() { +static TempStr GetAppVersionTemp() { char* s = str::DupTemp("v" CURR_VERSION_STRA); if (IsProcess64()) { s = str::JoinTemp(s, " 64-bit"); @@ -209,26 +210,18 @@ static Size CalcSumatraVersionSize(HWND hwnd, HDC hdc) { HFONT fontSumatraTxt = CreateSimpleFont(hdc, kSumatraTxtFont, kSumatraTxtFontSize); HFONT fontVersionTxt = CreateSimpleFont(hdc, kVersionTxtFont, kVersionTxtFontSize); - ScopedSelectObject selFont(hdc, fontSumatraTxt); - SIZE txtSize{}; /* calculate minimal top box size */ - const char* txt = kAppName; - - GetTextExtentPoint32Utf8(hdc, txt, (int)str::Len(txt), &txtSize); - result.dy = txtSize.cy + DpiScale(hwnd, ABOUT_BOX_MARGIN_DY * 2); - result.dx = txtSize.cx; + Size txtSize = HwndMeasureText(hwnd, kAppName, fontSumatraTxt); + result.dy = txtSize.dy + DpiScale(hwnd, kAboutBoxMarginDy * 2); + result.dx = txtSize.dx; /* consider version and version-sub strings */ - SelectObject(hdc, fontVersionTxt); - char* ver = GetAppVersionTemp(); - GetTextExtentPoint32Utf8(hdc, ver, (int)str::Len(ver), &txtSize); - LONG minWidth = txtSize.cx + DpiScale(hwnd, 8); - txt = VERSION_SUB_TXT; - GetTextExtentPoint32Utf8(hdc, txt, (int)str::Len(txt), &txtSize); - txtSize.cx = std::max(txtSize.cx, minWidth); - result.dx += 2 * (txtSize.cx + DpiScale(hwnd, kInnerPadding)); - + TempStr ver = GetAppVersionTemp(); + txtSize = HwndMeasureText(hwnd, ver, fontVersionTxt); + int minWidth = txtSize.dx + DpiScale(hwnd, 8); + int dx = std::max(txtSize.dx, minWidth); + result.dx += 2 * (dx + DpiScale(hwnd, kInnerPadding)); return result; } @@ -238,6 +231,7 @@ static void DrawSumatraVersion(HWND hwnd, HDC hdc, Rect rect) { SetBkMode(hdc, TRANSPARENT); + ScopedSelectFont f(hdc, fontSumatraTxt); SIZE txtSize; const char* txt = kAppName; GetTextExtentPoint32Utf8(hdc, txt, (int)str::Len(txt), &txtSize); @@ -367,8 +361,8 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec& stati } SelectObject(hdc, penDivideLine); - Rect divideLine(gAboutLayoutInfo[0].rightPos.x - DpiScale(hwnd, ABOUT_LEFT_RIGHT_SPACE_DX), - rect.y + titleRect.dy + 4, 0, rect.y + rect.dy - 4 - gAboutLayoutInfo[0].rightPos.y); + Rect divideLine(gAboutLayoutInfo[0].rightPos.x - DpiScale(hwnd, kAboutLeftRightSpaceDx), rect.y + titleRect.dy + 4, + 0, rect.y + rect.dy - 4 - gAboutLayoutInfo[0].rightPos.y); DrawLine(hdc, divideLine); } @@ -427,9 +421,9 @@ static void UpdateAboutLayoutInfo(HWND hwnd, HDC hdc, Rect* rect) { } } - int leftRightSpaceDx = DpiScale(hwnd, ABOUT_LEFT_RIGHT_SPACE_DX); - int marginDx = DpiScale(hwnd, ABOUT_MARGIN_DX); - int aboutTxtDy = DpiScale(hwnd, ABOUT_TXT_DY); + int leftRightSpaceDx = DpiScale(hwnd, kAboutLeftRightSpaceDx); + int marginDx = DpiScale(hwnd, kAboutMarginDx); + int aboutTxtDy = DpiScale(hwnd, kAboutTxtDy); /* calculate total dimension and position */ Rect minRect; minRect.dx = leftRightSpaceDx + leftLargestDx + ABOUT_LINE_SEP_SIZE + rightLargestDx + leftRightSpaceDx; @@ -645,7 +639,7 @@ void ShowAboutWindow(MainWindow* win) { SetLayout(hdc, LAYOUT_LTR); UpdateAboutLayoutInfo(gHwndAbout, hdc, &rc); EndPaint(gHwndAbout, &ps); - int rectPadding = DpiScale(gHwndAbout, ABOUT_RECT_PADDING); + int rectPadding = DpiScale(gHwndAbout, kAboutRectPadding); rc.Inflate(rectPadding, rectPadding); // resize the new window to just match these dimensions diff --git a/src/utils/WinUtil.cpp b/src/utils/WinUtil.cpp index 46b0971c6dea..7334217c31cb 100755 --- a/src/utils/WinUtil.cpp +++ b/src/utils/WinUtil.cpp @@ -2847,26 +2847,6 @@ void DrawCenteredText(HDC hdc, const RECT& r, const WCHAR* txt, bool isRTL) { DrawCenteredText(hdc, rc, txt, isRTL); } -// Return size of a text in a given , taking into account its font -Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) { - if (!txt || !*txt) { - return Size{}; - } - size_t txtLen = str::Len(txt); - HDC dc = GetWindowDC(hwnd); - /* GetWindowDC() returns dc with default state, so we have to first set - window's current font into dc */ - if (font == nullptr) { - font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0); - } - HGDIOBJ prev = SelectObject(dc, font); - SIZE sz{}; - GetTextExtentPoint32Utf8(dc, txt, (int)txtLen, &sz); - SelectObject(dc, prev); - ReleaseDC(hwnd, dc); - return Size(sz.cx, sz.cy); -} - // Return size of a text in a given , taking into account its font Size TextSizeInHwnd(HWND hwnd, const WCHAR* txt, HFONT font) { if (!txt || !*txt) { @@ -2905,56 +2885,41 @@ SIZE TextSizeInHwnd2(HWND hwnd, const WCHAR* txt, HFONT font) { return sz; } -/* Return size of a text in a given , taking into account its font */ -Size HwndMeasureText(HWND hwnd, const char* txt, HFONT font) { - SIZE sz{}; - size_t txtLen = str::Len(txt); - - AutoReleaseDC dc(hwnd); - /* GetWindowDC() returns dc with default state, so we have to first set - window's current font into dc */ - if (font == nullptr) { - font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0); +// Return size of a text in a given , taking into account its font +Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) { + if (!txt || !*txt) { + return Size{}; } - ScopedSelectFont prev(dc, font); - - RECT r{}; - uint fmt = DT_CALCRECT | DT_LEFT | DT_NOCLIP | DT_EDITCONTROL; - HdcDrawText(dc, txt, (int)txtLen, &r, fmt); - - int dx = RectDx(r); - int dy = RectDy(r); - return {dx, dy}; + TempWStr ws = ToWStrTemp(txt); + return TextSizeInHwnd(hwnd, ws, font); } /* Return size of a text in a given , taking into account its font */ Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font) { SIZE sz{}; - size_t txtLen = str::Len(txt); - HDC dc = GetWindowDC(hwnd); + + AutoReleaseDC dc(hwnd); /* GetWindowDC() returns dc with default state, so we have to first set window's current font into dc */ if (font == nullptr) { font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0); } - HGDIOBJ prev = SelectObject(dc, font); + ScopedSelectFont prev(dc, font); RECT r{}; uint fmt = DT_CALCRECT | DT_LEFT | DT_NOCLIP | DT_EDITCONTROL; + size_t txtLen = str::Len(txt); DrawTextExW(dc, (WCHAR*)txt, (int)txtLen, &r, fmt, nullptr); - SelectObject(dc, prev); - ReleaseDC(hwnd, dc); + int dx = RectDx(r); int dy = RectDy(r); return {dx, dy}; } -/* Return size of a text in a given , taking into account its font */ -Size TextSizeInDC(HDC hdc, const WCHAR* txt) { - SIZE sz; - size_t txtLen = str::Len(txt); - GetTextExtentPoint32(hdc, txt, (int)txtLen, &sz); - return Size(sz.cx, sz.cy); +/* Return size of a text in a given , taking into account its font */ +Size HwndMeasureText(HWND hwnd, const char* txt, HFONT font) { + TempWStr sw = ToWStrTemp(txt); + return HwndMeasureText(hwnd, sw, font); } void TreeViewExpandRecursively(HWND hTree, HTREEITEM hItem, uint flag, bool subtree) { diff --git a/src/utils/WinUtil.h b/src/utils/WinUtil.h index d0fd904cd1ef..f3d785067885 100644 --- a/src/utils/WinUtil.h +++ b/src/utils/WinUtil.h @@ -112,7 +112,6 @@ void DrawCenteredText(HDC, const RECT& r, const WCHAR* txt, bool isRTL = false); Size TextSizeInHwnd(HWND, const WCHAR*, HFONT = nullptr); Size TextSizeInHwnd(HWND, const char*, HFONT = nullptr); SIZE TextSizeInHwnd2(HWND, const WCHAR*, HFONT); -Size TextSizeInDC(HDC, const WCHAR*); bool IsFocused(HWND); bool IsCursorOverWindow(HWND);