Skip to content

Commit

Permalink
Revert "Use device-independent pixels in MyFrame (aardappel#364)"
Browse files Browse the repository at this point in the history
This reverts commit 624151b.
  • Loading branch information
arizvisa committed Aug 30, 2024
1 parent c21a4a8 commit 16b1f39
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
52 changes: 40 additions & 12 deletions src/myframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct MyFrame : wxFrame {
MyApp *app;
wxFileSystemWatcher *watcher;
bool watcherwaitingforuser;
double csf; // TODO: functions using this attribute should be modified to handle device-independent pixels
double csf, csf_orig;
std::vector<std::string> scripts_in_menu;
bool zenmode;
ColorDropdown *celldd = nullptr;
Expand Down Expand Up @@ -122,6 +122,37 @@ struct MyFrame : wxFrame {

app->AddTranslation(GetDataPath("translations"));

csf = GetContentScaleFactor();
wxLogMessage(L"content scale: %f", csf);
csf_orig = csf;
#ifdef __WXMSW__
// On Windows, I get csf == 1.25, as indicated in the display properties.
// With this factor set, bitmaps display. At their same physical sizes as when
// TreeSheets was a non-DPI-aware app, and extra resolution is used.
#endif
#ifdef __WXMAC__
// Typically csf == 2 on a retina mac. But on the mac, unlike Windows, image rendering
// *already* does scaling, and no way to turn that behavior off for now?
csf = 1.0;
// FIXME: This gives us low res images even though the display is capable of better!
// Apparently still not fixed: http://trac.wxwidgets.org/ticket/15808
// wxBitmap::CreateScaled could be the way to solve this, but it is not obvious
// how to use it, since you can't pass this scale to LoadFile etc. Could possibly
// blit it over via a MemoryDC?
#endif
#ifdef __WXGTK__
// On X11 platform, there is no scaling factor beside 1.0, but the DPI can be changed.

// On Wayland platform, there might be a scaling factor beside 1.0,
// but the DPI is assumed to be a fixed value by default.

// If upscaling is active in Wayland compositors, the reported bitmap to paint on is downsized (and vice versa).
// The scaling is handled by the GTK toolkit and the Wayland compositor.
// This is why csf should be set to 1.0 to let scaling be handled by the
// toolkit and compositor and not to be handled by TreeSheets itself.
csf = 1.0;
#endif

wxInitAllImageHandlers();

wxIconBundle icons;
Expand Down Expand Up @@ -640,15 +671,16 @@ struct MyFrame : wxFrame {

wxString iconpath =
GetDataPath(iconset ? L"images/webalys/toolbar/" : L"images/nuvola/toolbar/");
auto sz = FromDIP(iconset ? wxSize(18, 18) : wxSize(22, 22));
auto sz = (iconset ? wxSize(18, 18) : wxSize(22, 22)) * csf;
tb->SetToolBitmapSize(sz);

double sc = iconset ? 1.0 : 22.0 / 48.0;

auto AddTBIcon = [&](const wxChar *name, int action, wxString file) {
wxBitmap bm;
if (bm.LoadFile(file, wxBITMAP_TYPE_PNG)) {
ScaleBitmap(bm, sc, bm);
auto ns = csf_orig * sc;
ScaleBitmap(bm, ns, bm);
tb->AddTool(action, name, bm, bm, wxITEM_NORMAL, name);
}
};
Expand All @@ -672,27 +704,23 @@ struct MyFrame : wxFrame {
tb->AddSeparator();
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Search ")));
tb->AddControl(filter =
new wxTextCtrl(tb, A_SEARCH, "", wxDefaultPosition, FromDIP(wxSize(80, 22)), wxWANTS_CHARS | wxTE_PROCESS_ENTER));
AddTBIcon(_(L"Clear search"), A_CLEARSEARCH, iconpath + L"cancel.png");
new wxTextCtrl(tb, A_SEARCH, "", wxDefaultPosition, wxSize(80, 22) * csf, wxTE_PROCESS_ENTER));
AddTBIcon(_(L"Go to Next Search Result"), A_SEARCHNEXT, iconpath + L"search.png");
SEPARATOR;
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Replace ")));
tb->AddControl(replaces =
new wxTextCtrl(tb, A_REPLACE, "", wxDefaultPosition, FromDIP(wxSize(80, 22)), wxWANTS_CHARS | wxTE_PROCESS_ENTER));
AddTBIcon(_(L"Clear replace"), A_CLEARREPLACE, iconpath + L"cancel.png");
AddTBIcon(_(L"Replace in selection"), A_REPLACEONCE, iconpath + L"replace.png");
AddTBIcon(_(L"Replace All"), A_REPLACEALL, iconpath + L"replaceall.png");
new wxTextCtrl(tb, A_REPLACE, "", wxDefaultPosition, wxSize(60, 22) * csf));
tb->AddSeparator();
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Cell ")));
celldd = new ColorDropdown(tb, A_CELLCOLOR, 1);
celldd = new ColorDropdown(tb, A_CELLCOLOR, csf, 1);
tb->AddControl(celldd);
SEPARATOR;
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Text ")));
textdd = new ColorDropdown(tb, A_TEXTCOLOR, 2);
textdd = new ColorDropdown(tb, A_TEXTCOLOR, csf, 2);
tb->AddControl(textdd);
SEPARATOR;
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Border ")));
borddd = new ColorDropdown(tb, A_BORDCOLOR, 7);
borddd = new ColorDropdown(tb, A_BORDCOLOR, csf, 7);
tb->AddControl(borddd);
tb->AddSeparator();
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Image ")));
Expand Down
23 changes: 14 additions & 9 deletions src/mywxtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ struct ColorPopup : wxVListBoxComboPopup {
};

struct ColorDropdown : wxOwnerDrawnComboBox {
double csf;

ColorDropdown(wxWindow *parent, wxWindowID id, int sel) {
ColorDropdown(wxWindow *parent, wxWindowID id, double _csf, int sel) {
csf = _csf;
wxArrayString as;
as.Add(L"", sizeof(celltextcolors) / sizeof(uint));
Create(parent, id, L"", wxDefaultPosition, FromDIP(wxSize(44, 22)), as,
Create(parent, id, L"", wxDefaultPosition, wxSize(44, 22) * csf, as,
wxCB_READONLY | wxCC_SPECIAL_DCLICK);
SetPopupControl(new ColorPopup(this));
SetSelection(sel);
SetPopupMaxHeight(wxDisplay().GetGeometry().GetHeight() * 3 / 4);
}

wxCoord OnMeasureItem(size_t item) const { return FromDIP(22); }
wxCoord OnMeasureItemWidth(size_t item) const { return FromDIP(40); }
wxCoord OnMeasureItem(size_t item) const { return 22 * csf; }
wxCoord OnMeasureItemWidth(size_t item) const { return 40 * csf; }
void OnDrawBackground(wxDC &dc, const wxRect &rect, int item, int flags) const {
DrawRectangle(dc, 0xFFFFFF, rect.x, rect.y, rect.width, rect.height);
}
Expand Down Expand Up @@ -109,37 +111,40 @@ struct ImageDropdown : wxOwnerDrawnComboBox {
// FIXME: delete these somewhere
Vector<wxBitmap *> bitmaps_display;
wxArrayString as;
double csf, csf_orig;
const int image_space = 22;

ImageDropdown(wxWindow *parent, wxString &path) {
csf = sys->frame->csf;
csf_orig = sys->frame->csf;
wxString f = wxFindFirstFile(path + L"*.*");
while (!f.empty()) {
wxBitmap bm;
if (bm.LoadFile(f, wxBITMAP_TYPE_PNG)) {
auto dbm = new wxBitmap();
ScaleBitmap(bm, FromDIP(1.0) / dd_icon_res_scale, *dbm);
ScaleBitmap(bm, csf_orig / dd_icon_res_scale, *dbm);
bitmaps_display.push() = dbm;
as.Add(f);
}
f = wxFindNextFile();
}
Create(parent, A_DDIMAGE, L"", wxDefaultPosition,
FromDIP(wxSize(image_space * 2, image_space)), as,
wxSize(image_space * 2, image_space) * csf, as,
wxCB_READONLY | wxCC_SPECIAL_DCLICK);
SetPopupControl(new ImagePopup());
SetSelection(0);
SetPopupMaxHeight(wxDisplay().GetGeometry().GetHeight() * 3 / 4);
}

wxCoord OnMeasureItem(size_t item) const { return FromDIP(image_space); }
wxCoord OnMeasureItemWidth(size_t item) const { return FromDIP(image_space); }
wxCoord OnMeasureItem(size_t item) const { return image_space * csf; }
wxCoord OnMeasureItemWidth(size_t item) const { return image_space * csf; }
void OnDrawBackground(wxDC &dc, const wxRect &rect, int item, int flags) const {
DrawRectangle(dc, 0xFFFFFF, rect.x, rect.y, rect.width, rect.height);
}

void OnDrawItem(wxDC &dc, const wxRect &rect, int item, int flags) const {
auto bm = bitmaps_display[item];
sys->ImageDraw(bm, dc, rect.x + FromDIP(3), rect.y + FromDIP(3));
sys->ImageDraw(bm, dc, rect.x + 3 * csf, rect.y + 3 * csf);
}
};

Expand Down

0 comments on commit 16b1f39

Please sign in to comment.