From 6f6e037eb31eebf374ff7e084a838701e33fc846 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Thu, 7 Sep 2017 18:16:03 +0300 Subject: [PATCH] [fbreader] Added optional arguments to ZLTextSelectionModel methods ... extendTo() and selectWord() to make it easier to implement selection by words. --- .../text/src/area/ZLTextSelectionModel.cpp | 27 +++++++++++++++++-- .../text/src/area/ZLTextSelectionModel.h | 5 ++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.cpp b/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.cpp index 6bd5424..b3bf8e6 100644 --- a/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.cpp +++ b/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2004-2010 Geometer Plus + * Copyright (C) 2016-2017 Slava Monich * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -144,7 +145,7 @@ bool ZLTextSelectionModel::BoundElement::operator != (const ZLTextSelectionModel return !operator == (element); } -ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int y) { +ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int y, bool word) { if (!myIsActive || myArea.myTextElementMap.empty()) { return BOUND_NOT_CHANGED; } @@ -155,6 +156,25 @@ ZLTextSelectionModel::ExtensionResult ZLTextSelectionModel::extendTo(int x, int myStoredX = x; myStoredY = y; + if (word) { + const bool swap = (mySecondBound < myFirstBound); + Bound &from = swap ? mySecondBound : myFirstBound; + Bound &to = swap ? myFirstBound : mySecondBound; + BoundElement &after = from.After; + BoundElement &before = to.Before; + ZLTextWordCursor cursor = myArea.startCursor(); + cursor.moveToParagraph(before.ParagraphIndex); + cursor.moveTo(before.ElementIndex, before.CharIndex); + const ZLTextElement &element = cursor.element(); + if (element.kind() == ZLTextElement::WORD_ELEMENT) { + const ZLTextWord &word = (const ZLTextWord&)element; + before.CharIndex = word.Length; + } + after.CharIndex = 0; + from.Before = after; + to.After = before; + } + ExtensionResult result = BOUND_NOT_CHANGED; if ((oldRange.first != newRange.first) || (oldRange.second != newRange.second)) { myTextIsUpToDate = false; @@ -460,7 +480,7 @@ shared_ptr ZLTextSelectionModel::image() const { return myImage; } -bool ZLTextSelectionModel::selectWord(int x, int y) { +bool ZLTextSelectionModel::selectWord(int x, int y, bool activate) { clear(); const ZLTextElementRectangle *rectangle = myArea.elementByCoordinates(x, y); @@ -517,6 +537,9 @@ bool ZLTextSelectionModel::selectWord(int x, int y) { mySecondBound.Before.CharIndex = endIndex; mySecondBound.After = mySecondBound.Before; + myStoredX = x; + myStoredY = y; + myIsActive = activate; myIsEmpty = false; myTextIsUpToDate = false; myRangeVectorIsUpToDate = false; diff --git a/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.h b/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.h index 579490f..19a6363 100644 --- a/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.h +++ b/fbreader/fbreader/zlibrary/text/src/area/ZLTextSelectionModel.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2004-2010 Geometer Plus + * Copyright (C) 2016-2017 Slava Monich * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -58,13 +59,13 @@ public: BOUND_OVER_BEFORE, BOUND_OVER_AFTER }; - ExtensionResult extendTo(int x, int y); + ExtensionResult extendTo(int x, int y, bool word = false); void invalidate(); void update(); void deactivate(); void clear(); - bool selectWord(int x, int y); + bool selectWord(int x, int y, bool activate = false); void extendWordSelectionToParagraph(); const std::string &text() const;