Move the SearchReplaceEngine instance to the context

This commit is contained in:
Thomas Goyne 2013-01-11 08:53:36 -08:00
parent bc2ddfbaee
commit 2f4cae46b4
6 changed files with 30 additions and 29 deletions

View file

@ -462,7 +462,7 @@ struct edit_find_replace : public Command {
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
c->videoController->Stop(); c->videoController->Stop();
Search.OpenDialog(true); c->search->OpenDialog(true);
} }
}; };

View file

@ -105,7 +105,7 @@ struct subtitle_find : public Command {
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
c->videoController->Stop(); c->videoController->Stop();
Search.OpenDialog(false); c->search->OpenDialog(false);
} }
}; };
@ -119,7 +119,7 @@ struct subtitle_find_next : public Command {
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
c->videoController->Stop(); c->videoController->Stop();
Search.FindNext(); c->search->FindNext();
} }
}; };

View file

@ -80,6 +80,7 @@ enum {
DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace) DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace)
: wxDialog(c->parent, -1, withReplace ? _("Replace") : _("Find")) : wxDialog(c->parent, -1, withReplace ? _("Replace") : _("Find"))
, c(c)
, hasReplace(withReplace) , hasReplace(withReplace)
{ {
wxSizer *FindSizer = new wxFlexGridSizer(2, 2, 5, 15); wxSizer *FindSizer = new wxFlexGridSizer(2, 2, 5, 15);
@ -142,7 +143,7 @@ DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace)
SetSizerAndFit(MainSizer); SetSizerAndFit(MainSizer);
CenterOnParent(); CenterOnParent();
Search.OnDialogOpen(); c->search->OnDialogOpen();
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 0), BUTTON_FIND_NEXT); Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 0), BUTTON_FIND_NEXT);
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 1), BUTTON_REPLACE_NEXT); Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 1), BUTTON_REPLACE_NEXT);
@ -150,8 +151,8 @@ DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace)
} }
DialogSearchReplace::~DialogSearchReplace() { DialogSearchReplace::~DialogSearchReplace() {
Search.isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled(); c->search->isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
Search.matchCase = CheckMatchCase->IsChecked(); c->search->matchCase = CheckMatchCase->IsChecked();
OPT_SET("Tool/Search Replace/Match Case")->SetBool(CheckMatchCase->IsChecked()); OPT_SET("Tool/Search Replace/Match Case")->SetBool(CheckMatchCase->IsChecked());
OPT_SET("Tool/Search Replace/RegExp")->SetBool(CheckRegExp->IsChecked()); OPT_SET("Tool/Search Replace/RegExp")->SetBool(CheckRegExp->IsChecked());
OPT_SET("Tool/Search Replace/Field")->SetInt(Field->GetSelection()); OPT_SET("Tool/Search Replace/Field")->SetInt(Field->GetSelection());
@ -164,25 +165,25 @@ void DialogSearchReplace::FindReplace(int mode) {
wxString LookFor = FindEdit->GetValue(); wxString LookFor = FindEdit->GetValue();
if (!LookFor) return; if (!LookFor) return;
Search.isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled(); c->search->isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
Search.matchCase = CheckMatchCase->IsChecked(); c->search->matchCase = CheckMatchCase->IsChecked();
Search.LookFor = LookFor; c->search->LookFor = LookFor;
Search.initialized = true; c->search->initialized = true;
Search.affect = Affect->GetSelection(); c->search->affect = Affect->GetSelection();
Search.field = Field->GetSelection(); c->search->field = Field->GetSelection();
if (hasReplace) { if (hasReplace) {
wxString ReplaceWith = ReplaceEdit->GetValue(); wxString ReplaceWith = ReplaceEdit->GetValue();
Search.ReplaceWith = ReplaceWith; c->search->ReplaceWith = ReplaceWith;
config::mru->Add("Replace", from_wx(ReplaceWith)); config::mru->Add("Replace", from_wx(ReplaceWith));
} }
if (mode == 0) if (mode == 0)
Search.FindNext(); c->search->FindNext();
else if (mode == 1) else if (mode == 1)
Search.ReplaceNext(); c->search->ReplaceNext();
else else
Search.ReplaceAll(); c->search->ReplaceAll();
config::mru->Add("Find", from_wx(LookFor)); config::mru->Add("Find", from_wx(LookFor));
UpdateDropDowns(); UpdateDropDowns();
@ -204,8 +205,9 @@ void DialogSearchReplace::UpdateDropDowns() {
update_mru(ReplaceEdit, "Replace"); update_mru(ReplaceEdit, "Replace");
} }
SearchReplaceEngine::SearchReplaceEngine() SearchReplaceEngine::SearchReplaceEngine(agi::Context *c)
: curLine(0) : context(c)
, curLine(0)
, pos(0) , pos(0)
, matchLen(0) , matchLen(0)
, replaceLen(0) , replaceLen(0)
@ -216,7 +218,6 @@ SearchReplaceEngine::SearchReplaceEngine()
, initialized(false) , initialized(false)
, field(FIELD_TEXT) , field(FIELD_TEXT)
, affect(LIMIT_ALL) , affect(LIMIT_ALL)
, context(nullptr)
{ {
} }
@ -373,7 +374,7 @@ void SearchReplaceEngine::ReplaceAll() {
} }
} }
else { else {
if (!Search.matchCase) { if (!matchCase) {
bool replaced = false; bool replaced = false;
wxString Left, Right = *Text; wxString Left, Right = *Text;
size_t pos = 0; size_t pos = 0;
@ -439,5 +440,3 @@ void SearchReplaceEngine::OpenDialog(bool replace) {
diag->Show(); diag->Show();
hasReplace = replace; hasReplace = replace;
} }
SearchReplaceEngine Search;

View file

@ -41,6 +41,8 @@ class wxComboBox;
class wxRadioBox; class wxRadioBox;
class SearchReplaceEngine { class SearchReplaceEngine {
agi::Context *context;
int curLine; int curLine;
size_t pos; size_t pos;
size_t matchLen; size_t matchLen;
@ -56,24 +58,21 @@ class SearchReplaceEngine {
wxString ReplaceWith; wxString ReplaceWith;
public: public:
agi::Context *context;
void FindNext() { ReplaceNext(false); } void FindNext() { ReplaceNext(false); }
void ReplaceNext(bool DoReplace=true); void ReplaceNext(bool DoReplace=true);
void ReplaceAll(); void ReplaceAll();
void OpenDialog(bool HasReplace); void OpenDialog(bool HasReplace);
void OnDialogOpen(); void OnDialogOpen();
SearchReplaceEngine(); SearchReplaceEngine(agi::Context *c);
friend class DialogSearchReplace; friend class DialogSearchReplace;
}; };
// Instance
extern SearchReplaceEngine Search;
class DialogSearchReplace : public wxDialog { class DialogSearchReplace : public wxDialog {
friend class SearchReplaceEngine; friend class SearchReplaceEngine;
agi::Context *c;
bool hasReplace; bool hasReplace;
wxComboBox *FindEdit; wxComboBox *FindEdit;

View file

@ -376,7 +376,7 @@ void FrameMain::InitContents() {
StartupLog("Create subtitles grid"); StartupLog("Create subtitles grid");
context->subsGrid = SubsGrid = new SubtitlesGrid(Panel, context.get()); context->subsGrid = SubsGrid = new SubtitlesGrid(Panel, context.get());
context->selectionController = context->subsGrid; context->selectionController = context->subsGrid;
Search.context = context.get(); context->search = new SearchReplaceEngine(context.get());
StartupLog("Create video box"); StartupLog("Create video box");
videoBox = new VideoBox(Panel, false, context.get()); videoBox = new VideoBox(Panel, false, context.get());

View file

@ -4,6 +4,7 @@ class AudioController;
class AssDialogue; class AssDialogue;
class AudioKaraoke; class AudioKaraoke;
class DialogManager; class DialogManager;
class SearchReplaceEngine;
template<class T> class SelectionController; template<class T> class SelectionController;
class SubsTextEditCtrl; class SubsTextEditCtrl;
class SubtitlesGrid; class SubtitlesGrid;
@ -26,6 +27,8 @@ struct Context {
TextSelectionController *textSelectionController; TextSelectionController *textSelectionController;
VideoContext *videoController; VideoContext *videoController;
SearchReplaceEngine *search;
// Things that should probably be in some sort of UI-context-model // Things that should probably be in some sort of UI-context-model
wxWindow *parent; wxWindow *parent;
wxWindow *previousFocus; wxWindow *previousFocus;