Using of wxWidgets 3.1.6 WIP: Linux/OSX specific fixes

OSX specific: Fixed get_mouse_position_in_control().
+ Use GetItemRect() to calculation of the position and size for Extruder selector

Linux specific:
* Use just 1.0 scale for wxBitmapComboboxes under GTK3 and gtk3
* GTK2 specific: use GTK2 doesn't suppost get_scale, so scale bitmap size form em_unit()
This commit is contained in:
YuSanka 2022-06-03 12:49:21 +02:00
parent dd6f7a71f1
commit ae08819a24
9 changed files with 85 additions and 150 deletions

View File

@ -65,6 +65,8 @@ wxBitmapBundle* BitmapCache::insert_bndl(const std::string& name, const std::vec
wxVector<wxBitmap> bitmaps;
std::set<double> scales = {1.0};
#ifndef __linux__
#ifdef __APPLE__
scales.emplace(m_scale);
#else
@ -73,12 +75,14 @@ wxBitmapBundle* BitmapCache::insert_bndl(const std::string& name, const std::vec
scales.emplace(wxDisplay(disp).GetScaleFactor());
#endif
#endif // !__linux__
for (double scale : scales) {
size_t width = 0;
size_t height = 0;
for (const wxBitmapBundle* bmp_bndl : bmps) {
#ifdef __APPLE__
wxSize size = bmp_bndl->GetPreferredBitmapSizeAtScale(1.0);
wxSize size = bmp_bndl->GetDefaultSize();
#else
wxSize size = bmp_bndl->GetPreferredBitmapSizeAtScale(scale);
#endif
@ -98,7 +102,7 @@ wxBitmapBundle* BitmapCache::insert_bndl(const std::string& name, const std::vec
memset(image.GetAlpha(), 0, width * height);
size_t x = 0;
for (const wxBitmapBundle* bmp_bndl : bmps) {
wxBitmap bmp = bmp_bndl->GetBitmap(bmp_bndl->GetPreferredBitmapSizeAtScale(scale));
wxBitmap bmp = bmp_bndl->GetBitmap(bmp_bndl->GetDefaultSize());
if (bmp.GetWidth() > 0) {
if (bmp.GetDepth() == 32) {
wxAlphaPixelData data(bmp);
@ -138,7 +142,7 @@ wxBitmapBundle* BitmapCache::insert_bndl(const std::string& name, const std::vec
}
}
}
x += bmp.GetWidth();
x += bmp.GetScaledWidth();
}
bitmaps.push_back(* this->insert(bitmap_key, wxImage_to_wxBitmap_with_alpha(std::move(image))));
@ -156,8 +160,8 @@ wxBitmapBundle* BitmapCache::insert_bndl(const std::string& name, const std::vec
if (bmp.GetWidth() > 0)
memDC.DrawBitmap(bmp, x, 0, true);
#ifdef __APPLE__
// we should "move" with step equal to non-scaled width
#ifdef __APPLE__
x += bmp.GetScaledWidth();
#else
x += bmp.GetWidth();
@ -262,110 +266,6 @@ wxBitmap* BitmapCache::insert(const std::string &bitmap_key, const wxBitmap &bmp
return bitmap;
}
wxBitmap* BitmapCache::insert(const std::string &bitmap_key, const wxBitmap &bmp, const wxBitmap &bmp2)
{
// Copying the wxBitmaps is cheap as the bitmap's content is reference counted.
const wxBitmap bmps[2] = { bmp, bmp2 };
return this->insert(bitmap_key, bmps, bmps + 2);
}
wxBitmap* BitmapCache::insert(const std::string &bitmap_key, const wxBitmap &bmp, const wxBitmap &bmp2, const wxBitmap &bmp3)
{
// Copying the wxBitmaps is cheap as the bitmap's content is reference counted.
const wxBitmap bmps[3] = { bmp, bmp2, bmp3 };
return this->insert(bitmap_key, bmps, bmps + 3);
}
wxBitmap* BitmapCache::insert(const std::string &bitmap_key, const wxBitmap *begin, const wxBitmap *end)
{
size_t width = 0;
size_t height = 0;
for (const wxBitmap *bmp = begin; bmp != end; ++ bmp) {
#ifdef __APPLE__
width += bmp->GetScaledWidth();
height = std::max<size_t>(height, bmp->GetScaledHeight());
#else
width += bmp->GetWidth();
height = std::max<size_t>(height, bmp->GetHeight());
#endif
}
#ifdef __WXGTK2__
// Broken alpha workaround
wxImage image(width, height);
image.InitAlpha();
// Fill in with a white color.
memset(image.GetData(), 0x0ff, width * height * 3);
// Fill in with full transparency.
memset(image.GetAlpha(), 0, width * height);
size_t x = 0;
for (const wxBitmap *bmp = begin; bmp != end; ++ bmp) {
if (bmp->GetWidth() > 0) {
if (bmp->GetDepth() == 32) {
wxAlphaPixelData data(*const_cast<wxBitmap*>(bmp));
//FIXME The following method is missing from wxWidgets 3.1.1.
// It looks like the wxWidgets 3.0.3 called the wrapped bitmap's UseAlpha().
//data.UseAlpha();
if (data) {
for (int r = 0; r < bmp->GetHeight(); ++ r) {
wxAlphaPixelData::Iterator src(data);
src.Offset(data, 0, r);
unsigned char *dst_pixels = image.GetData() + (x + r * width) * 3;
unsigned char *dst_alpha = image.GetAlpha() + x + r * width;
for (int c = 0; c < bmp->GetWidth(); ++ c, ++ src) {
*dst_pixels ++ = src.Red();
*dst_pixels ++ = src.Green();
*dst_pixels ++ = src.Blue();
*dst_alpha ++ = src.Alpha();
}
}
}
} else if (bmp->GetDepth() == 24) {
wxNativePixelData data(*const_cast<wxBitmap*>(bmp));
if (data) {
for (int r = 0; r < bmp->GetHeight(); ++ r) {
wxNativePixelData::Iterator src(data);
src.Offset(data, 0, r);
unsigned char *dst_pixels = image.GetData() + (x + r * width) * 3;
unsigned char *dst_alpha = image.GetAlpha() + x + r * width;
for (int c = 0; c < bmp->GetWidth(); ++ c, ++ src) {
*dst_pixels ++ = src.Red();
*dst_pixels ++ = src.Green();
*dst_pixels ++ = src.Blue();
*dst_alpha ++ = wxALPHA_OPAQUE;
}
}
}
}
}
x += bmp->GetWidth();
}
return this->insert(bitmap_key, wxImage_to_wxBitmap_with_alpha(std::move(image)));
#else
wxBitmap *bitmap = this->insert(bitmap_key, width, height);
wxMemoryDC memDC;
memDC.SelectObject(*bitmap);
memDC.SetBackground(*wxTRANSPARENT_BRUSH);
memDC.Clear();
size_t x = 0;
for (const wxBitmap *bmp = begin; bmp != end; ++ bmp) {
if (bmp->GetWidth() > 0)
memDC.DrawBitmap(*bmp, x, 0, true);
#ifdef __APPLE__
// we should "move" with step equal to non-scaled width
x += bmp->GetScaledWidth();
#else
x += bmp->GetWidth();
#endif
}
memDC.SelectObject(wxNullBitmap);
return bitmap;
#endif
}
wxBitmap* BitmapCache::insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, const bool grayscale/* = false*/)
{
wxImage image(width, height);
@ -492,7 +392,6 @@ wxBitmapBundle* BitmapCache::from_svg(const std::string& bitmap_name, unsigned t
std::string bitmap_key = bitmap_name + (target_height != 0 ?
"-h" + std::to_string(target_height) :
"-w" + std::to_string(target_width))
// + (m_scale != 1.0f ? "-s" + float_to_string_decimal_point(m_scale) : "")
+ (dark_mode ? "-dm" : "")
+ new_color;
@ -594,9 +493,9 @@ wxBitmap* BitmapCache::load_svg(const std::string &bitmap_name, unsigned target_
return this->insert_raw_rgba(bitmap_key, width, height, data.data(), grayscale);
}
/*
//we make scaled solid bitmaps only for the cases, when its will be used with scaled SVG icon in one output bitmap
wxBitmap BitmapCache::mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, bool suppress_scaling/* = false*/, size_t border_width /*= 0*/, bool dark_mode/* = false*/)
wxBitmap BitmapCache::mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, bool suppress_scaling/* = false* /, size_t border_width /*= 0* /, bool dark_mode/* = false* /)
{
double scale = suppress_scaling ? 1.0f : m_scale;
width *= scale;
@ -638,13 +537,15 @@ wxBitmap BitmapCache::mksolid(size_t width, size_t height, unsigned char r, unsi
return wxImage_to_wxBitmap_with_alpha(std::move(image), scale);
}
*/
//we make scaled solid bitmaps only for the cases, when its will be used with scaled SVG icon in one output bitmap
wxBitmapBundle BitmapCache::mksolid(size_t width_in, size_t height_in, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, size_t border_width /*= 0*/, bool dark_mode/* = false*/)
{
wxVector<wxBitmap> bitmaps;
std::set<double> scales = { 1.0 };
#ifndef __linux__
#ifdef __APPLE__
scales.emplace(m_scale);
#else
@ -653,6 +554,8 @@ wxBitmapBundle BitmapCache::mksolid(size_t width_in, size_t height_in, unsigned
scales.emplace(wxDisplay(disp).GetScaleFactor());
#endif
#endif // !__linux__
for (double scale : scales) {
size_t width = width_in * scale;
size_t height = height_in * scale;
@ -698,7 +601,7 @@ wxBitmapBundle BitmapCache::mksolid(size_t width_in, size_t height_in, unsigned
wxBitmapBundle* BitmapCache::mksolid_bndl(size_t width, size_t height, const std::string& color, size_t border_width, bool dark_mode)
{
std::string bitmap_key = (color.empty() ? "empty-w" : color) + "-h" + std::to_string(height) + "-w" + std::to_string(width) + (dark_mode ? "-dm" : "");
std::string bitmap_key = (color.empty() ? "empty" : color) + "-h" + std::to_string(height) + "-w" + std::to_string(width) + (dark_mode ? "-dm" : "");
wxBitmapBundle* bndl = nullptr;
auto it = m_bndl_map.find(bitmap_key);

View File

@ -37,10 +37,6 @@ public:
wxBitmap* insert(const std::string &name, size_t width, size_t height, double scale = -1.0);
wxBitmap* insert(const std::string &name, const wxBitmap &bmp);
wxBitmap* insert(const std::string &name, const wxBitmap &bmp, const wxBitmap &bmp2);
wxBitmap* insert(const std::string &name, const wxBitmap &bmp, const wxBitmap &bmp2, const wxBitmap &bmp3);
wxBitmap* insert(const std::string &name, const std::vector<wxBitmap> &bmps) { return this->insert(name, &bmps.front(), &bmps.front() + bmps.size()); }
wxBitmap* insert(const std::string &name, const wxBitmap *begin, const wxBitmap *end);
wxBitmap* insert_raw_rgba(const std::string &bitmap_key, unsigned width, unsigned height, const unsigned char *raw_data, const bool grayscale = false);
// Load png from resources/icons. bitmap_key is given without the .png suffix. Bitmap will be rescaled to provided height/width if nonzero.
@ -58,9 +54,6 @@ public:
// Load svg from resources/icons. bitmap_key is given without the .svg suffix. SVG will be rasterized to provided height/width.
wxBitmap* load_svg(const std::string &bitmap_key, unsigned width = 0, unsigned height = 0, const bool grayscale = false, const bool dark_mode = false, const std::string& new_color = "");
wxBitmap mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, bool suppress_scaling = false, size_t border_width = 0, bool dark_mode = false);
wxBitmap mksolid(size_t width, size_t height, const ColorRGB& rgb, bool suppress_scaling = false, size_t border_width = 0, bool dark_mode = false) { return mksolid(width, height, rgb.r_uchar(), rgb.g_uchar(), rgb.b_uchar(), wxALPHA_OPAQUE, suppress_scaling, border_width, dark_mode); }
wxBitmap mkclear(size_t width, size_t height) { return mksolid(width, height, 0, 0, 0, wxALPHA_TRANSPARENT, true, 0); }
wxBitmapBundle mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, size_t border_width = 0, bool dark_mode = false);
wxBitmapBundle* mksolid_bndl(size_t width, size_t height, const std::string& color = std::string(), size_t border_width = 0, bool dark_mode = false);
wxBitmapBundle* mkclear_bndl(size_t width, size_t height) { return mksolid_bndl(width, height); }

View File

@ -33,6 +33,15 @@ wxIMPLEMENT_DYNAMIC_CLASS(DataViewBitmapText, wxObject)
IMPLEMENT_VARIANT_OBJECT(DataViewBitmapText)
static wxSize get_size(const wxBitmap& icon)
{
#ifdef __WIN32__
return icon.GetSize();
#else
return icon.GetScaledSize();
#endif
}
// ---------------------------------------------------------
// BitmapTextRenderer
// ---------------------------------------------------------
@ -124,11 +133,7 @@ bool BitmapTextRenderer::Render(wxRect rect, wxDC *dc, int state)
const wxBitmap& icon = m_value.GetBitmap();
if (icon.IsOk())
{
#ifdef __APPLE__
wxSize icon_sz = icon.GetScaledSize();
#else
wxSize icon_sz = icon.GetSize();
#endif
wxSize icon_sz = get_size(icon);
dc->DrawBitmap(icon, rect.x, rect.y + (rect.height - icon_sz.y) / 2);
xoffset = icon_sz.x + 4;
}
@ -264,11 +269,13 @@ bool BitmapChoiceRenderer::Render(wxRect rect, wxDC* dc, int state)
const wxBitmap& icon = m_value.GetBitmap();
if (icon.IsOk())
{
dc->DrawBitmap(icon, rect.x, rect.y + (rect.height - icon.GetHeight()) / 2);
xoffset = icon.GetWidth() + 4;
wxSize icon_sz = get_size(icon);
dc->DrawBitmap(icon, rect.x, rect.y + (rect.height - icon_sz.GetHeight()) / 2);
xoffset = icon_sz.GetWidth() + 4;
if (rect.height==0)
rect.height= icon.GetHeight();
rect.height= icon_sz.GetHeight();
}
#ifdef _WIN32

View File

@ -971,12 +971,11 @@ void ObjectList::extruder_editing()
if (!item || !(m_objects_model->GetItemType(item) & (itVolume | itObject)))
return;
const int column_width = GetColumn(colExtruder)->GetWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 5;
wxPoint pos = this->get_mouse_position_in_control();
wxSize size = wxSize(column_width, -1);
pos.x = GetColumn(colName)->GetWidth() + GetColumn(colPrint)->GetWidth() + 5;
pos.y -= GetTextExtent("m").y;
wxRect rect = this->GetItemRect(item, GetColumn(colExtruder));
wxPoint pos = rect.GetPosition();
pos.y -= 4;
wxSize size = rect.GetSize();
size.SetWidth(size.GetWidth() + 8);
apply_extruder_selector(&m_extruder_editor, this, L("default"), pos, size);
@ -2433,6 +2432,21 @@ bool ObjectList::can_merge_to_single_object() const
return (*m_objects)[obj_idx]->volumes.size() > 1;
}
wxPoint ObjectList::get_mouse_position_in_control() const
{
wxPoint pt = wxGetMousePosition() - this->GetScreenPosition();
#ifdef __APPLE__
// Workaround for OSX. From wxWidgets 3.1.6 Hittest doesn't respect to the header of wxDataViewCtrl
if (wxDataViewItem top_item = this->GetTopItem(); top_item.IsOk()) {
auto rect = this->GetItemRect(top_item, this->GetColumn(0));
pt.y -= rect.y;
}
#endif // __APPLE__
return pt;
}
// NO_PARAMETERS function call means that changed object index will be determine from Selection()
void ObjectList::changed_object(const int obj_idx/* = -1*/) const
{

View File

@ -280,7 +280,7 @@ public:
bool can_merge_to_multipart_object() const;
bool can_merge_to_single_object() const;
wxPoint get_mouse_position_in_control() const { return wxGetMousePosition() - this->GetScreenPosition(); }
wxPoint get_mouse_position_in_control() const;
wxBoxSizer* get_sizer() {return m_sizer;}
int get_selected_obj_idx() const;
ModelConfig& get_item_config(const wxDataViewItem& item) const;

View File

@ -21,10 +21,10 @@ static bool is_point_in_rect(const wxPoint& pt, const wxRect& rect)
static wxSize get_bitmap_size(const wxBitmapBundle* bmp, wxWindow* parent)
{
#ifdef __APPLE__
return bmp->GetDefaultSize();
#else
#ifdef __WIN32__
return bmp->GetBitmapFor(parent).GetSize();
#else
return bmp->GetDefaultSize();
#endif
}

View File

@ -389,14 +389,14 @@ void PresetComboBox::sys_color_changed()
void PresetComboBox::fill_width_height()
{
icon_height = m_bitmapCompatible->GetPreferredBitmapSizeAtScale(1.0).GetHeight();
norm_icon_width = m_bitmapCompatible->GetPreferredBitmapSizeAtScale(1.0).GetWidth();
null_icon_width = 2 * norm_icon_width;
icon_height = 16;
norm_icon_width = 16;
thin_icon_width = 8;
wide_icon_width = norm_icon_width + thin_icon_width;
null_icon_width = 2 * norm_icon_width;
space_icon_width = 2;
thin_space_icon_width = 4;
wide_space_icon_width = 6;
@ -484,6 +484,7 @@ wxBitmapBundle* PresetComboBox::get_bmp( std::string bitmap_key, const std::str
wxBitmapBundle PresetComboBox::NullBitmapBndl()
{
assert(null_icon_width > 0);
return *get_empty_bmp_bundle(null_icon_width, icon_height);
}

View File

@ -108,10 +108,10 @@ wxMenuItem* append_submenu(wxMenu* menu, wxMenu* sub_menu, int id, const wxStrin
wxMenuItem* item = new wxMenuItem(menu, id, string, description);
if (!icon.empty()) {
item->SetBitmap(*get_bmp_bundle(icon));
//#ifdef __WXMSW__
#ifndef __WXGTK__
#ifndef __linux__
msw_menuitem_bitmaps[id] = icon;
#endif /* __WXMSW__ */
#endif // no __linux__
}
item->SetSubMenu(sub_menu);
@ -409,8 +409,19 @@ int mode_icon_px_size()
#endif
}
#ifdef __WXGTK2__
static int scale()
{
return int(em_unit(nullptr) * 0.1f + 0.5f);
}
#endif // __WXGTK2__
wxBitmapBundle* get_bmp_bundle(const std::string& bmp_name_in, int px_cnt/* = 16*/)
{
#ifdef __WXGTK2__
px_cnt *= scale();
#endif // __WXGTK2__
static Slic3r::GUI::BitmapCache cache;
std::string bmp_name = bmp_name_in;
@ -430,13 +441,21 @@ wxBitmapBundle* get_bmp_bundle(const std::string& bmp_name_in, int px_cnt/* = 16
wxBitmapBundle* get_empty_bmp_bundle(int width, int height)
{
static Slic3r::GUI::BitmapCache cache;
#ifdef __WXGTK2__
return cache.mkclear_bndl(width * scale(), height * scale());
#else
return cache.mkclear_bndl(width, height);
#endif // __WXGTK2__
}
wxBitmapBundle* get_solid_bmp_bundle(int width, int height, const std::string& color )
{
static Slic3r::GUI::BitmapCache cache;
#ifdef __WXGTK2__
return cache.mksolid_bndl(width * scale(), height * scale(), color, 1, Slic3r::GUI::wxGetApp().dark_mode());
#else
return cache.mksolid_bndl(width, height, color, 1, Slic3r::GUI::wxGetApp().dark_mode());
#endif // __WXGTK2__
}
// win is used to get a correct em_unit value
@ -486,8 +505,6 @@ std::vector<wxBitmapBundle*> get_extruder_color_icons(bool thin_icon/* = false*/
if (colors.empty())
return bmps;
bool dark_mode = Slic3r::GUI::wxGetApp().dark_mode();
for (const std::string& color : colors)
bmps.emplace_back(get_solid_bmp_bundle(thin_icon ? 16 : 32, 16, color));

View File

@ -157,10 +157,10 @@ public:
int px_cnt() const { return m_px_cnt;}
wxSize GetSize() const {
#ifdef __APPLE__
return m_bmp.GetDefaultSize();
#else
#ifdef __WIN32__
return m_bmp.GetPreferredBitmapSizeFor(m_parent);
#else
return m_bmp.GetDefaultSize();
#endif
}
int GetWidth() const { return GetSize().GetWidth(); }