Skip to content

Commit

Permalink
tweaks to drawing about info
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Oct 28, 2023
1 parent 65f12cc commit 2d95d6b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 81 deletions.
54 changes: 24 additions & 30 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -367,8 +361,8 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& 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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
65 changes: 15 additions & 50 deletions src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <txt> in a given <hwnd>, 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 <txt> in a given <hwnd>, taking into account its font
Size TextSizeInHwnd(HWND hwnd, const WCHAR* txt, HFONT font) {
if (!txt || !*txt) {
Expand Down Expand Up @@ -2905,56 +2885,41 @@ SIZE TextSizeInHwnd2(HWND hwnd, const WCHAR* txt, HFONT font) {
return sz;
}

/* Return size of a text <txt> in a given <hwnd>, 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 <txt> in a given <hwnd>, 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 <txt> in a given <hwnd>, 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 <txt> in a given <hdc>, 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 <txt> in a given <hwnd>, 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) {
Expand Down
1 change: 0 additions & 1 deletion src/utils/WinUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2d95d6b

Please sign in to comment.