From 822877c6d6d019f725e7696855cf1996b2138ab0 Mon Sep 17 00:00:00 2001 From: Walker Thompson Date: Fri, 17 Apr 2026 11:09:37 +0000 Subject: Add DPI scaling for TOTP text field under GTK+ --- totpgen.h | 2 ++ wxgui.cpp | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/totpgen.h b/totpgen.h index f1a368d..8feee9a 100644 --- a/totpgen.h +++ b/totpgen.h @@ -16,6 +16,7 @@ #include #include #include +#include static const std::string base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; @@ -50,6 +51,7 @@ class TOTPGen : public wxFrame { wxBoxSizer *TextSizer; wxStaticText *TOTPField; + void ResizeTextToFit(wxStaticText *text_field); wxString SecretFile; diff --git a/wxgui.cpp b/wxgui.cpp index 6a6873a..846c042 100644 --- a/wxgui.cpp +++ b/wxgui.cpp @@ -19,7 +19,7 @@ bool TOTPGenGUI::OnInit() TOTPGen::TOTPGen() : wxFrame(nullptr, wxID_ANY, wxT("TOTP Generator"), wxDefaultPosition, wxSize(320,240), - wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER) + wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)) { MainWindow = new wxPanel(this, wxID_ANY); MainSizer = new wxBoxSizer(wxVERTICAL); @@ -43,6 +43,7 @@ TOTPGen::TOTPGen() : wxFrame(nullptr, wxID_ANY, wxT("TOTP Generator"), wxFONTWEIGHT_NORMAL); TOTPField->SetFont(LabelFont); TOTPField->SetLabel(" "); + ResizeTextToFit(TOTPField); TextSizer = new wxBoxSizer(wxHORIZONTAL); TextSizer->Add(TOTPField, 2, wxCENTER); @@ -152,7 +153,7 @@ void TOTPGen::UpdateSecret(wxCommandEvent& WXUNUSED(event)) Secret = Secret_New; EncryptSecret(Secret); - if (Locked) { + if (Locked == true) { Locked = false; MainWindow->Connect(CountdownTimer->GetId(), wxEVT_TIMER, @@ -213,4 +214,35 @@ void TOTPGen::CopyText(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->SetData(new wxTextDataObject(ToCopy)); wxTheClipboard->Close(); } -} \ No newline at end of file +} + +void TOTPGen::ResizeTextToFit(wxStaticText* text_field) +{ + wxString text = text_field->GetLabel(); + wxSize text_size = text_field->GetClientSize(); +#if defined(__WXGTK__) + double GTK_scale = std::stod(getenv("GDK_DPI_SCALE")); + text_size.x /= GTK_scale; + text_size.y /= GTK_scale; +#endif + + wxFont font = text_field->GetFont(); + int font_size = font.GetPointSize(); + + wxClientDC device_ctx(text_field); + device_ctx.SetFont(font); + + int text_width, text_height; + device_ctx.GetTextExtent(text, &text_width, &text_height); + + while (text_width > (text_size.x - 12) + || text_height > (text_size.y - 12)) { + font_size--; + font.SetPointSize(font_size); + device_ctx.SetFont(font); + device_ctx.GetTextExtent(text, &text_width, &text_height); + } + + text_field->SetFont(font); + text_field->Refresh(); +} -- cgit v1.3