Skip to content

Commit

Permalink
Using of wxWidgets 3.1.6 WIP: Linux/OSX specific fixes
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
YuSanka authored and lukasmatena committed Jul 29, 2022
1 parent 066b567 commit f8477d1
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 150 deletions.
129 changes: 16 additions & 113 deletions src/slic3r/GUI/BitmapCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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))));
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/slic3r/GUI/BitmapCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ class BitmapCache

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.
Expand All @@ -58,9 +54,6 @@ class BitmapCache
// 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); }
Expand Down
23 changes: 15 additions & 8 deletions src/slic3r/GUI/ExtraRenderers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ---------------------------------------------------------
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions src/slic3r/GUI/GUI_ObjectList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/GUI_ObjectList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class ObjectList : public wxDataViewCtrl
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;
Expand Down
6 changes: 3 additions & 3 deletions src/slic3r/GUI/OG_CustomCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions src/slic3r/GUI/PresetComboBoxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Loading

0 comments on commit f8477d1

Please sign in to comment.