diff options
Diffstat (limited to 'wx+/context_menu.h')
-rwxr-xr-x | wx+/context_menu.h | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/wx+/context_menu.h b/wx+/context_menu.h index bd77100c..41d2bf71 100755 --- a/wx+/context_menu.h +++ b/wx+/context_menu.h @@ -28,60 +28,63 @@ namespace zen class ContextMenu : private wxEvtHandler { public: - ContextMenu() : menu(std::make_unique<wxMenu>()) {} + ContextMenu() {} void addItem(const wxString& label, const std::function<void()>& command, const wxBitmap* bmp = nullptr, bool enabled = true) { - wxMenuItem* newItem = new wxMenuItem(menu.get(), wxID_ANY, label); //menu owns item! + wxMenuItem* newItem = new wxMenuItem(menu_.get(), wxID_ANY, label); //menu owns item! if (bmp) newItem->SetBitmap(*bmp); //do not set AFTER appending item! wxWidgets screws up for yet another crappy reason - menu->Append(newItem); + menu_->Append(newItem); if (!enabled) newItem->Enable(false); //do not enable BEFORE appending item! wxWidgets screws up for yet another crappy reason - commandList[newItem->GetId()] = command; //defer event connection, this may be a submenu only! + commandList_[newItem->GetId()] = command; //defer event connection, this may be a submenu only! } void addCheckBox(const wxString& label, const std::function<void()>& command, bool checked, bool enabled = true) { - wxMenuItem* newItem = menu->AppendCheckItem(wxID_ANY, label); + wxMenuItem* newItem = menu_->AppendCheckItem(wxID_ANY, label); newItem->Check(checked); if (!enabled) newItem->Enable(false); - commandList[newItem->GetId()] = command; + commandList_[newItem->GetId()] = command; } void addRadio(const wxString& label, const std::function<void()>& command, bool selected, bool enabled = true) { - wxMenuItem* newItem = menu->AppendRadioItem(wxID_ANY, label); + wxMenuItem* newItem = menu_->AppendRadioItem(wxID_ANY, label); newItem->Check(selected); if (!enabled) newItem->Enable(false); - commandList[newItem->GetId()] = command; + commandList_[newItem->GetId()] = command; } - void addSeparator() { menu->AppendSeparator(); } + void addSeparator() { menu_->AppendSeparator(); } void addSubmenu(const wxString& label, ContextMenu& submenu, const wxBitmap* bmp = nullptr) //invalidates submenu! { //transfer submenu commands: - commandList.insert(submenu.commandList.begin(), submenu.commandList.end()); - submenu.commandList.clear(); + commandList_.insert(submenu.commandList_.begin(), submenu.commandList_.end()); + submenu.commandList_.clear(); - submenu.menu->SetNextHandler(menu.get()); //on wxGTK submenu events are not propagated to their parent menu by default! + submenu.menu_->SetNextHandler(menu_.get()); //on wxGTK submenu events are not propagated to their parent menu by default! - wxMenuItem* newItem = new wxMenuItem(menu.get(), wxID_ANY, label, L"", wxITEM_NORMAL, submenu.menu.release()); //menu owns item, item owns submenu! + wxMenuItem* newItem = new wxMenuItem(menu_.get(), wxID_ANY, label, L"", wxITEM_NORMAL, submenu.menu_.release()); //menu owns item, item owns submenu! if (bmp) newItem->SetBitmap(*bmp); //do not set AFTER appending item! wxWidgets screws up for yet another crappy reason - menu->Append(newItem); + menu_->Append(newItem); } void popup(wxWindow& wnd, const wxPoint& pos = wxDefaultPosition) //show popup menu + process lambdas { //eventually all events from submenu items will be received by this menu - for (const auto& item : commandList) - menu->Connect(item.first, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(ContextMenu::onSelection), new GenericCommand(item.second) /*pass ownership*/, this); + for (const auto& item : commandList_) + menu_->Connect(item.first, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(ContextMenu::onSelection), new GenericCommand(item.second) /*pass ownership*/, this); - wnd.PopupMenu(menu.get(), pos); + wnd.PopupMenu(menu_.get(), pos); wxTheApp->ProcessPendingEvents(); //make sure lambdas are evaluated before going out of scope; //although all events seem to be processed within wxWindows::PopupMenu, we shouldn't trust wxWidgets in this regard } private: + ContextMenu (const ContextMenu&) = delete; + ContextMenu& operator=(const ContextMenu&) = delete; + void onSelection(wxCommandEvent& event) { if (auto cmd = dynamic_cast<GenericCommand*>(event.m_callbackUserData)) @@ -94,8 +97,8 @@ private: std::function<void()> fun_; }; - std::unique_ptr<wxMenu> menu; - std::map<int, std::function<void()>> commandList; //(item id, command) + std::unique_ptr<wxMenu> menu_ = std::make_unique<wxMenu>(); + std::map<int, std::function<void()>> commandList_; //(item id, command) }; } |