mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
find/replace working better (but not done yet)
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@300 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -271,7 +271,7 @@ void CardsPanel::doPaste() { CUT_COPY_PASTE(doPaste, ;) }
|
||||
class CardsPanel::SearchFindInfo : public FindInfo {
|
||||
public:
|
||||
SearchFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {}
|
||||
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos) {
|
||||
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos, bool was_selection) {
|
||||
// Select the card
|
||||
panel.card_list->setCard(card);
|
||||
return true;
|
||||
@@ -283,13 +283,18 @@ class CardsPanel::SearchFindInfo : public FindInfo {
|
||||
class CardsPanel::ReplaceFindInfo : public FindInfo {
|
||||
public:
|
||||
ReplaceFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {}
|
||||
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos) {
|
||||
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos, bool was_selection) {
|
||||
// Select the card
|
||||
panel.card_list->setCard(card);
|
||||
// Replace
|
||||
panel.editor->insert(escape(what.GetReplaceString()), _("Replace"));
|
||||
return true;
|
||||
if (was_selection) {
|
||||
panel.editor->insert(escape(what.GetReplaceString()), _("Replace"));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
virtual bool searchSelection() const { return true; }
|
||||
private:
|
||||
CardsPanel& panel;
|
||||
};
|
||||
|
||||
@@ -677,14 +677,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) {
|
||||
if (!is_substr(s, pos, find.findString().Lower())) return false;
|
||||
}
|
||||
// handle
|
||||
bool was_selection = false;
|
||||
if (find.select()) {
|
||||
editor().select(this);
|
||||
editor().SetFocus();
|
||||
size_t old_sel_start = selection_start, old_sel_end = selection_end;
|
||||
selection_start_i = untagged_to_index(value().value(), pos, true);
|
||||
selection_end_i = untagged_to_index(value().value(), pos + find.findString().size(), true);
|
||||
fixSelection(TYPE_INDEX);
|
||||
was_selection = old_sel_start == selection_start && old_sel_end == selection_end;
|
||||
}
|
||||
if (find.handle(viewer.getCard(), valueP(), pos)) {
|
||||
if (find.handle(viewer.getCard(), valueP(), pos, was_selection)) {
|
||||
return true;
|
||||
} else {
|
||||
// TODO: string might have changed when doing replace all
|
||||
@@ -695,15 +698,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) {
|
||||
bool TextValueEditor::search(FindInfo& find, bool from_start) {
|
||||
String v = untag(value().value());
|
||||
if (!find.caseSensitive()) v.LowerCase();
|
||||
size_t selection_min = index_to_untagged(value().value(), min(selection_start_i, selection_end_i));
|
||||
size_t selection_max = index_to_untagged(value().value(), max(selection_start_i, selection_end_i));
|
||||
if (find.forward()) {
|
||||
size_t start = min(v.size(), max(selection_start, selection_end));
|
||||
size_t start = min(v.size(), find.searchSelection() ? selection_min : selection_max);
|
||||
size_t end = max(0, (int)v.size() - (int)find.findString().size());
|
||||
for (size_t i = start ; i <= end ; ++i) {
|
||||
if (matchSubstr(v, i, find)) return true;
|
||||
}
|
||||
} else {
|
||||
size_t start = 0;
|
||||
int end = (int)min(selection_start, selection_end) - (int)find.findString().size();
|
||||
int end = (int)(find.searchSelection() ? selection_max : selection_min) - (int)find.findString().size();
|
||||
if (end < 0) return false;
|
||||
for (size_t i = end ; i >= start ; --i) {
|
||||
if (matchSubstr(v, i, find)) return true;
|
||||
|
||||
@@ -329,6 +329,21 @@ size_t untagged_to_index(const String& str, size_t pos, bool inside) {
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t index_to_untagged(const String& str, size_t index) {
|
||||
size_t i = 0, p = 0;
|
||||
index = min(str.size(), index);
|
||||
while (i < index) {
|
||||
Char c = str.GetChar(i);
|
||||
if (c == _('<')) {
|
||||
i = skip_tag(str, i);
|
||||
} else {
|
||||
i++;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Global operations
|
||||
|
||||
String remove_tag(const String& str, const String& tag) {
|
||||
|
||||
@@ -119,6 +119,11 @@ size_t cursor_to_index(const String& str, size_t cursor, Movement dir = MOVE_MID
|
||||
*/
|
||||
size_t untagged_to_index(const String& str, size_t pos, bool inside);
|
||||
|
||||
/// Find the untagged position corresponding to the given tagged position.
|
||||
/** An untagged position in str is a position in untag(str).
|
||||
*/
|
||||
size_t index_to_untagged(const String& str, size_t index);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Global operations
|
||||
|
||||
/// Remove all instances of a tag and its close tag, but keep the contents.
|
||||
|
||||
Reference in New Issue
Block a user