forked from mia/Aegisub
Eliminate some duplicated code in the find dialog
This commit is contained in:
parent
2d521fafc5
commit
c923b05c23
2 changed files with 47 additions and 56 deletions
|
@ -140,10 +140,6 @@ DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace)
|
||||||
}
|
}
|
||||||
|
|
||||||
DialogSearchReplace::~DialogSearchReplace() {
|
DialogSearchReplace::~DialogSearchReplace() {
|
||||||
UpdateSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
void DialogSearchReplace::UpdateSettings() {
|
|
||||||
Search.isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
|
Search.isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
|
||||||
Search.matchCase = CheckMatchCase->IsChecked();
|
Search.matchCase = CheckMatchCase->IsChecked();
|
||||||
OPT_SET("Tool/Search Replace/Match Case")->SetBool(CheckMatchCase->IsChecked());
|
OPT_SET("Tool/Search Replace/Match Case")->SetBool(CheckMatchCase->IsChecked());
|
||||||
|
@ -165,42 +161,37 @@ void DialogSearchReplace::FindReplace(int mode) {
|
||||||
Search.affect = Affect->GetSelection();
|
Search.affect = Affect->GetSelection();
|
||||||
Search.field = Field->GetSelection();
|
Search.field = Field->GetSelection();
|
||||||
|
|
||||||
if (mode == 0) {
|
if (hasReplace) {
|
||||||
Search.FindNext();
|
|
||||||
if (hasReplace) {
|
|
||||||
wxString ReplaceWith = ReplaceEdit->GetValue();
|
|
||||||
Search.ReplaceWith = ReplaceWith;
|
|
||||||
config::mru->Add("Replace", from_wx(ReplaceWith));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
wxString ReplaceWith = ReplaceEdit->GetValue();
|
wxString ReplaceWith = ReplaceEdit->GetValue();
|
||||||
Search.ReplaceWith = ReplaceWith;
|
Search.ReplaceWith = ReplaceWith;
|
||||||
if (mode == 1) Search.ReplaceNext();
|
|
||||||
else Search.ReplaceAll();
|
|
||||||
config::mru->Add("Replace", from_wx(ReplaceWith));
|
config::mru->Add("Replace", from_wx(ReplaceWith));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mode == 0)
|
||||||
|
Search.FindNext();
|
||||||
|
else if (mode == 1)
|
||||||
|
Search.ReplaceNext();
|
||||||
|
else
|
||||||
|
Search.ReplaceAll();
|
||||||
|
|
||||||
config::mru->Add("Find", from_wx(LookFor));
|
config::mru->Add("Find", from_wx(LookFor));
|
||||||
UpdateDropDowns();
|
UpdateDropDowns();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DialogSearchReplace::UpdateDropDowns() {
|
static void update_mru(wxComboBox *cb, const char *mru_name) {
|
||||||
FindEdit->Freeze();
|
cb->Freeze();
|
||||||
FindEdit->Clear();
|
cb->Clear();
|
||||||
FindEdit->Append(lagi_MRU_wxAS("Find"));
|
cb->Append(lagi_MRU_wxAS(mru_name));
|
||||||
if (!FindEdit->IsListEmpty())
|
if (!cb->IsListEmpty())
|
||||||
FindEdit->SetSelection(0);
|
cb->SetSelection(0);
|
||||||
FindEdit->Thaw();
|
cb->Thaw();
|
||||||
|
}
|
||||||
|
|
||||||
if (hasReplace) {
|
void DialogSearchReplace::UpdateDropDowns() {
|
||||||
ReplaceEdit->Freeze();
|
update_mru(FindEdit, "Find");
|
||||||
ReplaceEdit->Clear();
|
|
||||||
ReplaceEdit->Append(lagi_MRU_wxAS("Replace"));
|
if (hasReplace)
|
||||||
if (!ReplaceEdit->IsListEmpty())
|
update_mru(ReplaceEdit, "Replace");
|
||||||
ReplaceEdit->SetSelection(0);
|
|
||||||
ReplaceEdit->Thaw();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SearchReplaceEngine::SearchReplaceEngine()
|
SearchReplaceEngine::SearchReplaceEngine()
|
||||||
|
@ -257,8 +248,19 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
size_t tempPos;
|
size_t tempPos;
|
||||||
int regFlags = wxRE_ADVANCED;
|
int regFlags = wxRE_ADVANCED;
|
||||||
if (!matchCase) {
|
if (!matchCase) {
|
||||||
if (isReg) regFlags |= wxRE_ICASE;
|
if (isReg)
|
||||||
else LookFor.MakeLower();
|
regFlags |= wxRE_ICASE;
|
||||||
|
else
|
||||||
|
LookFor.MakeLower();
|
||||||
|
}
|
||||||
|
wxRegEx regex;
|
||||||
|
if (isReg) {
|
||||||
|
regex.Compile(LookFor, regFlags);
|
||||||
|
|
||||||
|
if (!regex.IsValid()) {
|
||||||
|
LastWasFind = !DoReplace;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for it
|
// Search for it
|
||||||
|
@ -271,14 +273,11 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
tempPos = pos+replaceLen;
|
tempPos = pos+replaceLen;
|
||||||
|
|
||||||
if (isReg) {
|
if (isReg) {
|
||||||
wxRegEx regex (LookFor,regFlags);
|
if (regex.Matches(Text->get().Mid(tempPos))) {
|
||||||
if (regex.IsValid()) {
|
size_t match_start;
|
||||||
if (regex.Matches(Text->get().Mid(tempPos))) {
|
regex.GetMatch(&match_start,&matchLen,0);
|
||||||
size_t match_start;
|
pos = match_start + tempPos;
|
||||||
regex.GetMatch(&match_start,&matchLen,0);
|
found = true;
|
||||||
pos = match_start + tempPos;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -294,11 +293,10 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
|
|
||||||
// Didn't find, go to next line
|
// Didn't find, go to next line
|
||||||
if (!found) {
|
if (!found) {
|
||||||
curLine++;
|
curLine = (curLine + 1) % nrows;
|
||||||
pos = 0;
|
pos = 0;
|
||||||
matchLen = 0;
|
matchLen = 0;
|
||||||
replaceLen = 0;
|
replaceLen = 0;
|
||||||
if (curLine == nrows) curLine = 0;
|
|
||||||
if (curLine == start) break;
|
if (curLine == start) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,7 +307,6 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
else {
|
else {
|
||||||
if (isReg) {
|
if (isReg) {
|
||||||
wxString toReplace = Text->get().Mid(pos,matchLen);
|
wxString toReplace = Text->get().Mid(pos,matchLen);
|
||||||
wxRegEx regex(LookFor,regFlags);
|
|
||||||
regex.ReplaceFirst(&toReplace,ReplaceWith);
|
regex.ReplaceFirst(&toReplace,ReplaceWith);
|
||||||
*Text = Text->get().Left(pos) + toReplace + Text->get().Mid(pos+matchLen);
|
*Text = Text->get().Left(pos) + toReplace + Text->get().Mid(pos+matchLen);
|
||||||
replaceLen = toReplace.Length();
|
replaceLen = toReplace.Length();
|
||||||
|
@ -422,20 +419,15 @@ void SearchReplaceEngine::OnDialogOpen() {
|
||||||
void SearchReplaceEngine::OpenDialog(bool replace) {
|
void SearchReplaceEngine::OpenDialog(bool replace) {
|
||||||
static DialogSearchReplace *diag = nullptr;
|
static DialogSearchReplace *diag = nullptr;
|
||||||
|
|
||||||
// already opened
|
if (diag && replace != hasReplace) {
|
||||||
if (diag) {
|
// Already opened, but wrong type - destroy and create the right one
|
||||||
// it's the right type so give focus
|
|
||||||
if(replace == hasReplace) {
|
|
||||||
diag->FindEdit->SetFocus();
|
|
||||||
diag->Show();
|
|
||||||
OnDialogOpen();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// wrong type - destroy and create the right one
|
|
||||||
diag->Destroy();
|
diag->Destroy();
|
||||||
|
diag = nullptr;
|
||||||
}
|
}
|
||||||
// create new one
|
|
||||||
diag = new DialogSearchReplace(context, replace);
|
if (!diag)
|
||||||
|
diag = new DialogSearchReplace(context, replace);
|
||||||
|
|
||||||
diag->FindEdit->SetFocus();
|
diag->FindEdit->SetFocus();
|
||||||
diag->Show();
|
diag->Show();
|
||||||
hasReplace = replace;
|
hasReplace = replace;
|
||||||
|
|
|
@ -94,5 +94,4 @@ class DialogSearchReplace : public wxDialog {
|
||||||
public:
|
public:
|
||||||
DialogSearchReplace(agi::Context* c, bool withReplace);
|
DialogSearchReplace(agi::Context* c, bool withReplace);
|
||||||
~DialogSearchReplace();
|
~DialogSearchReplace();
|
||||||
void UpdateSettings();
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue