From c8f5b8c4a288c3fca9fd4f047893149292c42fc1 Mon Sep 17 00:00:00 2001 From: twanvl Date: Mon, 23 Apr 2007 23:02:38 +0000 Subject: [PATCH] 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 --- src/gui/set/cards_panel.cpp | 13 +++++++++---- src/gui/value/text.cpp | 11 ++++++++--- src/util/tagged_string.cpp | 15 +++++++++++++++ src/util/tagged_string.hpp | 5 +++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/gui/set/cards_panel.cpp b/src/gui/set/cards_panel.cpp index cce05ef0..db54f6bf 100644 --- a/src/gui/set/cards_panel.cpp +++ b/src/gui/set/cards_panel.cpp @@ -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; }; diff --git a/src/gui/value/text.cpp b/src/gui/value/text.cpp index 3ad594a5..eb4cf31c 100644 --- a/src/gui/value/text.cpp +++ b/src/gui/value/text.cpp @@ -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; diff --git a/src/util/tagged_string.cpp b/src/util/tagged_string.cpp index bea0b7b9..e875e658 100644 --- a/src/util/tagged_string.cpp +++ b/src/util/tagged_string.cpp @@ -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) { diff --git a/src/util/tagged_string.hpp b/src/util/tagged_string.hpp index fb0e33cb..15be2494 100644 --- a/src/util/tagged_string.hpp +++ b/src/util/tagged_string.hpp @@ -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.