mirror of https://github.com/odrling/Aegisub
Eliminate some duplicated code in the find dialog
This commit is contained in:
parent
2d521fafc5
commit
c923b05c23
|
@ -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) {
|
|
||||||
Search.FindNext();
|
|
||||||
if (hasReplace) {
|
if (hasReplace) {
|
||||||
wxString ReplaceWith = ReplaceEdit->GetValue();
|
wxString ReplaceWith = ReplaceEdit->GetValue();
|
||||||
Search.ReplaceWith = ReplaceWith;
|
Search.ReplaceWith = ReplaceWith;
|
||||||
config::mru->Add("Replace", from_wx(ReplaceWith));
|
config::mru->Add("Replace", from_wx(ReplaceWith));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
if (mode == 0)
|
||||||
wxString ReplaceWith = ReplaceEdit->GetValue();
|
Search.FindNext();
|
||||||
Search.ReplaceWith = ReplaceWith;
|
else if (mode == 1)
|
||||||
if (mode == 1) Search.ReplaceNext();
|
Search.ReplaceNext();
|
||||||
else Search.ReplaceAll();
|
else
|
||||||
config::mru->Add("Replace", from_wx(ReplaceWith));
|
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) {
|
|
||||||
ReplaceEdit->Freeze();
|
|
||||||
ReplaceEdit->Clear();
|
|
||||||
ReplaceEdit->Append(lagi_MRU_wxAS("Replace"));
|
|
||||||
if (!ReplaceEdit->IsListEmpty())
|
|
||||||
ReplaceEdit->SetSelection(0);
|
|
||||||
ReplaceEdit->Thaw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DialogSearchReplace::UpdateDropDowns() {
|
||||||
|
update_mru(FindEdit, "Find");
|
||||||
|
|
||||||
|
if (hasReplace)
|
||||||
|
update_mru(ReplaceEdit, "Replace");
|
||||||
}
|
}
|
||||||
|
|
||||||
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,8 +273,6 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
tempPos = pos+replaceLen;
|
tempPos = pos+replaceLen;
|
||||||
|
|
||||||
if (isReg) {
|
if (isReg) {
|
||||||
wxRegEx regex (LookFor,regFlags);
|
|
||||||
if (regex.IsValid()) {
|
|
||||||
if (regex.Matches(Text->get().Mid(tempPos))) {
|
if (regex.Matches(Text->get().Mid(tempPos))) {
|
||||||
size_t match_start;
|
size_t match_start;
|
||||||
regex.GetMatch(&match_start,&matchLen,0);
|
regex.GetMatch(&match_start,&matchLen,0);
|
||||||
|
@ -280,7 +280,6 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
wxString src = Text->get().Mid(tempPos);
|
wxString src = Text->get().Mid(tempPos);
|
||||||
if (!matchCase) src.MakeLower();
|
if (!matchCase) src.MakeLower();
|
||||||
|
@ -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
|
|
||||||
|
if (!diag)
|
||||||
diag = new DialogSearchReplace(context, replace);
|
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 New Issue