From adf0b42e5bfc4b607490e1d4db8e04d50c37e9ac Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 28 Aug 2012 20:49:11 +0200 Subject: [PATCH] coolreader search highlight This is rough port of Android code, but seems to work --- cre.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ crereader.lua | 26 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/cre.cpp b/cre.cpp index 7a2e09503..16dda017d 100644 --- a/cre.cpp +++ b/cre.cpp @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#define DEBUG_CRENGINE 1 extern "C" { #include "blitbuffer.h" @@ -424,6 +425,73 @@ static int registerFont(lua_State *L) { return 0; } +// ported from Android UI kpvcrlib/crengine/android/jni/docview.cpp + +static int findText(lua_State *L) { + CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); + const char *l_pattern = luaL_checkstring(L, 2); + lString16 pattern = lString16(l_pattern); + int origin = luaL_checkint(L, 3); + bool reverse = luaL_checkint(L, 4); + bool caseInsensitive = luaL_checkint(L, 5); + const char *l_lastPatt = luaL_checkstring(L, 6); + lString16 _lastPattern = lString16(l_lastPatt); + + if ( pattern.empty() ) + return 0; + if ( pattern!=_lastPattern && origin==1 ) + origin = 0; + _lastPattern = pattern; + LVArray words; + lvRect rc; + doc->text_view->GetPos( rc ); + int pageHeight = rc.height(); + int start = -1; + int end = -1; + if ( reverse ) { + // reverse + if ( origin == 0 ) { + // from end current page to first page + end = rc.bottom; + } else if ( origin == -1 ) { + // from last page to end of current page + start = rc.bottom; + } else { // origin == 1 + // from prev page to first page + end = rc.top; + } + } else { + // forward + if ( origin == 0 ) { + // from current page to last page + start = rc.top; + } else if ( origin == -1 ) { + // from first page to current page + end = rc.top; + } else { // origin == 1 + // from next page to last + start = rc.bottom; + } + } + CRLog::debug("CRViewDialog::findText: Current page: %d .. %d", rc.top, rc.bottom); + CRLog::debug("CRViewDialog::findText: searching for text '%s' from %d to %d origin %d", LCSTR(pattern), start, end, origin ); + if ( doc->text_view->getDocument()->findText( pattern, caseInsensitive, reverse, start, end, words, 200, pageHeight ) ) { + CRLog::debug("CRViewDialog::findText: pattern found"); + doc->text_view->clearSelection(); + doc->text_view->selectWords( words ); + ldomMarkedRangeList * ranges = doc->text_view->getMarkedRanges(); + if ( ranges ) { + if ( ranges->length()>0 ) { + int pos = ranges->get(0)->start.y; + doc->text_view->SetPos(pos); + } + } + return 0; + } + CRLog::debug("CRViewDialog::findText: pattern not found"); + return 0; +} + static const struct luaL_Reg cre_func[] = { {"openDocument", openDocument}, {"getFontFaces", getFontFaces}, @@ -459,6 +527,7 @@ static const struct luaL_Reg credocument_meth[] = { //{"cursorLeft", cursorLeft}, //{"cursorRight", cursorRight}, {"drawCurrentPage", drawCurrentPage}, + {"findText", findText}, {"close", closeDocument}, {"__gc", closeDocument}, {NULL, NULL} diff --git a/crereader.lua b/crereader.lua index 56afb136e..3b2cbd4c9 100644 --- a/crereader.lua +++ b/crereader.lua @@ -592,3 +592,29 @@ function CREReader:adjustCreReaderCommands() end ) end + + +---------------------------------------------------- +--- search +---------------------------------------------------- +function CREReader:searchHighLight(search) + Debug("FIXME CreReader::searchHighLight", search) + + if self.last_search ~= nil then + self.last_search = { + search = "", + } + end + + self.doc:findText( + search, + 0, -- origin: 0=current 1=prev-first -1=backwards + 0, -- reverse: boolean + 1, -- caseInsensitive: boolean + self.last_search.search + ) + + self:redrawCurrentPage() + + self.last_search.search = search +end