Use enums rather than magic numbers

This commit is contained in:
Thomas Goyne 2013-01-11 08:18:10 -08:00
parent 199507a58a
commit d459b7089c
2 changed files with 27 additions and 17 deletions

View file

@ -66,6 +66,18 @@ enum {
BUTTON_REPLACE_ALL BUTTON_REPLACE_ALL
}; };
enum {
FIELD_TEXT = 0,
FIELD_STYLE,
FIELD_ACTOR,
FIELD_EFFECT
};
enum {
LIMIT_ALL = 0,
LIMIT_SELECTED
};
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"))
, hasReplace(withReplace) , hasReplace(withReplace)
@ -202,22 +214,20 @@ SearchReplaceEngine::SearchReplaceEngine()
, isReg(false) , isReg(false)
, matchCase(false) , matchCase(false)
, initialized(false) , initialized(false)
, field(0) , field(FIELD_TEXT)
, affect(0) , affect(LIMIT_ALL)
, context(0) , context(nullptr)
{ {
} }
void SearchReplaceEngine::FindNext() {
ReplaceNext(false);
}
static boost::flyweight<wxString> *get_text(AssDialogue *cur, int field) { static boost::flyweight<wxString> *get_text(AssDialogue *cur, int field) {
if (field == 0) return &cur->Text; switch (field) {
else if (field == 1) return &cur->Style; case FIELD_TEXT: return &cur->Text;
else if (field == 2) return &cur->Actor; case FIELD_STYLE: return &cur->Style;
else if (field == 3) return &cur->Effect; case FIELD_ACTOR: return &cur->Actor;
else throw wxString("Invalid field"); case FIELD_EFFECT: return &cur->Effect;
default: throw agi::InternalError("Bad find/replace field", 0);
}
} }
void SearchReplaceEngine::ReplaceNext(bool DoReplace) { void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
@ -318,13 +328,13 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
context->subsGrid->SelectRow(curLine,false); context->subsGrid->SelectRow(curLine,false);
context->subsGrid->MakeCellVisible(curLine,0); context->subsGrid->MakeCellVisible(curLine,0);
if (field == 0) { if (field == FIELD_TEXT) {
context->selectionController->SetActiveLine(context->subsGrid->GetDialogue(curLine)); context->selectionController->SetActiveLine(context->subsGrid->GetDialogue(curLine));
context->textSelectionController->SetSelection(pos, pos + replaceLen); context->textSelectionController->SetSelection(pos, pos + replaceLen);
} }
// hAx to prevent double match on style/actor // hAx to prevent double match on style/actor
if (field != 0) replaceLen = 99999; else
replaceLen = 99999;
} }
LastWasFind = !DoReplace; LastWasFind = !DoReplace;
} }
@ -341,7 +351,7 @@ void SearchReplaceEngine::ReplaceAll() {
SubtitleSelection const& sel = context->selectionController->GetSelectedSet(); SubtitleSelection const& sel = context->selectionController->GetSelectedSet();
bool hasSelection = !sel.empty(); bool hasSelection = !sel.empty();
bool inSel = affect == 1; bool inSel = affect == LIMIT_SELECTED;
for (auto diag : context->ass->Line | agi::of_type<AssDialogue>()) { for (auto diag : context->ass->Line | agi::of_type<AssDialogue>()) {
if (inSel && hasSelection && !sel.count(diag)) if (inSel && hasSelection && !sel.count(diag))

View file

@ -58,7 +58,7 @@ class SearchReplaceEngine {
public: public:
agi::Context *context; agi::Context *context;
void FindNext(); 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);