Skip to content

Commit

Permalink
Fix DPI scaling of panel captions and pop-up volume bar
Browse files Browse the repository at this point in the history
This:

- makes panel captions scale with the system DPI setting
- fixes the incorrect positioning of text in vertical panel captions
- makes the status bar pop-up volume bar scale with the system DPI setting
  • Loading branch information
reupen committed Jan 8, 2022
1 parent ba9ce80 commit a15c85f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 69 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

* A warning was added under the 'Show toolbars' option in preferences. [[#410](https://github.com/reupen/columns_ui/pull/410)]

### Bug fixes

* Various bugs with the positioning and sizing of panel captions were fixed. [[#418](https://github.com/reupen/columns_ui/pull/418)]

* The status bar pop-up volume bar now correctly scales with the system DPI setting. [[#418](https://github.com/reupen/columns_ui/pull/418)]

### Internal changes

* Some internal changes were made in advance of support for the Windows 10 dark mode. [[#411](https://github.com/reupen/columns_ui/pull/411), [#413](https://github.com/reupen/columns_ui/pull/413), [#415](https://github.com/reupen/columns_ui/pull/415)]
Expand Down
45 changes: 15 additions & 30 deletions foo_ui_columns/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,24 @@ HBITMAP LoadMonoBitmap(INT_PTR uid, COLORREF cr_btntext)
return rv;
}

BOOL uDrawPanelTitle(HDC dc, const RECT* rc_clip, const char* text, int len, bool vert, bool is_dark)
BOOL uDrawPanelTitle(HDC dc, const RECT* rc_clip, const char* text, int len, bool is_font_vertical, bool is_dark)
{
COLORREF cr_fore = cui::dark::get_system_colour(COLOR_MENUTEXT, is_dark);

{
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, cr_fore);

SIZE sz;
uGetTextExtentPoint32(dc, text, len, &sz);
int extra = vert ? rc_clip->bottom - sz.cy : (rc_clip->bottom - rc_clip->top - sz.cy - 1) / 2;
/*
if (world)
{
SetGraphicsMode(dc, GM_ADVANCED);
XFORM xf;
xf.eM11 = 0;
xf.eM21 = 1;
xf.eDx = 0;
xf.eM12 = -1;
xf.eM22 = 0;
xf.eDy = rc_clip->right;
SetWorldTransform(dc, &xf);
}
*/
// HFONT old = SelectFont(dc, fnt_menu);
const COLORREF cr_fore = cui::dark::get_system_colour(COLOR_MENUTEXT, is_dark);

uExtTextOut(dc, 5 + rc_clip->left, extra, ETO_CLIPPED, rc_clip, text, len, nullptr);
// SelectFont(dc, old);
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, cr_fore);

return TRUE;
}
return FALSE;
SIZE sz{};
uGetTextExtentPoint32(dc, text, len, &sz);

const auto rect_text_top = is_font_vertical ? rc_clip->left : rc_clip->top;
const auto rect_text_bottom = is_font_vertical ? rc_clip->right : rc_clip->bottom;

const auto text_top_offset = rect_text_top + (rect_text_bottom - rect_text_top - sz.cy - 1) / 2;
const auto x = is_font_vertical ? text_top_offset : 4_spx;
const auto y = is_font_vertical ? rc_clip->bottom - 5_spx : text_top_offset;

return uExtTextOut(dc, rc_clip->left + x, rc_clip->top + y, ETO_CLIPPED, rc_clip, text, len, nullptr);
}

namespace cui::helpers {
Expand Down
2 changes: 1 addition & 1 deletion foo_ui_columns/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void g_compare_file_with_bytes(
const service_ptr_t<file>& p1, const pfc::array_t<t_uint8>& p2, bool& b_same, abort_callback& p_abort);

HBITMAP LoadMonoBitmap(INT_PTR uid, COLORREF cr_btntext);
BOOL uDrawPanelTitle(HDC dc, const RECT* rc_clip, const char* text, int len, bool vert, bool is_dark);
BOOL uDrawPanelTitle(HDC dc, const RECT* rc_clip, const char* text, int len, bool is_font_vertical, bool is_dark);

namespace cui::helpers {

Expand Down
4 changes: 2 additions & 2 deletions foo_ui_columns/mw_wnd_proc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ LRESULT cui::MainWindow::on_message(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
HWND wndvol = cui::status_bar::volume_popup_window.create(wnd);
POINT pt = lpnmm->pt;
ClientToScreen(lpnmm->hdr.hwndFrom, &pt);
int cx = volume_popup_t::g_get_caption_size() + 28;
int cy = 150;
int cx = volume_popup_t::g_get_caption_size() + 26_spx + 2 * 1_spx;
int cy = 144_spx + 2 * 3_spx;
int x = pt.x;
int y = pt.y;
HMONITOR mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
Expand Down
2 changes: 1 addition & 1 deletion foo_ui_columns/splitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum Orientation {
class FlatSplitterPanel : public uie::container_ui_extension_t<ui_helpers::container_window, uie::splitter_window_v2> {
public:
virtual Orientation get_orientation() const = 0;
static unsigned g_get_caption_size();
static int g_get_caption_size();
void get_category(pfc::string_base& p_out) const override;
unsigned get_type() const override;

Expand Down
10 changes: 4 additions & 6 deletions foo_ui_columns/splitter_panel_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ LRESULT FlatSplitterPanel::Panel::PanelContainer::on_message(HWND wnd, UINT msg,
RECT rc_dummy;
GetClientRect(wnd, &rc_client);

unsigned caption_size = g_get_caption_size();
const auto caption_size = g_get_caption_size();

unsigned cx
= m_this->m_panels[index]->m_caption_orientation == vertical ? caption_size : rc_client.right;
unsigned cy
= m_this->m_panels[index]->m_caption_orientation == vertical ? rc_client.bottom : caption_size;
auto cx = m_this->m_panels[index]->m_caption_orientation == vertical ? caption_size : rc_client.right;
auto cy = m_this->m_panels[index]->m_caption_orientation == vertical ? rc_client.bottom : caption_size;

RECT rc_caption = {0, 0, gsl::narrow<long>(cx), gsl::narrow<long>(cy)};
RECT rc_caption = {0, 0, cx, cy};

if (IntersectRect(&rc_dummy, &ps.rcPaint, &rc_caption)) {
const auto is_dark = dark::is_dark_mode_enabled();
Expand Down
6 changes: 2 additions & 4 deletions foo_ui_columns/splitter_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,9 @@ void FlatSplitterPanel::get_category(pfc::string_base& p_out) const
p_out = "Splitters";
}

unsigned FlatSplitterPanel::g_get_caption_size()
int FlatSplitterPanel::g_get_caption_size()
{
unsigned rv = uGetFontHeight(g_font_menu_horizontal.get());
rv += 9;
return rv;
return uGetFontHeight(g_font_menu_horizontal.get()) + 8_spx;
}

void FlatSplitterPanel::FlatSplitterPanelHost::relinquish_ownership(HWND wnd)
Expand Down
50 changes: 25 additions & 25 deletions foo_ui_columns/volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ class VolumeBar
SIZE sz{};
get_caption_extent(sz);
unsigned size_caption = get_caption_size();
const int x = b_vertical ? size_caption : sz.cx;
const int y = 0;
const int cx = LOWORD(lp) - (b_vertical ? size_caption : sz.cx);
const int cy = HIWORD(lp);
const int x = b_vertical ? size_caption + 1_spx : sz.cx;
const int y = b_vertical ? 3_spx : 0;
const int cx = LOWORD(lp) - (b_vertical ? size_caption + 2 * 1_spx : sz.cx);
const int cy = HIWORD(lp) - (b_vertical ? 2 * 3_spx : 0);
SetWindowPos(wnd_trackbar, nullptr, x, y, cx, cy, SWP_NOZORDER);
RedrawWindow(wnd, nullptr, nullptr, RDW_INVALIDATE | RDW_ERASE);
} else
Expand Down Expand Up @@ -221,26 +221,28 @@ class VolumeBar
}
} break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC dc = BeginPaint(wnd, &ps);
PAINTSTRUCT ps{};
const auto _ = wil::BeginPaint(wnd, &ps);

if (t_attributes::get_show_caption()) {
RECT rc_client, rc_dummy;
GetClientRect(wnd, &rc_client);
SIZE sz{};
get_caption_extent(sz);
auto size_caption = (long)get_caption_size();
RECT rc_caption = {0, 0, b_vertical ? size_caption : sz.cx, rc_client.bottom};
const auto size_caption = get_caption_size();
const RECT rc_caption = {0, 5_spx, size_caption - 2_spx, rc_client.bottom - 5_spx};

if (IntersectRect(&rc_dummy, &rc_caption, &ps.rcPaint)) {
HFONT old = SelectFont(dc, m_font_caption.get());
uDrawPanelTitle(dc, &rc_caption, "Volume", 6, b_vertical, false);
SelectFont(dc, old);
SetBkMode(ps.hdc, TRANSPARENT);
SetTextColor(ps.hdc, GetSysColor(COLOR_MENUTEXT));

const auto _ = wil::SelectObject(ps.hdc, m_font_caption.get());
uExtTextOut(ps.hdc, rc_caption.left, rc_caption.bottom, ETO_CLIPPED, &rc_caption, label_text.data(),
label_text.length(), nullptr);
}
}
EndPaint(wnd, &ps);
}
return 0;
}
case WM_KEYDOWN: {
auto lpkeyb = uih::GetKeyboardLParam(lp);
if (b_popup && wp == VK_ESCAPE && !lpkeyb.transition_code && !lpkeyb.previous_key_state) {
Expand Down Expand Up @@ -292,15 +294,14 @@ class VolumeBar
update_position(vol);
}
void update_position(float p_new_volume) { m_child.set_position(volume_to_position(p_new_volume)); }
static unsigned g_get_caption_size(HFONT fnt)
static int g_get_caption_size(HFONT fnt)
{
if (!t_attributes::get_show_caption())
return 0;
unsigned rv = uGetFontHeight(fnt);
rv += 9;
return rv;

return gsl::narrow<int>(uGetFontHeight(fnt)) + 2_spx;
}
static unsigned g_get_caption_size()
static int g_get_caption_size()
{
if (!t_attributes::get_show_caption())
return 0;
Expand All @@ -310,15 +311,12 @@ class VolumeBar
}

private:
unsigned get_caption_size() const { return g_get_caption_size(m_font_caption.get()); }
int get_caption_size() const { return g_get_caption_size(m_font_caption.get()); }
bool get_caption_extent(SIZE& p_out) const
{
HDC dc = GetDC(this->get_wnd());
HFONT old = SelectFont(dc, m_font_caption.get());
bool ret = uGetTextExtentPoint32(dc, "Volume", 6, &p_out) != 0;
SelectFont(dc, old);
ReleaseDC(this->get_wnd(), dc);
return ret;
const auto dc = wil::GetDC(this->get_wnd());
const auto _ = wil::SelectObject(dc.get(), m_font_caption.get());
return uGetTextExtentPoint32(dc.get(), label_text.data(), label_text.size(), &p_out) != 0;
}

void FB2KAPI on_playback_starting(play_control::t_track_command p_command, bool p_paused) override{};
Expand All @@ -332,6 +330,8 @@ class VolumeBar
void FB2KAPI on_playback_time(double p_time) override{};
void FB2KAPI on_volume_change(float p_new_val) override { update_position(p_new_val); }

constexpr static auto label_text = "Volume"sv;

wil::unique_hfont m_font_caption;
ULONG_PTR m_Gdiplus_token{NULL};
bool m_using_gdiplus{false};
Expand Down

0 comments on commit a15c85f

Please sign in to comment.