Skip to content

Commit

Permalink
Generalize TextSelectionController to support wxTextCtrl
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqr committed Apr 3, 2021
1 parent 347b26f commit 3d7b8c4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
70 changes: 48 additions & 22 deletions src/text_selection_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,44 @@

#include "text_selection_controller.h"

#ifdef WITH_WXSTC
#include <wx/stc/stc.h>

void TextSelectionController::SetControl(wxStyledTextCtrl *ctrl) {
this->ctrl = ctrl;
void TextSelectionController::SetControl(wxStyledTextCtrl* ctrl) {
this->ctrl_te = ctrl;
this->ctrl_ctl = ctrl;
if (ctrl)
ctrl->Bind(wxEVT_STC_UPDATEUI, &TextSelectionController::UpdateUI, this);
use_stc = true;
}
#endif

void TextSelectionController::SetControl(wxTextCtrl* ctrl) {
this->ctrl_te = ctrl;
this->ctrl_ctl = ctrl;
if (ctrl) {
ctrl->Bind(wxEVT_KEY_DOWN, &TextSelectionController::UpdateUI, this);
ctrl->Bind(wxEVT_LEFT_DOWN, &TextSelectionController::UpdateUI, this);
}
#ifdef WITH_WXSTC
use_stc = false;
#endif
}

TextSelectionController::~TextSelectionController() {
if (ctrl) ctrl->Unbind(wxEVT_STC_UPDATEUI, &TextSelectionController::UpdateUI, this);
#ifdef WITH_WXSTC
if (ctrl_ctl) {
if (use_stc) {
ctrl_ctl->Unbind(wxEVT_STC_UPDATEUI, &TextSelectionController::UpdateUI, this);
}
else {
#endif
ctrl_ctl->Unbind(wxEVT_KEY_DOWN, &TextSelectionController::UpdateUI, this);
ctrl_ctl->Unbind(wxEVT_LEFT_DOWN, &TextSelectionController::UpdateUI, this);
#ifdef WITH_WXSTC
}
}
#endif
}

#define GET(var, new_value) do { \
Expand All @@ -36,40 +64,38 @@ TextSelectionController::~TextSelectionController() {
} \
} while(false)

#define SET(var, new_value, Setter) do { \
if (var != new_value) { \
var = new_value; \
if (ctrl) ctrl->Setter(var); \
} \
} while (false)

void TextSelectionController::UpdateUI(wxStyledTextEvent &evt) {
void TextSelectionController::UpdateUI(wxEvent& evt) {
if (changing) return;

bool changed = false;
GET(insertion_point, ctrl->GetInsertionPoint());
if (evt.GetUpdated() & wxSTC_UPDATE_SELECTION) {
GET(selection_start, ctrl->GetSelectionStart());
GET(selection_end, ctrl->GetSelectionEnd());
}
else {
GET(selection_start, insertion_point);
GET(selection_end, insertion_point);
GET(insertion_point, ctrl_te->GetInsertionPoint());
long tmp_start, tmp_end;
ctrl_te->GetSelection(&tmp_start, &tmp_end);
if (tmp_start != selection_start || tmp_end != selection_end) {
selection_start = tmp_start;
selection_end = tmp_end;
changed = true;
}
if (changed) AnnounceSelectionChanged();
}

void TextSelectionController::SetInsertionPoint(int position) {
changing = true;
SET(insertion_point, position, SetInsertionPoint);
if (insertion_point != position) {
insertion_point = position;
if (ctrl_te) ctrl_te->SetInsertionPoint(position);
}
changing = false;
AnnounceSelectionChanged();
}

void TextSelectionController::SetSelection(int start, int end) {
changing = true;
SET(selection_start, start, SetSelectionStart);
SET(selection_end, end, SetSelectionEnd);
if (selection_start != start || selection_end != end) {
selection_start = start;
selection_end = end;
if (ctrl_te) ctrl_te->SetSelection(start, end);
}
changing = false;
AnnounceSelectionChanged();
}
25 changes: 16 additions & 9 deletions src/text_selection_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,33 @@ class wxStyledTextCtrl;
class wxStyledTextEvent;

class TextSelectionController {
int selection_start = 0;
int selection_end = 0;
int insertion_point = 0;
long selection_start = 0;
long selection_end = 0;
long insertion_point = 0;
bool changing = false;

wxStyledTextCtrl *ctrl = nullptr;
#ifdef WITH_WXSTC
bool use_stc;
#endif
wxTextEntryBase* ctrl_te = nullptr;
wxControl* ctrl_ctl = nullptr;

void UpdateUI(wxStyledTextEvent &evt);
void UpdateUI(wxEvent& evt);

agi::signal::Signal<> AnnounceSelectionChanged;

public:
void SetSelection(int start, int end);
void SetInsertionPoint(int point);

int GetSelectionStart() const { return selection_start; }
int GetSelectionEnd() const { return selection_end; }
int GetInsertionPoint() const { return insertion_point; }
long GetSelectionStart() const { return selection_start; }
long GetSelectionEnd() const { return selection_end; }
long GetInsertionPoint() const { return insertion_point; }

void SetControl(wxStyledTextCtrl *ctrl);
#ifdef WITH_WXSTC
void SetControl(wxStyledTextCtrl* ctrl);
#endif
void SetControl(wxTextCtrl* ctrl);
~TextSelectionController();

DEFINE_SIGNAL_ADDERS(AnnounceSelectionChanged, AddSelectionListener)
Expand Down

0 comments on commit 3d7b8c4

Please sign in to comment.