diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 693ce063d2..431c79c80e 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -20,6 +20,7 @@ #include #include #include +#include //#include #include #include @@ -182,19 +183,34 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S else init_menubar_as_editor(); +#ifndef __APPLE__ + std::vector& entries_cache = accelerator_entries_cache(); + assert(entries_cache.size() + 6 < 100); + wxAcceleratorEntry entries[100]; + + int id = 0; + for (const auto* entry : entries_cache) + entries[id++].Set(entry->GetFlags(), entry->GetKeyCode(), entry->GetMenuItem()->GetId()); + #if _WIN32 // This is needed on Windows to fake the CTRL+# of the window menu when using the numpad - wxAcceleratorEntry entries[6]; - entries[0].Set(wxACCEL_CTRL, WXK_NUMPAD1, wxID_HIGHEST + 1); - entries[1].Set(wxACCEL_CTRL, WXK_NUMPAD2, wxID_HIGHEST + 2); - entries[2].Set(wxACCEL_CTRL, WXK_NUMPAD3, wxID_HIGHEST + 3); - entries[3].Set(wxACCEL_CTRL, WXK_NUMPAD4, wxID_HIGHEST + 4); - entries[4].Set(wxACCEL_CTRL, WXK_NUMPAD5, wxID_HIGHEST + 5); - entries[5].Set(wxACCEL_CTRL, WXK_NUMPAD6, wxID_HIGHEST + 6); - wxAcceleratorTable accel(6, entries); - SetAcceleratorTable(accel); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD1, wxID_HIGHEST + 1); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD2, wxID_HIGHEST + 2); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD3, wxID_HIGHEST + 3); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD4, wxID_HIGHEST + 4); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD5, wxID_HIGHEST + 5); + entries[id++].Set(wxACCEL_CTRL, WXK_NUMPAD6, wxID_HIGHEST + 6); #endif // _WIN32 + wxAcceleratorTable accel(id, entries); + SetAcceleratorTable(accel); + + // clear cache with wxAcceleratorEntry, because it's no need anymore + for (auto entry : entries_cache) + delete entry; + entries_cache.clear(); +#endif + // set default tooltip timer in msec // SetAutoPop supposedly accepts long integers but some bug doesn't allow for larger values // (SetAutoPop is not available on GTK.) diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 157418dd75..1fd5d5ce73 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -9,6 +9,7 @@ #include #include +#include #include @@ -51,6 +52,14 @@ void sys_color_changed_menu(wxMenu* menu) } #endif /* no __linux__ */ +#ifndef __APPLE__ +std::vector& accelerator_entries_cache() +{ + static std::vector entries; + return entries; +} +#endif + void enable_menu_item(wxUpdateUIEvent& evt, std::function const cb_condition, wxMenuItem* item, wxWindow* win) { const bool enable = cb_condition(); @@ -78,8 +87,20 @@ wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const event_handler->Bind(wxEVT_MENU, cb, id); else #endif // __WXMSW__ +#ifndef __APPLE__ + if (parent) + parent->Bind(wxEVT_MENU, cb, id); + else +#endif // n__APPLE__ menu->Bind(wxEVT_MENU, cb, id); +#ifndef __APPLE__ + if (wxAcceleratorEntry* entry = wxAcceleratorEntry::Create(string)) { + entry->SetMenuItem(item); + accelerator_entries_cache().push_back(entry); + } +#endif + if (parent) { parent->Bind(wxEVT_UPDATE_UI, [cb_condition, item, parent](wxUpdateUIEvent& evt) { enable_menu_item(evt, cb_condition, item, parent); }, id); diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 803c0cc656..e4b62bfda2 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -26,6 +26,11 @@ void sys_color_changed_menu(wxMenu* menu); inline void sys_color_changed_menu(wxMenu* /* menu */) {} #endif // no __linux__ +#ifndef __APPLE__ +// Caching wxAcceleratorEntries to use them during mainframe creation +std::vector& accelerator_entries_cache(); +#endif // no __APPLE__ + wxMenuItem* append_menu_item(wxMenu* menu, int id, const wxString& string, const wxString& description, std::function cb, wxBitmapBundle* icon, wxEvtHandler* event_handler = nullptr, std::function const cb_condition = []() { return true;}, wxWindow* parent = nullptr, int insert_pos = wxNOT_FOUND);