Finished scaling of menu icons on Windows.

This commit is contained in:
bubnikv 2019-05-07 13:35:37 +02:00
parent 347a65bc4a
commit ab4f349456
3 changed files with 40 additions and 97 deletions

View File

@ -340,7 +340,6 @@ void MainFrame::init_menubar()
// File menu
wxMenu* fileMenu = new wxMenu;
set_as_owner_drawn(fileMenu);
{
wxMenuItem* item_open = append_menu_item(fileMenu, wxID_ANY, _(L("&Open Project")) + dots + "\tCtrl+O", _(L("Open a project file")),
[this](wxCommandEvent&) { if (m_plater) m_plater->load_project(); }, menu_icon("open"));
@ -352,7 +351,6 @@ void MainFrame::init_menubar()
fileMenu->AppendSeparator();
wxMenu* import_menu = new wxMenu();
set_as_owner_drawn(import_menu);
wxMenuItem* item_import_model = append_menu_item(import_menu, wxID_ANY, _(L("Import STL/OBJ/AM&F/3MF")) + dots + "\tCtrl+I", _(L("Load a model")),
[this](wxCommandEvent&) { if (m_plater) m_plater->add_model(); }, menu_icon("import_plater"));
import_menu->AppendSeparator();
@ -366,7 +364,6 @@ void MainFrame::init_menubar()
append_submenu(fileMenu, import_menu, wxID_ANY, _(L("&Import")), "");
wxMenu* export_menu = new wxMenu();
set_as_owner_drawn(export_menu);
wxMenuItem* item_export_gcode = append_menu_item(export_menu, wxID_ANY, _(L("Export &G-code")) + dots +"\tCtrl+G", _(L("Export current plate as G-code")),
[this](wxCommandEvent&) { if (m_plater) m_plater->export_gcode(); }, menu_icon("export_gcode"));
m_changeable_menu_items.push_back(item_export_gcode);
@ -418,29 +415,6 @@ void MainFrame::init_menubar()
[this](wxCommandEvent&) { Close(false); });
Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(m_plater != nullptr); }, item_open->GetId());
// Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt)
// {
// wxMenuItem* item = dynamic_cast<wxMenu*>(evt.GetEventObject())->FindItem(evt.GetId());
//
// // static const wxBitmap enabled_bmp = item->GetBitmap();
// // static const wxBitmap disabled_bmp = enabled_bmp.IsOk() ? enabled_bmp.ConvertToImage().ConvertToGreyscale(0.5, 0.5, 0.5) : enabled_bmp;
//
// // bool enable = (m_plater != nullptr) && can_save();
// // evt.Enable(enable);
// // item->SetBitmap(enable ? enabled_bmp : disabled_bmp);
//
// bool enable = (m_plater != nullptr) && can_save();
// evt.Enable(enable);
//
// const std::string& icon_name = get_menuitem_icon_name(item->GetId());
// if (!icon_name.empty())
// {
// const wxBitmap enabled_bmp = create_scaled_bitmap(nullptr, icon_name);
// const wxBitmap disabled_bmp = enabled_bmp.IsOk() ? enabled_bmp.ConvertToImage().ConvertToGreyscale(0.5, 0.5, 0.5) : enabled_bmp;
//
// item->SetBitmap(enable ? enabled_bmp : disabled_bmp);
// }
// }, item_save->GetId());
Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable((m_plater != nullptr) && can_save()); }, item_save->GetId());
Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable((m_plater != nullptr) && can_save()); }, item_save_as->GetId());
Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(m_plater != nullptr); }, item_import_model->GetId());
@ -466,7 +440,6 @@ void MainFrame::init_menubar()
if (m_plater != nullptr)
{
editMenu = new wxMenu();
set_as_owner_drawn(editMenu);
#ifdef __APPLE__
// Backspace sign
wxString hotkey_delete = "\u232b";
@ -497,7 +470,6 @@ void MainFrame::init_menubar()
// Window menu
auto windowMenu = new wxMenu();
set_as_owner_drawn(windowMenu);
{
size_t tab_offset = 0;
if (m_plater) {

View File

@ -25,62 +25,28 @@ using Slic3r::GUI::from_u8;
wxDEFINE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent);
wxDEFINE_EVENT(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED, wxCommandEvent);
void set_as_owner_drawn(wxMenu* menu)
{
#ifdef __WXMSW__
// this function is implemented only for MSW (in Prusa/wxWidgets fork)
menu->SetOwnerDrawn(true);
#endif
}
std::map<int, std::string> menuitem_bitmaps;
static std::string empty_str = "";
const std::string& get_menuitem_icon_name(const int item_id)
{
const auto item = menuitem_bitmaps.find(item_id);
if (item == menuitem_bitmaps.end())
return empty_str;
return menuitem_bitmaps.at(item_id);
}
void update_menu_item_icons(wxMenuItem* item)
{
const std::string& icon_name = get_menuitem_icon_name(item->GetId());
if (!icon_name.empty())
{
const wxBitmap& item_icon = create_scaled_bitmap(nullptr, icon_name);
if (item_icon.IsOk())
{
item->SetBitmap(item_icon);
wxImage imgGrey = item_icon.ConvertToImage().ConvertToGreyscale();
if (imgGrey.IsOk())
item->SetDisabledBitmap(wxBitmap(imgGrey));
}
}
}
static std::map<int, std::string> msw_menuitem_bitmaps;
void msw_rescale_menu(wxMenu* menu)
{
wxMenuItemList& items = menu->GetMenuItems();
for (auto item : items)
{
update_menu_item_icons(item);
item->SetFont(Slic3r::GUI::wxGetApp().normal_font());
struct update_icons {
static void run(wxMenuItem* item) {
const auto it = msw_menuitem_bitmaps.find(item->GetId());
if (it != msw_menuitem_bitmaps.end()) {
const wxBitmap& item_icon = create_scaled_bitmap(nullptr, it->second);
if (item_icon.IsOk())
item->SetBitmap(item_icon);
}
if (item->IsSubMenu())
{
wxMenuItemList& sub_items = item->GetSubMenu()->GetMenuItems();
for (auto sub_item : sub_items)
{
update_menu_item_icons(sub_item);
sub_item->SetFont(Slic3r::GUI::wxGetApp().normal_font());
}
}
for (wxMenuItem *sub_item : item->GetSubMenu()->GetMenuItems())
update_icons::run(sub_item);
}
};
for (wxMenuItem *item : menu->GetMenuItems())
update_icons::run(item);
}
#endif /* __WXMSW__ */
wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const wxString& description,
std::function<void(wxCommandEvent& event)> cb, const wxBitmap& icon, wxEvtHandler* event_handler)
@ -88,16 +54,16 @@ wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const
if (id == wxID_ANY)
id = wxNewId();
wxMenuItem* item = menu->Append(id, string, description);
// item->SetBitmap(icon);
if (icon.IsOk())
{
auto *item = new wxMenuItem(menu, id, string, description);
if (icon.IsOk()) {
item->SetBitmap(icon);
#ifndef __WXMSW__
wxImage imgGrey = icon.ConvertToImage().ConvertToGreyscale();
if (imgGrey.IsOk())
item->SetDisabledBitmap(wxBitmap(imgGrey));
#endif /* __WXMSW__ */
}
menu->Append(item);
#ifdef __WXMSW__
if (event_handler != nullptr && event_handler != menu)
@ -116,8 +82,10 @@ wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const
id = wxNewId();
const wxBitmap& bmp = !icon.empty() ? create_scaled_bitmap(nullptr, icon) : wxNullBitmap; // FIXME: pass window ptr
#ifdef __WXMSW__
if (bmp.IsOk())
menuitem_bitmaps[id] = icon;
msw_menuitem_bitmaps[id] = icon;
#endif /* __WXMSW__ */
return append_menu_item(menu, id, string, description, cb, bmp, event_handler);
}
@ -128,8 +96,16 @@ wxMenuItem* append_submenu(wxMenu* menu, wxMenu* sub_menu, int id, const wxStrin
id = wxNewId();
wxMenuItem* item = new wxMenuItem(menu, id, string, description);
if (!icon.empty())
if (!icon.empty()) {
item->SetBitmap(create_scaled_bitmap(nullptr, icon)); // FIXME: pass window ptr
#ifdef __WXMSW__
msw_menuitem_bitmaps[id] = icon;
#else /* __WXMSW__ */
wxImage imgGrey = icon.ConvertToImage().ConvertToGreyscale();
if (imgGrey.IsOk())
item->SetDisabledBitmap(wxBitmap(imgGrey));
#endif /* __WXMSW__ */
}
item->SetSubMenu(sub_menu);
menu->Append(item);

View File

@ -20,10 +20,11 @@ namespace Slic3r {
enum class ModelVolumeType : int;
};
void set_as_owner_drawn(wxMenu* menu);
const std::string& get_menuitem_icon_name(const int item_id);
void update_menu_item_icons(wxMenuItem* item);
#ifdef __WXMSW__
void msw_rescale_menu(wxMenu* menu);
#else /* __WXMSW__ */
inline void msw_rescale_menu(wxMenu* /* menu */) {}
#endif /* __WXMSW__ */
wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const wxString& description,
std::function<void(wxCommandEvent& event)> cb, const wxBitmap& icon, wxEvtHandler* event_handler = nullptr);
@ -955,16 +956,10 @@ class MenuWithSeparators : public wxMenu
{
public:
MenuWithSeparators(const wxString& title, long style = 0)
: wxMenu(title, style)
{
set_as_owner_drawn(this);
}
: wxMenu(title, style) {}
MenuWithSeparators(long style = 0)
: wxMenu(style)
{
set_as_owner_drawn(this);
}
: wxMenu(style) {}
~MenuWithSeparators() {}