[reader] Make improve coding style and added empty view message

This commit is contained in:
Laury
2021-09-05 21:10:56 +02:00
parent 7f0b8170d4
commit 19ee32986f
21 changed files with 349 additions and 387 deletions

View File

@@ -1,2 +1,2 @@
Default,calculation,rpn,graph,code,statistics,probability,solver,atomic,sequence,regression,settings
HidePython,calculation,rpn,graph,code,statistics,probability,solver,atomic,sequence,regression,settings
Default,calculation,rpn,graph,code,statistics,probability,solver,atomic,sequence,regression,reader,settings
HidePython,calculation,rpn,graph,code,statistics,probability,solver,atomic,sequence,regression,reader,settings
1 Default calculation rpn graph code statistics probability solver atomic sequence regression reader settings
2 HidePython calculation rpn graph code statistics probability solver atomic sequence regression reader settings

View File

@@ -1,4 +1,4 @@
apps += reader::App
apps += Reader::App
app_headers += apps/reader/app.h
app_sreader_src = $(addprefix apps/reader/,\

View File

@@ -4,7 +4,7 @@
#include "apps/i18n.h"
namespace reader {
namespace Reader {
I18n::Message App::Descriptor::name() {
return I18n::Message::ReaderApp;
@@ -32,7 +32,8 @@ App::Descriptor * App::Snapshot::descriptor() {
App::App(Snapshot * snapshot) :
::App(snapshot, &m_stackViewController),
m_listBookController(&m_stackViewController),
m_stackViewController(nullptr, &m_listBookController)
m_alternateEmptyViewController(&m_stackViewController, &m_listBookController, &m_listBookController),
m_stackViewController(&m_modalViewController, &m_alternateEmptyViewController)
{
}

View File

@@ -4,7 +4,7 @@
#include <escher.h>
#include "list_book_controller.h"
namespace reader {
namespace Reader {
class App : public ::App {
public:
@@ -22,6 +22,7 @@ public:
private:
App(Snapshot * snapshot);
ListBookController m_listBookController;
AlternateEmptyViewController m_alternateEmptyViewController;
StackViewController m_stackViewController;
};

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay = "Keine Dateien zum Anzeigen!"
ReaderApp = "Leser"
ReaderAppCapital = "LESER"
NoFileToDisplay = "Keine Dateien zum Anzeigen"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay = "No file to display!"
NoFileToDisplay = "No file to display"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay ="No hay archivos para mostrar!"
ReaderApp = "Lector"
ReaderAppCapital = "LECTOR"
NoFileToDisplay ="No hay archivos para mostrar"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay = "Aucun fichier à afficher!"
ReaderApp = "Liseuse"
ReaderAppCapital = "LISEUSE"
NoFileToDisplay = "Aucun fichier à afficher"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay ="Nincs megjeleníthető fájl"
ReaderApp = "Olvasó"
ReaderAppCapital = "OLVASÓ"
NoFileToDisplay = "Nincs megjeleníthető fájl"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay ="essun file da visualizzare"
ReaderApp = "Lettore"
ReaderAppCapital = "LETTORE"
NoFileToDisplay = "essun file da visualizzare"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay ="Geen bestanden om weer te geven"
ReaderApp = "Lezer"
ReaderAppCapital = "LEZER"
NoFileToDisplay = "Geen bestanden om weer te geven"

View File

@@ -1,3 +1,3 @@
ReaderApp = "Reader"
ReaderAppCapital = "READER"
NoFileToDisplay ="Nenhum arquivo para exibir"
ReaderApp = "Leitor"
ReaderAppCapital = "LEITOR"
NoFileToDisplay = "Nenhum arquivo para exibir"

View File

@@ -1,102 +1,100 @@
#include "list_book_controller.h"
#include "utility.h"
#include <escher/metric.h>
#include "apps/i18n.h"
namespace reader
namespace Reader
{
View* ListBookController::view()
{
return &m_tableView;
View* ListBookController::view() {
return &m_tableView;
}
ListBookController::ListBookController(Responder * parentResponder):
ViewController(parentResponder),
m_tableView(this, this, this),
m_readBookController(this)
ViewController(parentResponder),
m_tableView(this, this, this),
m_readBookController(this)
{
m_nbFiles = filesWithExtension(".txt", m_files, NB_FILES);
cleanRemovedBookRecord();
m_filesNumber = filesWithExtension(".txt", m_files, k_maxFilesNumber);
cleanRemovedBookRecord();
}
int ListBookController::numberOfRows() const
{
return m_nbFiles;
int ListBookController::numberOfRows() const {
return m_filesNumber;
}
KDCoordinate ListBookController::cellHeight()
{
return 50;
KDCoordinate ListBookController::cellHeight() {
return Metric::StoreRowHeight;
}
HighlightCell * ListBookController::reusableCell(int index)
{
return &m_cells[index];
HighlightCell * ListBookController::reusableCell(int index) {
return &m_cells[index];
}
int ListBookController::reusableCellCount() const
{
return NB_CELLS;
int ListBookController::reusableCellCount() const {
return k_cellsNumber;
}
void ListBookController::willDisplayCellForIndex(HighlightCell * cell, int index)
{
MessageTableCell* myTextCell = static_cast<MessageTableCell*>(cell);
MessageTextView* textView = static_cast<MessageTextView*>(myTextCell->labelView());
textView->setText(m_files[index].name);
myTextCell->setMessageFont(KDFont::LargeFont);
void ListBookController::willDisplayCellForIndex(HighlightCell * cell, int index) {
MessageTableCell* myTextCell = static_cast<MessageTableCell*>(cell);
MessageTextView* textView = static_cast<MessageTextView*>(myTextCell->labelView());
textView->setText(m_files[index].name);
myTextCell->setMessageFont(KDFont::LargeFont); //TODO set cell font at building ?
}
void ListBookController::didBecomeFirstResponder()
{
if (selectedRow() < 0) {
selectCellAtLocation(0, 0);
}
Container::activeApp()->setFirstResponder(&m_tableView);
if(m_nbFiles == 0)
void ListBookController::didBecomeFirstResponder() {
if (selectedRow() < 0) {
selectCellAtLocation(0, 0);
}
Container::activeApp()->setFirstResponder(&m_tableView);
}
bool ListBookController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right)
{
m_readBookController.setBook(m_files[selectedRow()]);
static_cast<StackViewController*>(parentResponder())->push(&m_readBookController);
Container::activeApp()->setFirstResponder(&m_readBookController);
return true;
}
return false;
}
bool ListBookController::hasBook(const char* filename) const {
for(int i=0;i<m_filesNumber;i++)
{
if(strcmp(m_files[i].name, filename) == 0)
{
Container::activeApp()->displayWarning(I18n::Message::NoFileToDisplay);
return true;
}
}
return false;
}
bool ListBookController::handleEvent(Ion::Events::Event event)
{
if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right)
{
m_readBookController.setBook(m_files[selectedRow()]);
static_cast<StackViewController*>(parentResponder())->push(&m_readBookController);
Container::activeApp()->setFirstResponder(&m_readBookController);
return true;
}
return false;
}
bool ListBookController::hasBook(const char* filename) const
{
for(int i=0;i<m_nbFiles;i++)
{
if(strcmp(m_files[i].name, filename) == 0)
{
return true;
}
}
return false;
}
void ListBookController::cleanRemovedBookRecord()
{
void ListBookController::cleanRemovedBookRecord() {
int nb = Ion::Storage::sharedStorage()->numberOfRecordsWithExtension("txt");
for(int i=0; i<nb; i++)
{
Ion::Storage::Record r = Ion::Storage::sharedStorage()->recordWithExtensionAtIndex("txt", i);
if(!hasBook(r.fullName()))
{
r.destroy();
}
Ion::Storage::Record r = Ion::Storage::sharedStorage()->recordWithExtensionAtIndex("txt", i);
if(!hasBook(r.fullName()))
{
r.destroy();
}
}
}
bool ListBookController::isEmpty() const {
return m_filesNumber == 0;
}
I18n::Message ListBookController::emptyMessage() {
return I18n::Message::NoFileToDisplay;
}
Responder * ListBookController::defaultController() {
return parentResponder();
}
}

View File

@@ -6,35 +6,35 @@
#include "read_book_controller.h"
namespace reader
namespace Reader
{
class ListBookController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource
class ListBookController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource, public AlternateEmptyViewDefaultDelegate
{
public:
ListBookController(Responder * parentResponder);
View* view() override;
ListBookController(Responder * parentResponder);
View* view() override;
int numberOfRows() const override;
KDCoordinate cellHeight() override;
HighlightCell * reusableCell(int index) override;
int reusableCellCount() const override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;
void didBecomeFirstResponder() override;
bool handleEvent(Ion::Events::Event event) override;
bool hasBook(const char* filename) const;
void cleanRemovedBookRecord();
int numberOfRows() const override;
KDCoordinate cellHeight() override;
HighlightCell * reusableCell(int index) override;
int reusableCellCount() const override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;
void didBecomeFirstResponder() override;
bool handleEvent(Ion::Events::Event event) override;
bool hasBook(const char* filename) const;
void cleanRemovedBookRecord();
bool isEmpty() const override;
I18n::Message emptyMessage() override;
Responder * defaultController() override;
private:
SelectableTableView m_tableView;
static const int NB_FILES = 20;
External::Archive::File m_files[NB_FILES];
int m_nbFiles = 0;
static const int NB_CELLS = 6;
MessageTableCell m_cells[NB_CELLS];
ReadBookController m_readBookController;
SelectableTableView m_tableView;
static const int k_maxFilesNumber = 20;
External::Archive::File m_files[k_maxFilesNumber];
int m_filesNumber = 0;
static const int k_cellsNumber = 6;
MessageTableCellWithChevron m_cells[k_cellsNumber];
ReadBookController m_readBookController;
};
}

View File

@@ -1,6 +1,6 @@
#include "read_book_controller.h"
namespace reader
namespace Reader
{
ReadBookController::ReadBookController(Responder * parentResponder) :
@@ -8,60 +8,49 @@ ReadBookController::ReadBookController(Responder * parentResponder) :
{
}
View * ReadBookController::view()
{
View * ReadBookController::view() {
return &m_readerView;
}
void ReadBookController::setBook(const External::Archive::File& file)
{
m_file = &file;
loadPosition();
m_readerView.setText(reinterpret_cast<const char*>(file.data), file.dataLength);
void ReadBookController::setBook(const External::Archive::File& file) {
m_file = &file;
loadPosition();
m_readerView.setText(reinterpret_cast<const char*>(file.data), file.dataLength);
}
bool ReadBookController::handleEvent(Ion::Events::Event event)
{
if(event == Ion::Events::Down)
{
m_readerView.nextPage();
return true;
}
if(event == Ion::Events::Up)
{
m_readerView.previousPage();
return true;
}
if(event == Ion::Events::Back || event == Ion::Events::Home)
{
savePosition();
}
return false;
bool ReadBookController::handleEvent(Ion::Events::Event event) {
if(event == Ion::Events::Down) {
m_readerView.nextPage();
return true;
}
if(event == Ion::Events::Up) {
m_readerView.previousPage();
return true;
}
if(event == Ion::Events::Back || event == Ion::Events::Home) {
savePosition();
}
return false;
}
void ReadBookController::savePosition() const
{
void ReadBookController::savePosition() const {
int pageOffset = m_readerView.getPageOffset();
Ion::Storage::Record::ErrorStatus status = Ion::Storage::sharedStorage()->createRecordWithFullName(m_file->name, &pageOffset, sizeof(pageOffset));
if(Ion::Storage::Record::ErrorStatus::NameTaken == status)
{
Ion::Storage::Record::Data data;
data.buffer = &pageOffset;
data.size = sizeof(pageOffset);
status = Ion::Storage::sharedStorage()->recordNamed(m_file->name).setValue(data);
if(Ion::Storage::Record::ErrorStatus::NameTaken == status) {
Ion::Storage::Record::Data data;
data.buffer = &pageOffset;
data.size = sizeof(pageOffset);
status = Ion::Storage::sharedStorage()->recordNamed(m_file->name).setValue(data);
}
}
void ReadBookController::loadPosition()
{
void ReadBookController::loadPosition() {
Ion::Storage::Record r = Ion::Storage::sharedStorage()->recordNamed(m_file->name);
if(Ion::Storage::sharedStorage()->hasRecord(r))
{
if(Ion::Storage::sharedStorage()->hasRecord(r)) {
int pageOffset = *(static_cast<const int*>(r.value().buffer));
m_readerView.setPageOffset(pageOffset);
}
else
{
else {
m_readerView.setPageOffset(0);
}
}

View File

@@ -5,7 +5,7 @@
#include "apps/external/archive.h"
#include "word_wrap_view.h"
namespace reader {
namespace Reader {
class ReadBookController : public ViewController {
public:

View File

@@ -8,109 +8,100 @@
#include <sys/stat.h>
#endif
namespace reader
namespace Reader
{
bool stringEndsWith(const char* str, const char* pattern)
{
int strLength = strlen(str);
int patternLength = strlen(pattern);
if (patternLength > strLength)
return false;
const char* strIter = str + strlen(str);
const char* patternIter = pattern + strlen(pattern);
while(*strIter == *patternIter)
{
if(patternIter == pattern)
return true;
strIter--;
patternIter--;
}
bool stringEndsWith(const char* str, const char* pattern) {
int strLength = strlen(str);
int patternLength = strlen(pattern);
if (patternLength > strLength) {
return false;
}
const char* strIter = str + strlen(str);
const char* patternIter = pattern + strlen(pattern);
while(*strIter == *patternIter) {
if(patternIter == pattern) {
return true;
}
strIter--;
patternIter--;
}
return false;
}
void stringNCopy(char* dest, int max, const char* src, int len)
{
while(len>0 && max >1 && *src)
{
*dest = *src;
dest++;
src++;
len--;
max--;
}
*dest=0;
void stringNCopy(char* dest, int max, const char* src, int len) {
while(len>0 && max >1 && *src)
{
*dest = *src;
dest++;
src++;
len--;
max--;
}
*dest=0;
}
#ifdef DEVICE
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize)
{
size_t nbTotalFiles = External::Archive::numberOfFiles();
int nbFiles = 0;
for(size_t i=0; i < nbTotalFiles; ++i)
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) {
size_t nbTotalFiles = External::Archive::numberOfFiles();
int nbFiles = 0;
for(size_t i=0; i < nbTotalFiles; ++i)
{
External::Archive::File file;
External::Archive::fileAtIndex(i, file);
if(stringEndsWith(file.name, ".txt"))
{
External::Archive::File file;
External::Archive::fileAtIndex(i, file);
if(stringEndsWith(file.name, ".txt"))
{
files[nbFiles] = file;
nbFiles++;
if(nbFiles == filesSize)
break;
}
files[nbFiles] = file;
nbFiles++;
if(nbFiles == filesSize)
break;
}
return nbFiles;
}
return nbFiles;
}
#else
static void fillFileData(External::Archive::File& file)
{
file.data = nullptr;
file.dataLength = 0;
static void fillFileData(External::Archive::File& file) {
file.data = nullptr;
file.dataLength = 0;
struct stat info;
if (stat(file.name, &info) != 0)
{
return;
}
unsigned char* content = new unsigned char[info.st_size];
if (content == NULL)
{
return ;
}
FILE *fp = fopen(file.name, "rb");
if (fp == NULL)
{
return ;
}
fread(content, info.st_size, 1, fp);
fclose(fp);
file.data = content;
file.dataLength = info.st_size;
struct stat info;
if (stat(file.name, &info) != 0) {
return;
}
unsigned char* content = new unsigned char[info.st_size];
if (content == NULL) {
return;
}
FILE *fp = fopen(file.name, "rb");
if (fp == NULL) {
return ;
}
fread(content, info.st_size, 1, fp);
fclose(fp);
file.data = content;
file.dataLength = info.st_size;
}
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize)
{
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) {
dirent *file;
DIR *d = opendir(".");
int nb = 0;
if (d)
{
while ((file = readdir(d)) != NULL)
{
if(stringEndsWith(file->d_name, extension))
{
if (d) {
while ((file = readdir(d)) != NULL) {
if(stringEndsWith(file->d_name, extension)) {
files[nb].name = strdup(file->d_name);//will probably leak
fillFileData(files[nb]);
nb++;
if(nb == filesSize)
break;
if(nb == filesSize) {
break;
}
}
}
closedir(d);

View File

@@ -3,11 +3,11 @@
#include <apps/external/archive.h>
namespace reader
namespace Reader
{
bool stringEndsWith(const char* str, const char* end);
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize) ;
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize);
void stringNCopy(char* dest, int max, const char* src, int len);
}

View File

@@ -2,155 +2,138 @@
#include "utility.h"
namespace reader
namespace Reader
{
void WordWrapTextView::nextPage()
{
if(m_nextPageOffset >= m_length)
return;
m_pageOffset = m_nextPageOffset;
markRectAsDirty(bounds());
void WordWrapTextView::nextPage() {
if(m_nextPageOffset >= m_length) {
return;
}
m_pageOffset = m_nextPageOffset;
markRectAsDirty(bounds());
}
void WordWrapTextView::setText(const char* text, int length)
{
PointerTextView::setText(text);
m_length = length;
void WordWrapTextView::setText(const char* text, int length) {
PointerTextView::setText(text);
m_length = length;
}
void WordWrapTextView::previousPage()
{
if(m_pageOffset <= 0)
return;
void WordWrapTextView::previousPage() {
if(m_pageOffset <= 0) {
return;
}
const int spaceWidth = m_font->stringSize(" ").width();
const int spaceWidth = m_font->stringSize(" ").width();
const char * endOfWord = text() + m_pageOffset;
const char * startOfWord = UTF8Helper::BeginningOfWord(text(), endOfWord);
KDPoint textPosition(m_frame.width() - margin, m_frame.height() - margin);
const char * endOfWord = text() + m_pageOffset;
const char * startOfWord = UTF8Helper::BeginningOfWord(text(), endOfWord);
while(startOfWord>=text()) {
endOfWord = UTF8Helper::EndOfWord(startOfWord);
KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord);
KDPoint previousTextPosition = KDPoint(textPosition.x()-textSize.width(), textPosition.y());
KDPoint textPosition(m_frame.width() - margin, m_frame.height() - margin);
if(previousTextPosition.x() < margin) {
textPosition = KDPoint(m_frame.width() - margin, textPosition.y() - textSize.height());
previousTextPosition = KDPoint(textPosition.x() - textSize.width(), textPosition.y());
}
if(textPosition.y() - textSize.height() < margin) {
break;
}
while(startOfWord>=text())
{
endOfWord = UTF8Helper::EndOfWord(startOfWord);
KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord);
KDPoint previousTextPosition = KDPoint(textPosition.x()-textSize.width(), textPosition.y());
if(previousTextPosition.x() < margin)
{
textPosition = KDPoint(m_frame.width() - margin, textPosition.y() - textSize.height());
previousTextPosition = KDPoint(textPosition.x() - textSize.width(), textPosition.y());
}
if(textPosition.y() - textSize.height() < margin)
{
break;
}
--startOfWord;
while(startOfWord >= text() && *startOfWord == ' ')
{
previousTextPosition = KDPoint(previousTextPosition.x() - spaceWidth, previousTextPosition.y());
--startOfWord;
}
if(previousTextPosition.x() < margin)
{
previousTextPosition = KDPoint(m_frame.width() - margin, previousTextPosition.y() - textSize.height());
}
while(startOfWord >= text() && *startOfWord == '\n')
{
previousTextPosition = KDPoint(m_frame.width() - margin, previousTextPosition.y() - textSize.height());
--startOfWord;
}
if(previousTextPosition.y() - textSize.height() < margin)
{
break;
}
textPosition = previousTextPosition;
endOfWord = startOfWord;
startOfWord = UTF8Helper::BeginningOfWord(text(), endOfWord);
--startOfWord;
while(startOfWord >= text() && *startOfWord == ' ') {
previousTextPosition = KDPoint(previousTextPosition.x() - spaceWidth, previousTextPosition.y());
--startOfWord;
}
if(startOfWord == text())
m_pageOffset = 0;
else
m_pageOffset = UTF8Helper::EndOfWord(startOfWord) - text() + 1;
markRectAsDirty(bounds());
}
void WordWrapTextView::drawRect(KDContext * ctx, KDRect rect) const
{
ctx->fillRect(KDRect(0, 0, bounds().width(), bounds().height()), m_backgroundColor);
const char * endOfFile = text() + m_length;
const char * startOfWord = text() + m_pageOffset;
const char * endOfWord = UTF8Helper::EndOfWord(startOfWord);
KDPoint textPosition(margin, margin);
const int wordMaxLength = 128;
char word[wordMaxLength];
const int spaceWidth = m_font->stringSize(" ").width();
while(startOfWord < endOfFile)
{
KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord);
KDPoint nextTextPosition = KDPoint(textPosition.x()+textSize.width(), textPosition.y());
if(nextTextPosition.x() > m_frame.width() - margin)
{
textPosition = KDPoint(margin, textPosition.y() + textSize.height());
nextTextPosition = KDPoint(margin + textSize.width(), textPosition.y());
}
if(textPosition.y() + textSize.height() > m_frame.height() - margin)
{
break;
}
stringNCopy(word, wordMaxLength, startOfWord, endOfWord-startOfWord);
ctx->drawString(word, textPosition, m_font, m_textColor, m_backgroundColor);
while(*endOfWord == ' ')
{
nextTextPosition = KDPoint(nextTextPosition.x() + spaceWidth, nextTextPosition.y());
++endOfWord;
}
if(nextTextPosition.x() > m_frame.width() - margin)
{
nextTextPosition = KDPoint(margin, nextTextPosition.y() + textSize.height());
}
while(*endOfWord == '\n')
{
nextTextPosition = KDPoint(margin, nextTextPosition.y() + textSize.height());
++endOfWord;
}
if(nextTextPosition.y() + textSize.height() > m_frame.height() - margin)
{
break;
}
textPosition = nextTextPosition;
startOfWord = endOfWord;
endOfWord = UTF8Helper::EndOfWord(startOfWord);
if(previousTextPosition.x() < margin) {
previousTextPosition = KDPoint(m_frame.width() - margin, previousTextPosition.y() - textSize.height());
}
m_nextPageOffset = startOfWord - text();
while(startOfWord >= text() && *startOfWord == '\n') {
previousTextPosition = KDPoint(m_frame.width() - margin, previousTextPosition.y() - textSize.height());
--startOfWord;
}
if(previousTextPosition.y() - textSize.height() < margin) {
break;
}
textPosition = previousTextPosition;
endOfWord = startOfWord;
startOfWord = UTF8Helper::BeginningOfWord(text(), endOfWord);
}
if(startOfWord == text()) {
m_pageOffset = 0;
}
else {
m_pageOffset = UTF8Helper::EndOfWord(startOfWord) - text() + 1;
}
markRectAsDirty(bounds());
}
int WordWrapTextView::getPageOffset() const
{
return m_pageOffset;
void WordWrapTextView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(KDRect(0, 0, bounds().width(), bounds().height()), m_backgroundColor);
const char * endOfFile = text() + m_length;
const char * startOfWord = text() + m_pageOffset;
const char * endOfWord = UTF8Helper::EndOfWord(startOfWord);
KDPoint textPosition(margin, margin);
const int wordMaxLength = 128;
char word[wordMaxLength];
const int spaceWidth = m_font->stringSize(" ").width();
while(startOfWord < endOfFile) {
KDSize textSize = m_font->stringSizeUntil(startOfWord, endOfWord);
KDPoint nextTextPosition = KDPoint(textPosition.x()+textSize.width(), textPosition.y());
if(nextTextPosition.x() > m_frame.width() - margin) {
textPosition = KDPoint(margin, textPosition.y() + textSize.height());
nextTextPosition = KDPoint(margin + textSize.width(), textPosition.y());
}
if(textPosition.y() + textSize.height() > m_frame.height() - margin) {
break;
}
stringNCopy(word, wordMaxLength, startOfWord, endOfWord-startOfWord);
ctx->drawString(word, textPosition, m_font, m_textColor, m_backgroundColor);
while(*endOfWord == ' ') {
nextTextPosition = KDPoint(nextTextPosition.x() + spaceWidth, nextTextPosition.y());
++endOfWord;
}
if(nextTextPosition.x() > m_frame.width() - margin) {
nextTextPosition = KDPoint(margin, nextTextPosition.y() + textSize.height());
}
while(*endOfWord == '\n') {
nextTextPosition = KDPoint(margin, nextTextPosition.y() + textSize.height());
++endOfWord;
}
if(nextTextPosition.y() + textSize.height() > m_frame.height() - margin) {
break;
}
textPosition = nextTextPosition;
startOfWord = endOfWord;
endOfWord = UTF8Helper::EndOfWord(startOfWord);
}
m_nextPageOffset = startOfWord - text();
}
void WordWrapTextView::setPageOffset(int o)
{
m_pageOffset = o;
int WordWrapTextView::getPageOffset() const {
return m_pageOffset;
}
void WordWrapTextView::setPageOffset(int o) {
m_pageOffset = o;
}
}

View File

@@ -3,23 +3,23 @@
#include <escher.h>
namespace reader
namespace Reader
{
class WordWrapTextView : public PointerTextView {
public:
void drawRect(KDContext * ctx, KDRect rect) const override;
void setText(const char*, int length);
void nextPage();
void previousPage();
int getPageOffset() const;
void setPageOffset(int o);
void drawRect(KDContext * ctx, KDRect rect) const override;
void setText(const char*, int length);
void nextPage();
void previousPage();
int getPageOffset() const;
void setPageOffset(int o);
protected:
int m_pageOffset = 0;
mutable int m_nextPageOffset = 0;
int m_length = 0;
int m_pageOffset = 0;
mutable int m_nextPageOffset = 0;
int m_length = 0;
static const int margin = 10;
static const int margin = 10;
};
}

View File

@@ -57,8 +57,7 @@ const char * AlternateEmptyViewController::title() {
bool AlternateEmptyViewController::handleEvent(Ion::Events::Event event) {
if (m_contentView.alternateEmptyViewDelegate()->isEmpty()) {
if (event != Ion::Events::Home && event != Ion::Events::OnOff) {
m_contentView.alternateEmptyViewDelegate()->defaultController()->handleEvent(Ion::Events::Back);
return true;
return m_contentView.alternateEmptyViewDelegate()->defaultController()->handleEvent(Ion::Events::Back);
}
return false;
}