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 // File menu
wxMenu* fileMenu = new wxMenu; 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")), 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")); [this](wxCommandEvent&) { if (m_plater) m_plater->load_project(); }, menu_icon("open"));
@ -352,7 +351,6 @@ void MainFrame::init_menubar()
fileMenu->AppendSeparator(); fileMenu->AppendSeparator();
wxMenu* import_menu = new wxMenu(); 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")), 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")); [this](wxCommandEvent&) { if (m_plater) m_plater->add_model(); }, menu_icon("import_plater"));
import_menu->AppendSeparator(); import_menu->AppendSeparator();
@ -366,7 +364,6 @@ void MainFrame::init_menubar()
append_submenu(fileMenu, import_menu, wxID_ANY, _(L("&Import")), ""); append_submenu(fileMenu, import_menu, wxID_ANY, _(L("&Import")), "");
wxMenu* export_menu = new wxMenu(); 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")), 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")); [this](wxCommandEvent&) { if (m_plater) m_plater->export_gcode(); }, menu_icon("export_gcode"));
m_changeable_menu_items.push_back(item_export_gcode); m_changeable_menu_items.push_back(item_export_gcode);
@ -418,29 +415,6 @@ void MainFrame::init_menubar()
[this](wxCommandEvent&) { Close(false); }); [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) { 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->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) && can_save()); }, item_save_as->GetId());
Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(m_plater != nullptr); }, item_import_model->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) if (m_plater != nullptr)
{ {
editMenu = new wxMenu(); editMenu = new wxMenu();
set_as_owner_drawn(editMenu);
#ifdef __APPLE__ #ifdef __APPLE__
// Backspace sign // Backspace sign
wxString hotkey_delete = "\u232b"; wxString hotkey_delete = "\u232b";
@ -497,7 +470,6 @@ void MainFrame::init_menubar()
// Window menu // Window menu
auto windowMenu = new wxMenu(); auto windowMenu = new wxMenu();
set_as_owner_drawn(windowMenu);
{ {
size_t tab_offset = 0; size_t tab_offset = 0;
if (m_plater) { if (m_plater) {

View File

@ -25,62 +25,28 @@ using Slic3r::GUI::from_u8;
wxDEFINE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent); wxDEFINE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent);
wxDEFINE_EVENT(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED, wxCommandEvent); wxDEFINE_EVENT(wxCUSTOMEVT_LAST_VOLUME_IS_DELETED, wxCommandEvent);
void set_as_owner_drawn(wxMenu* menu)
{
#ifdef __WXMSW__ #ifdef __WXMSW__
// this function is implemented only for MSW (in Prusa/wxWidgets fork) static std::map<int, std::string> msw_menuitem_bitmaps;
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));
}
}
}
void msw_rescale_menu(wxMenu* menu) void msw_rescale_menu(wxMenu* menu)
{ {
wxMenuItemList& items = menu->GetMenuItems(); struct update_icons {
for (auto item : items) static void run(wxMenuItem* item) {
{ const auto it = msw_menuitem_bitmaps.find(item->GetId());
update_menu_item_icons(item); if (it != msw_menuitem_bitmaps.end()) {
item->SetFont(Slic3r::GUI::wxGetApp().normal_font()); const wxBitmap& item_icon = create_scaled_bitmap(nullptr, it->second);
if (item_icon.IsOk())
item->SetBitmap(item_icon);
}
if (item->IsSubMenu()) if (item->IsSubMenu())
{ for (wxMenuItem *sub_item : item->GetSubMenu()->GetMenuItems())
wxMenuItemList& sub_items = item->GetSubMenu()->GetMenuItems(); update_icons::run(sub_item);
for (auto sub_item : sub_items)
{
update_menu_item_icons(sub_item);
sub_item->SetFont(Slic3r::GUI::wxGetApp().normal_font());
}
}
} }
};
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, 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) 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) if (id == wxID_ANY)
id = wxNewId(); id = wxNewId();
wxMenuItem* item = menu->Append(id, string, description); auto *item = new wxMenuItem(menu, id, string, description);
// item->SetBitmap(icon); if (icon.IsOk()) {
if (icon.IsOk())
{
item->SetBitmap(icon); item->SetBitmap(icon);
#ifndef __WXMSW__
wxImage imgGrey = icon.ConvertToImage().ConvertToGreyscale(); wxImage imgGrey = icon.ConvertToImage().ConvertToGreyscale();
if (imgGrey.IsOk()) if (imgGrey.IsOk())
item->SetDisabledBitmap(wxBitmap(imgGrey)); item->SetDisabledBitmap(wxBitmap(imgGrey));
#endif /* __WXMSW__ */
} }
menu->Append(item);
#ifdef __WXMSW__ #ifdef __WXMSW__
if (event_handler != nullptr && event_handler != menu) 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(); id = wxNewId();
const wxBitmap& bmp = !icon.empty() ? create_scaled_bitmap(nullptr, icon) : wxNullBitmap; // FIXME: pass window ptr const wxBitmap& bmp = !icon.empty() ? create_scaled_bitmap(nullptr, icon) : wxNullBitmap; // FIXME: pass window ptr
#ifdef __WXMSW__
if (bmp.IsOk()) 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); 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(); id = wxNewId();
wxMenuItem* item = new wxMenuItem(menu, id, string, description); 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 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); item->SetSubMenu(sub_menu);
menu->Append(item); menu->Append(item);

View File

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