Commit 188fbe73 authored by twanvl's avatar twanvl

correct cursor position for formating actions

parent 7ceb069c
...@@ -96,26 +96,29 @@ TextValue& TextValueAction::value() const { ...@@ -96,26 +96,29 @@ TextValue& TextValueAction::value() const {
} }
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start, size_t end, const String& action_name) { TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name) {
if (start > end) swap(start, end); if (start > end) {
swap(start, end);
swap(start_i, end_i);
}
String new_value; String new_value;
const String& str = value->value(); const String& str = value->value();
// Are we inside the tag we are toggling? // Are we inside the tag we are toggling?
size_t tagpos = in_tag(str, _("<") + tag, start, end); size_t tagpos = in_tag(str, _("<") + tag, start_i, end_i);
if (tagpos == String::npos) { if (tagpos == String::npos) {
// we are not inside this tag, add it // we are not inside this tag, add it
new_value = str.substr(0, start); new_value = str.substr(0, start_i);
new_value += _("<") + tag + _(">"); new_value += _("<") + tag + _(">");
new_value += str.substr(start, end - start); new_value += str.substr(start_i, end_i - start_i);
new_value += _("</") + tag + _(">"); new_value += _("</") + tag + _(">");
new_value += str.substr(end); new_value += str.substr(end_i);
} else { } else {
// we are inside this tag, _('remove') it // we are inside this tag, _('remove') it
new_value = str.substr(0, start); new_value = str.substr(0, start_i);
new_value += _("</") + tag + _(">"); new_value += _("</") + tag + _(">");
new_value += str.substr(start, end - start); new_value += str.substr(start_i, end_i - start_i);
new_value += _("<") + tag + _(">"); new_value += _("<") + tag + _(">");
new_value += str.substr(end); new_value += str.substr(end_i);
} }
// Build action // Build action
if (start != end) { if (start != end) {
...@@ -132,7 +135,10 @@ TextValueAction* toggle_format_action(const TextValueP& value, const String& tag ...@@ -132,7 +135,10 @@ TextValueAction* toggle_format_action(const TextValueP& value, const String& tag
TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) { TextValueAction* typing_action(const TextValueP& value, size_t start_i, size_t end_i, size_t start, size_t end, const String& replacement, const String& action_name) {
bool reverse = start > end; bool reverse = start > end;
if (reverse) swap(start, end); if (reverse) {
swap(start, end);
swap(start_i, end_i);
}
String new_value = tagged_substr_replace(value->value(), start_i, end_i, replacement); String new_value = tagged_substr_replace(value->value(), start_i, end_i, replacement);
if (value->value() == new_value) { if (value->value() == new_value) {
// no change // no change
......
...@@ -72,7 +72,7 @@ class TextValueAction : public ValueAction { ...@@ -72,7 +72,7 @@ class TextValueAction : public ValueAction {
}; };
/// Action for toggleing some formating tag on or off in some range /// Action for toggleing some formating tag on or off in some range
TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start, size_t end, const String& action_name); TextValueAction* toggle_format_action(const TextValueP& value, const String& tag, size_t start_i, size_t end_i, size_t start, size_t end, const String& action_name);
/// Typing in a TextValue, replace the selection [start...end) with replacement /// Typing in a TextValue, replace the selection [start...end) with replacement
/** start and end are cursor positions, start_i and end_i are indices*/ /** start and end are cursor positions, start_i and end_i are indices*/
......
...@@ -390,15 +390,15 @@ void TextValueEditor::doFormat(int type) { ...@@ -390,15 +390,15 @@ void TextValueEditor::doFormat(int type) {
size_t ss = selection_start, se = selection_end; size_t ss = selection_start, se = selection_end;
switch (type) { switch (type) {
case ID_FORMAT_BOLD: { case ID_FORMAT_BOLD: {
getSet().actions.add(toggle_format_action(valueP(), _("b"), selection_start_i, selection_end_i, _("Bold"))); getSet().actions.add(toggle_format_action(valueP(), _("b"), selection_start_i, selection_end_i, selection_start, selection_end, _("Bold")));
break; break;
} }
case ID_FORMAT_ITALIC: { case ID_FORMAT_ITALIC: {
getSet().actions.add(toggle_format_action(valueP(), _("i"), selection_start_i, selection_end_i, _("Italic"))); getSet().actions.add(toggle_format_action(valueP(), _("i"), selection_start_i, selection_end_i, selection_start, selection_end, _("Italic")));
break; break;
} }
case ID_FORMAT_SYMBOL: { case ID_FORMAT_SYMBOL: {
getSet().actions.add(toggle_format_action(valueP(), _("sym"), selection_start_i, selection_end_i, _("Symbols"))); getSet().actions.add(toggle_format_action(valueP(), _("sym"), selection_start_i, selection_end_i, selection_start, selection_end, _("Symbols")));
break; break;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment