EditGCodeDialog: Fixed Linux specific crash on Destroy()

+ Improved cursor position in custom G-code after a paste of placeholder
This commit is contained in:
YuSanka 2023-09-19 12:38:15 +02:00
parent 0d734e7bb5
commit 361ef22b6d
2 changed files with 92 additions and 66 deletions

View File

@ -45,7 +45,7 @@ EditGCodeDialog::EditGCodeDialog(wxWindow* parent, const std::string& key, const
auto* grid_sizer = new wxFlexGridSizer(1, 3, 5, 15);
grid_sizer->SetFlexibleDirection(wxBOTH);
m_params_list = new ParamsViewCtrl(this, wxSize(em * 30, em * 70));
m_params_list = new ParamsViewCtrl(this, wxSize(em * 45, em * 70));
m_params_list->SetFont(wxGetApp().code_font());
wxGetApp().UpdateDarkUI(m_params_list);
@ -58,6 +58,7 @@ EditGCodeDialog::EditGCodeDialog(wxWindow* parent, const std::string& key, const
#endif
);
m_gcode_editor->SetFont(wxGetApp().code_font());
m_gcode_editor->SetInsertionPointEnd();
wxGetApp().UpdateDarkUI(m_gcode_editor);
grid_sizer->Add(m_params_list, 1, wxEXPAND);
@ -97,6 +98,13 @@ EditGCodeDialog::EditGCodeDialog(wxWindow* parent, const std::string& key, const
bind_list_and_button();
}
EditGCodeDialog::~EditGCodeDialog()
{
// To avoid redundant process of wxEVT_DATAVIEW_SELECTION_CHANGED after dialog distroing (on Linux)
// unbind this event from params_list
m_params_list->Unbind(wxEVT_DATAVIEW_SELECTION_CHANGED, &EditGCodeDialog::selection_changed, this);
}
std::string EditGCodeDialog::get_edited_gcode() const
{
return into_u8(m_gcode_editor->GetValue());
@ -229,13 +237,25 @@ wxDataViewItem EditGCodeDialog::add_presets_placeholders()
void EditGCodeDialog::add_selected_value_to_gcode()
{
const wxString val = m_params_list->GetSelectedValue();
if (!val.IsEmpty())
m_gcode_editor->WriteText(val + "\n");
if (val.IsEmpty())
return;
const long pos = m_gcode_editor->GetInsertionPoint();
m_gcode_editor->WriteText(m_gcode_editor->GetInsertionPoint() == m_gcode_editor->GetLastPosition() ? "\n" + val : val);
if (val.Last() == ']') {
const long new_pos = m_gcode_editor->GetInsertionPoint();
if (val[val.Len() - 2] == '[')
m_gcode_editor->SetInsertionPoint(new_pos - 1); // set cursor into brackets
else
m_gcode_editor->SetSelection(new_pos - 17, new_pos - 1); // select "current_extruder"
}
void EditGCodeDialog::bind_list_and_button()
m_gcode_editor->SetFocus();
}
void EditGCodeDialog::selection_changed(wxDataViewEvent& evt)
{
m_params_list->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [this](wxDataViewEvent& evt) {
wxString label;
wxString description;
@ -296,7 +316,11 @@ void EditGCodeDialog::bind_list_and_button()
m_param_description->SetLabel(description);
Layout();
});
}
void EditGCodeDialog::bind_list_and_button()
{
m_params_list->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &EditGCodeDialog::selection_changed, this);
m_params_list->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [this](wxDataViewEvent& ) {
add_selected_value_to_gcode();

View File

@ -44,7 +44,7 @@ class EditGCodeDialog : public DPIDialog
public:
EditGCodeDialog(wxWindow*parent, const std::string&key, const std::string&value);
~EditGCodeDialog() {}
~EditGCodeDialog();
std::string get_edited_gcode() const;
@ -57,6 +57,8 @@ public:
protected:
void on_dpi_changed(const wxRect& suggested_rect) override;
void on_sys_color_changed() override;
void selection_changed(wxDataViewEvent& evt);
};