From a74fb146131de6eb4994649316f58998dce39a9f Mon Sep 17 00:00:00 2001 From: "chunmao.guo" Date: Fri, 29 Jul 2022 11:18:33 +0800 Subject: [PATCH] FIX: button handle key event Change-Id: If3ecd2356e516105c2054c9b85a122f3ccc72d91 --- src/slic3r/GUI/SavePresetDialog.cpp | 8 ++--- src/slic3r/GUI/SavePresetDialog.hpp | 4 +-- src/slic3r/GUI/UnsavedChangesDialog.cpp | 2 +- src/slic3r/GUI/Widgets/Button.cpp | 39 +++++++++++++++++++++++++ src/slic3r/GUI/Widgets/Button.hpp | 6 ++++ src/slic3r/GUI/Widgets/ComboBox.cpp | 9 ++++-- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/slic3r/GUI/SavePresetDialog.cpp b/src/slic3r/GUI/SavePresetDialog.cpp index fd148cb6b..3f5822c7e 100644 --- a/src/slic3r/GUI/SavePresetDialog.cpp +++ b/src/slic3r/GUI/SavePresetDialog.cpp @@ -340,7 +340,7 @@ void SavePresetDialog::build(std::vector types, std::string suffix m_confirm->SetTextColor(wxColour(255, 255, 255)); m_confirm->SetMinSize(SAVE_PRESET_DIALOG_BUTTON_SIZE); m_confirm->SetCornerRadius(12); - m_confirm->Bind(wxEVT_LEFT_DOWN, &SavePresetDialog::accept, this); + m_confirm->Bind(wxEVT_BUTTON, &SavePresetDialog::accept, this); btns->Add(m_confirm, 0, wxEXPAND, 0); auto block_middle = new wxWindow(this, -1); @@ -351,7 +351,7 @@ void SavePresetDialog::build(std::vector types, std::string suffix m_cancel->SetMinSize(SAVE_PRESET_DIALOG_BUTTON_SIZE); m_cancel->SetTextColor(wxColour(107, 107, 107)); m_cancel->SetCornerRadius(12); - m_cancel->Bind(wxEVT_LEFT_DOWN, &SavePresetDialog::on_select_cancel, this); + m_cancel->Bind(wxEVT_BUTTON, &SavePresetDialog::on_select_cancel, this); btns->Add(m_cancel, 0, wxEXPAND, 0); auto block_right = new wxWindow(this, -1); @@ -372,7 +372,7 @@ void SavePresetDialog::build(std::vector types, std::string suffix this->Centre(wxBOTH); } -void SavePresetDialog::on_select_cancel(wxMouseEvent &event) +void SavePresetDialog::on_select_cancel(wxCommandEvent &event) { EndModal(wxID_CANCEL); } @@ -511,7 +511,7 @@ void SavePresetDialog::update_physical_printers(const std::string &preset_name) } } -void SavePresetDialog::accept(wxMouseEvent &event) +void SavePresetDialog::accept(wxCommandEvent &event) { for (Item *item : m_items) { item->accept(); diff --git a/src/slic3r/GUI/SavePresetDialog.hpp b/src/slic3r/GUI/SavePresetDialog.hpp index 2449c08b7..1b2089f89 100644 --- a/src/slic3r/GUI/SavePresetDialog.hpp +++ b/src/slic3r/GUI/SavePresetDialog.hpp @@ -111,9 +111,9 @@ protected: private: void build(std::vector types, std::string suffix = ""); - void on_select_cancel(wxMouseEvent &event); + void on_select_cancel(wxCommandEvent &event); void update_physical_printers(const std::string &preset_name); - void accept(wxMouseEvent &event); + void accept(wxCommandEvent &event); }; } // namespace GUI diff --git a/src/slic3r/GUI/UnsavedChangesDialog.cpp b/src/slic3r/GUI/UnsavedChangesDialog.cpp index c62377b39..a54810e7c 100644 --- a/src/slic3r/GUI/UnsavedChangesDialog.cpp +++ b/src/slic3r/GUI/UnsavedChangesDialog.cpp @@ -955,7 +955,7 @@ void UnsavedChangesDialog::build(Preset::Type type, PresetCollection *dependent_ (*btn)->SetMinSize(UNSAVE_CHANGE_DIALOG_BUTTON_SIZE); (*btn)->SetCornerRadius(12); - (*btn)->Bind(wxEVT_LEFT_DOWN, [this, close_act, dependent_presets](wxEvent &) { + (*btn)->Bind(wxEVT_BUTTON, [this, close_act, dependent_presets](wxEvent &) { bool save_names_and_types = close_act == Action::Save || (close_act == Action::Transfer && ActionButtons::KEEP & m_buttons); if (save_names_and_types && !save(dependent_presets, close_act == Action::Save)) return; close(close_act); diff --git a/src/slic3r/GUI/Widgets/Button.cpp b/src/slic3r/GUI/Widgets/Button.cpp index b7abb2412..54b10c4b1 100644 --- a/src/slic3r/GUI/Widgets/Button.cpp +++ b/src/slic3r/GUI/Widgets/Button.cpp @@ -7,6 +7,8 @@ BEGIN_EVENT_TABLE(Button, StaticBox) EVT_LEFT_DOWN(Button::mouseDown) EVT_LEFT_UP(Button::mouseReleased) +EVT_KEY_DOWN(Button::keyDownUp) +EVT_KEY_UP(Button::keyDownUp) // catch paint events EVT_PAINT(Button::paintEvent) @@ -241,9 +243,46 @@ void Button::mouseReleased(wxMouseEvent& event) } } +void Button::keyDownUp(wxKeyEvent &event) +{ + if (event.GetKeyCode() == WXK_SPACE || event.GetKeyCode() == WXK_RETURN) { + wxMouseEvent evt(event.GetEventType() == wxEVT_KEY_UP ? wxEVT_LEFT_UP : wxEVT_LEFT_DOWN); + event.SetEventObject(this); + GetEventHandler()->ProcessEvent(evt); + return; + } + event.Skip(); +} + void Button::sendButtonEvent() { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); event.SetEventObject(this); GetEventHandler()->ProcessEvent(event); } + +#ifdef __WIN32__ + +WXLRESULT Button::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) +{ + if (nMsg == WM_GETDLGCODE) { return DLGC_WANTMESSAGE; } + if (nMsg == WM_KEYDOWN) { + wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_DOWN, wParam, lParam)); + switch (wParam) { + case WXK_RETURN: { + GetEventHandler()->ProcessEvent(event); + return 0; + } + case WXK_TAB: + case WXK_LEFT: + case WXK_RIGHT: + case WXK_UP: + case WXK_DOWN: + if (HandleAsNavigationKey(event)) + return 0; + } + } + return wxWindow::MSWWindowProc(nMsg, wParam, lParam); +} + +#endif diff --git a/src/slic3r/GUI/Widgets/Button.hpp b/src/slic3r/GUI/Widgets/Button.hpp index 169678568..3b3233e5a 100644 --- a/src/slic3r/GUI/Widgets/Button.hpp +++ b/src/slic3r/GUI/Widgets/Button.hpp @@ -47,6 +47,11 @@ public: void Rescale(); +protected: +#ifdef __WIN32__ + WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) override; +#endif + private: void paintEvent(wxPaintEvent& evt); @@ -57,6 +62,7 @@ private: // some useful events void mouseDown(wxMouseEvent& event); void mouseReleased(wxMouseEvent& event); + void keyDownUp(wxKeyEvent &event); void sendButtonEvent(); diff --git a/src/slic3r/GUI/Widgets/ComboBox.cpp b/src/slic3r/GUI/Widgets/ComboBox.cpp index d4d4e28c5..e6fd9c2be 100644 --- a/src/slic3r/GUI/Widgets/ComboBox.cpp +++ b/src/slic3r/GUI/Widgets/ComboBox.cpp @@ -248,9 +248,11 @@ void ComboBox::keyDown(wxKeyEvent& event) { break; case WXK_UP: case WXK_DOWN: - if (event.GetKeyCode() == WXK_UP && GetSelection() > 0) { + case WXK_LEFT: + case WXK_RIGHT: + if ((event.GetKeyCode() == WXK_UP || event.GetKeyCode() == WXK_LEFT) && GetSelection() > 0) { SetSelection(GetSelection() - 1); - } else if (event.GetKeyCode() == WXK_DOWN && GetSelection() + 1 < texts.size()) { + } else if ((event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_RIGHT) && GetSelection() + 1 < texts.size()) { SetSelection(GetSelection() + 1); } else { break; @@ -263,6 +265,9 @@ void ComboBox::keyDown(wxKeyEvent& event) { GetEventHandler()->ProcessEvent(e); } break; + case WXK_TAB: + HandleAsNavigationKey(event); + break; default: event.Skip(); break;