bool ZLTextView::search(const std::string &text, bool ignoreCase, bool wholeText, bool backward, bool thisSectionOnly) { if (text.empty()) { return false; } size_t startIndex = 0; size_t endIndex = myModel->paragraphsNumber(); if (thisSectionOnly) { std::vector<size_t>::const_iterator i = nextBreakIterator(); if (i != myTextBreaks.begin()) { startIndex = *(i - 1); } if (i != myTextBreaks.end()) { endIndex = *i; } } myModel->search(text, startIndex, endIndex, ignoreCase); if (!startCursor().isNull()) { rebuildPaintInfo(true); ZLTextMark position = startCursor().position(); gotoMark(wholeText ? (backward ? myModel->lastMark() : myModel->firstMark()) : (backward ? myModel->previousMark(position) : //myModel->nextMark(position))); myModel->nextMark(position).ParagraphIndex > -1 ? myModel->nextMark(position) : myModel->previousMark(position))); application().refreshWindow(); } if(myModel->marks().empty()) return false; else return true; }
void ZLTextView::gotoMark(ZLTextMark mark) { if (mark.ParagraphIndex < 0) { return; } bool doRepaint = false; if (startCursor().isNull()) { doRepaint = true; preparePaintInfo(); } if (startCursor().isNull()) { return; } if (((int)startCursor().paragraphCursor().index() != mark.ParagraphIndex) || (startCursor().position() > mark)) { doRepaint = true; gotoParagraph(mark.ParagraphIndex); preparePaintInfo(); } if (endCursor().isNull()) { preparePaintInfo(); } while (mark > endCursor().position()) { doRepaint = true; scrollPage(true, NO_OVERLAPPING, 0); preparePaintInfo(); } if (doRepaint) { application().refreshWindow(); } }
bool ZLTextView::onStylusPress(int x, int y) { mySelectionModel.deactivate(); if (myModel.isNull()) { return false; } shared_ptr<ZLTextPositionIndicatorInfo> indicatorInfo = this->indicatorInfo(); if (!indicatorInfo.isNull() && indicatorInfo->isVisible() && indicatorInfo->isSensitive()) { myTreeStateIsFrozen = true; bool indicatorAnswer = positionIndicator()->onStylusPress(x, y); myTreeStateIsFrozen = false; if (indicatorAnswer) { application().refreshWindow(); return true; } } if (myModel->kind() == ZLTextModel::TREE_MODEL) { ZLTextTreeNodeMap::const_iterator it = std::find_if(myTreeNodeMap.begin(), myTreeNodeMap.end(), ZLTextTreeNodeArea::RangeChecker(x, y)); if (it != myTreeNodeMap.end()) { int paragraphNumber = it->ParagraphNumber; ZLTextTreeParagraph *paragraph = (ZLTextTreeParagraph*)(*myModel)[paragraphNumber]; paragraph->open(!paragraph->isOpen()); rebuildPaintInfo(true); preparePaintInfo(); if (paragraph->isOpen()) { int nextParagraphNumber = paragraphNumber + paragraph->fullSize(); int lastParagraphNumber = endCursor().paragraphCursor().index(); if (endCursor().isEndOfParagraph()) { ++lastParagraphNumber; } if (lastParagraphNumber < nextParagraphNumber) { gotoParagraph(nextParagraphNumber, true); preparePaintInfo(); } } int firstParagraphNumber = startCursor().paragraphCursor().index(); if (startCursor().isStartOfParagraph()) { --firstParagraphNumber; } if (firstParagraphNumber >= paragraphNumber) { gotoParagraph(paragraphNumber); preparePaintInfo(); } application().refreshWindow(); return true; } } return false; }
void BookTextView::scrollToHome() { if (!startCursor().isNull() && startCursor().isStartOfParagraph() && startCursor().paragraphCursor().index() == 0) { return; } gotoParagraph(0, false); fbreader().refreshWindow(); }
void ZLTextView::gotoCharIndex(size_t charIndex) { if (positionIndicator().isNull()) { return; } std::vector<size_t>::const_iterator i = nextBreakIterator(); const size_t startParagraphIndex = (i != myTextBreaks.begin()) ? *(i - 1) + 1 : 0; const size_t endParagraphIndex = (i != myTextBreaks.end()) ? *i : myModel->paragraphsNumber(); const size_t fullTextSize = myTextSize[endParagraphIndex] - myTextSize[startParagraphIndex]; charIndex = std::min(charIndex, fullTextSize - 1); std::vector<size_t>::const_iterator j = std::lower_bound(myTextSize.begin(), myTextSize.end(), charIndex + myTextSize[startParagraphIndex]); size_t paragraphIndex = j - myTextSize.begin(); if ((*myModel)[paragraphIndex]->kind() == ZLTextParagraph::END_OF_SECTION_PARAGRAPH) { gotoParagraph(paragraphIndex, true); return; } if (paragraphIndex > startParagraphIndex) { --paragraphIndex; } gotoParagraph(paragraphIndex, false); preparePaintInfo(); if (!positionIndicator().isNull()) { size_t endCharIndex = positionIndicator()->sizeOfTextBeforeCursor(endCursor()); if (endCharIndex > charIndex) { while (endCharIndex > charIndex) { scrollPage(false, SCROLL_LINES, 1); preparePaintInfo(); if (positionIndicator()->sizeOfTextBeforeCursor(startCursor()) <= myTextSize[startParagraphIndex]) { break; } endCharIndex = positionIndicator()->sizeOfTextBeforeCursor(endCursor()); } if (endCharIndex < charIndex) { scrollPage(true, SCROLL_LINES, 1); } } else { int startCharIndex = positionIndicator()->sizeOfTextBeforeCursor(startCursor()); while (endCharIndex < charIndex) { scrollPage(true, SCROLL_LINES, 1); preparePaintInfo(); const int newStartCharIndex = positionIndicator()->sizeOfTextBeforeCursor(startCursor()); if (newStartCharIndex <= startCharIndex) { break; } startCharIndex = newStartCharIndex; endCharIndex = positionIndicator()->sizeOfTextBeforeCursor(endCursor()); } if (endCharIndex > charIndex) { scrollPage(false, SCROLL_LINES, 1); } } } }
void ZLTextView::gotoParagraph(int num, bool end) { if (myModel.isNull()) { return; } if (!startCursor().isNull() && startCursor().isStartOfParagraph() && startCursor().paragraphCursor().isFirst() && (num >= (int)startCursor().paragraphCursor().index()) && !endCursor().isNull() && endCursor().isEndOfParagraph() && endCursor().paragraphCursor().isLast() && (num <= (int)endCursor().paragraphCursor().index())) { return; } if (myModel->kind() == ZLTextModel::TREE_MODEL) { if ((num >= 0) && (num < (int)myModel->paragraphsNumber())) { ZLTextTreeParagraph *tp = (ZLTextTreeParagraph*)(*myModel)[num]; if (myTreeStateIsFrozen) { int corrected = num; ZLTextTreeParagraph *visible = tp; for (ZLTextTreeParagraph *parent = tp->parent(); parent != 0; parent = parent->parent()) { if (!parent->isOpen()) { visible = parent; } } if (visible != tp) { for (--corrected; ((corrected > 0) && visible != (*myModel)[corrected]); --corrected) { } } if (end && (corrected != num)) { ++corrected; } num = corrected; } else { tp->openTree(); rebuildPaintInfo(true); } } } if (end) { if ((num > 0) && (num <= (int)myModel->paragraphsNumber())) { moveEndCursor(num); } } else { if ((num >= 0) && (num < (int)myModel->paragraphsNumber())) { moveStartCursor(num); } } }
void ZLTextView::scrollToStartOfText() { if (endCursor().isNull()) { return; } if (!startCursor().isNull() && startCursor().isStartOfParagraph() && startCursor().paragraphCursor().isFirst()) { return; } std::vector<size_t>::const_iterator i = nextBreakIterator(); gotoParagraph((i != myTextBreaks.begin()) ? *(i - 1) : 0, false); application().refreshWindow(); }
bool ZLTextArea::_getHyperlinkInfo(const ZLTextElementRectangle &rectangle, std::string &id, std::string &type, ZLTextKind& hyperlinkKind, int& x1, int&y1, int& x2, int& y2) const { if ((rectangle.Kind != ZLTextElement::WORD_ELEMENT) && (rectangle.Kind != ZLTextElement::IMAGE_ELEMENT)) { return false; } ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(rectangle.ParagraphIndex); cursor.moveToParagraphStart(); hyperlinkKind = REGULAR; for (int i = 0; i < rectangle.ElementIndex; ++i) { const ZLTextElement &element = cursor.element(); if (element.kind() == ZLTextElement::CONTROL_ELEMENT) { const ZLTextControlEntry &control = ((const ZLTextControlElement&)element).entry(); if (control.isHyperlink()) { hyperlinkKind = control.kind(); id = ((const ZLTextHyperlinkControlEntry&)control).label(); type = ((const ZLTextHyperlinkControlEntry&)control).hyperlinkType(); x1 = rectangle.XStart; y1 = rectangle.YStart; x2 = rectangle.XEnd; y2 = rectangle.YEnd; } else if (!control.isStart() && (control.kind() == hyperlinkKind)) { hyperlinkKind = REGULAR; } } cursor.nextWord(); } return hyperlinkKind != REGULAR; }
bool BookTextView::getHyperlinkId(const ZLTextElementArea &area, std::string &id, bool &isExternal) const { if ((area.Kind != ZLTextElement::WORD_ELEMENT) && (area.Kind != ZLTextElement::IMAGE_ELEMENT)) { return false; } ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(area.ParagraphNumber); cursor.moveToParagraphStart(); ZLTextKind hyperlinkKind = REGULAR; for (int i = 0; i < area.TextElementNumber; ++i) { const ZLTextElement &element = cursor.element(); if (element.kind() == ZLTextElement::CONTROL_ELEMENT) { const ZLTextControlEntry &control = ((const ZLTextControlElement&)element).entry(); if (control.isHyperlink()) { hyperlinkKind = control.kind(); id = ((const ZLTextHyperlinkControlEntry&)control).label(); } else if (!control.isStart() && (control.kind() == hyperlinkKind)) { hyperlinkKind = REGULAR; } } cursor.nextWord(); } isExternal = hyperlinkKind == EXTERNAL_HYPERLINK; return hyperlinkKind != REGULAR; }
bool BookTextView::getHyperlinkInfo(const ZLTextElementArea &area, std::string &id, std::string &type) const { if ((area.Kind != ZLTextElement::WORD_ELEMENT) && (area.Kind != ZLTextElement::IMAGE_ELEMENT)) { return false; } ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(area.ParagraphIndex); cursor.moveToParagraphStart(); ZLTextKind hyperlinkKind = REGULAR; for (int i = 0; i < area.ElementIndex; ++i) { const ZLTextElement &element = cursor.element(); if (element.kind() == ZLTextElement::CONTROL_ELEMENT) { const ZLTextControlEntry &control = ((const ZLTextControlElement&)element).entry(); if (control.isHyperlink()) { hyperlinkKind = control.kind(); id = ((const ZLTextHyperlinkControlEntry&)control).label(); type = ((const ZLTextHyperlinkControlEntry&)control).hyperlinkType(); } else if (!control.isStart() && (control.kind() == hyperlinkKind)) { hyperlinkKind = REGULAR; } } cursor.nextWord(); } return hyperlinkKind != REGULAR; }
void ZLTextView::onScrollbarMoved(Direction direction, size_t full, size_t from, size_t to) { if (direction != VERTICAL) { return; } mySelectionModel.deactivate(); if (myModel.isNull()) { return; } if (startCursor().isNull() || endCursor().isNull()) { return; } myTreeStateIsFrozen = true; if (from == 0) { scrollToStartOfText(); } else if (to == full) { scrollToEndOfText(); } else { gotoCharIndex(to); } preparePaintInfo(); myTreeStateIsFrozen = false; myDoUpdateScrollbar = false; application().refreshWindow(); }
std::string FBView::word(const ZLTextElementArea &area) const { std::string txt; if (area.Kind == ZLTextElement::WORD_ELEMENT) { ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(area.ParagraphIndex); cursor.moveTo(area.ElementIndex, 0); const ZLTextWord &word = (ZLTextWord&)cursor.element(); ZLUnicodeUtil::Ucs4String ucs4; ZLUnicodeUtil::utf8ToUcs4(ucs4, word.Data, word.Size); ZLUnicodeUtil::Ucs4String::iterator it = ucs4.begin(); while ((it != ucs4.end()) && !ZLUnicodeUtil::isLetter(*it)) { ++it; } if (it != ucs4.end()) { ucs4.erase(ucs4.begin(), it); it = ucs4.end() - 1; while (!ZLUnicodeUtil::isLetter(*it)) { --it; } ucs4.erase(it + 1, ucs4.end()); ZLUnicodeUtil::ucs4ToUtf8(txt, ucs4); } } return txt; }
void BookTextView::replaceCurrentPositionInStack() { const ZLTextWordCursor &cursor = startCursor(); if (!cursor.isNull()) { myPositionStack[myCurrentPointInStack] = cursorPosition(cursor); myStackChanged = true; } }
void ZLTextView::paint() { preparePaintInfo(); myTextElementMap.clear(); myTreeNodeMap.clear(); context().clear(ZLTextStyleCollection::instance().baseStyle().BackgroundColorOption.value()); if (empty()) { return; } std::vector<size_t> labels; labels.reserve(myLineInfos.size() + 1); labels.push_back(0); int y = topMargin(); for (std::vector<ZLTextLineInfoPtr>::const_iterator it = myLineInfos.begin(); it != myLineInfos.end(); ++it) { const ZLTextLineInfo &info = **it; prepareTextLine(info, y); y += info.Height + info.Descent + info.VSpaceAfter; labels.push_back(myTextElementMap.size()); } mySelectionModel.update(); y = topMargin(); int index = 0; for (std::vector<ZLTextLineInfoPtr>::const_iterator it = myLineInfos.begin(); it != myLineInfos.end(); ++it) { const ZLTextLineInfo &info = **it; drawTextLine(info, y, labels[index], labels[index + 1]); y += info.Height + info.Descent + info.VSpaceAfter; ++index; } fb::shared_ptr<ZLTextPositionIndicatorInfo> indicatorInfo = this->indicatorInfo(); if (!indicatorInfo.isNull() && (indicatorInfo->type() == ZLTextPositionIndicatorInfo::FB_INDICATOR)) { positionIndicator()->draw(); } if (myDoUpdateScrollbar && !indicatorInfo.isNull()) { myDoUpdateScrollbar = false; const size_t full = positionIndicator()->sizeOfTextBeforeParagraph(positionIndicator()->endTextIndex()); const size_t from = positionIndicator()->sizeOfTextBeforeCursor(startCursor()); const size_t to = positionIndicator()->sizeOfTextBeforeCursor(endCursor()); bool showScrollbar = (indicatorInfo->type() == ZLTextPositionIndicatorInfo::OS_SCROLLBAR) && (to - from < full); if (showScrollbar) { setScrollbarEnabled(VERTICAL, true); setScrollbarParameters(VERTICAL, full, from, to); } else { setScrollbarEnabled(VERTICAL, false); } } ZLTextParagraphCursorCache::cleanup(); }
std::vector<size_t>::const_iterator ZLTextView::nextBreakIterator() const { ZLTextWordCursor cursor = endCursor(); if (cursor.isNull()) { cursor = startCursor(); } if (cursor.isNull()) { return myTextBreaks.begin(); } return std::lower_bound(myTextBreaks.begin(), myTextBreaks.end(), cursor.paragraphCursor().index()); }
void ContentsView::saveState() { const WordCursor &cursor = startCursor(); if (!cursor.isNull()) { const std::string &group = fileName(); ZLIntegerOption(ZLOption::STATE_CATEGORY, group, PARAGRAPH_OPTION_NAME, 0).setValue(cursor.paragraphCursor().index()); ZLIntegerOption(ZLOption::STATE_CATEGORY, group, WORD_OPTION_NAME, 0).setValue(cursor.wordNumber()); ZLIntegerOption(ZLOption::STATE_CATEGORY, group, CHAR_OPTION_NAME, 0).setValue(cursor.charNumber()); } }
bool BookTextView::canUndoPageMove() { if (empty()) { return false; } if (myCurrentPointInStack == 0) { return false; } if ((myCurrentPointInStack == 1) && (myPositionStack.size() == 1)) { const ZLTextWordCursor &cursor = startCursor(); if (!cursor.isNull()) { return myPositionStack.back() != cursorPosition(cursor); } } return true; }
bool CollectionView::_onStylusPress(int x, int y) { fbreader().setHyperlinkCursor(false); const ZLTextElementArea *imageArea = elementByCoordinates(x, y); if ((imageArea != 0) && (imageArea->Kind == ZLTextElement::IMAGE_ELEMENT)) { ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(imageArea->ParagraphIndex); cursor.moveTo(imageArea->ElementIndex, 0); const ZLTextElement &element = cursor.element(); if (element.kind() != ZLTextElement::IMAGE_ELEMENT) { return false; } const ZLTextImageElement &imageElement = (ZLTextImageElement&)element; const std::string &id = imageElement.id(); if (id == CollectionModel::BookInfoImageId) { editBookInfo(collectionModel().bookByParagraphIndex(imageArea->ParagraphIndex)); return true; } else if (id == CollectionModel::RemoveBookImageId) { removeBook(collectionModel().bookByParagraphIndex(imageArea->ParagraphIndex)); return true; } else if (id == CollectionModel::RemoveTagImageId) { removeTag(collectionModel().tagByParagraphIndex(imageArea->ParagraphIndex)); return true; } else if (id == CollectionModel::TagInfoImageId) { editTagInfo(collectionModel().tagByParagraphIndex(imageArea->ParagraphIndex)); return true; } else { return false; } } int index = paragraphIndexByCoordinates(x, y); if (index == -1) { return false; } BookDescriptionPtr book = collectionModel().bookByParagraphIndex(index); if (!book.isNull()) { fbreader().openBook(book); fbreader().showBookTextView(); return true; } return false; }
std::string BookTextView::getFirstInternalHyperlinkId(int x0, int y0, int x1, int y1) { std::string id; std::string type; const ZLTextElementArea * area = elementByCoordinates(x0, y0); const ZLTextElementArea * stop_area = elementByCoordinates(x1, y1); if ( !area || ((area->Kind != ZLTextElement::WORD_ELEMENT) && (area->Kind != ZLTextElement::IMAGE_ELEMENT))) { return id; } ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(area->ParagraphIndex); int paragraphs = area->ParagraphIndex; int end_paragraphs = stop_area ? stop_area->ParagraphIndex : 0; do { cursor.moveToParagraphStart(); ZLTextKind hyperlinkKind = REGULAR; for ( ; !cursor.isEndOfParagraph();) { const ZLTextElement &element = cursor.element(); if (element.kind() == ZLTextElement::CONTROL_ELEMENT) { const ZLTextControlEntry &control = ((const ZLTextControlElement&)element).entry(); if (control.isHyperlink()) { hyperlinkKind = control.kind(); id = ((const ZLTextHyperlinkControlEntry&)control).label(); type = ((const ZLTextHyperlinkControlEntry&)control).hyperlinkType(); if (type == "internal") { return id; } } else if (!control.isStart() && (control.kind() == hyperlinkKind)) { hyperlinkKind = REGULAR; } } cursor.nextWord(); } } while( ++paragraphs <= end_paragraphs && cursor.nextParagraph()); return std::string(); }
bool BookTextView::pushCurrentPositionIntoStack(bool doPushSamePosition) { const ZLTextWordCursor &cursor = startCursor(); if (cursor.isNull()) { return false; } Position pos = cursorPosition(cursor); if (!doPushSamePosition && !myPositionStack.empty() && (myPositionStack.back() == pos)) { return false; } myPositionStack.push_back(pos); while (myPositionStack.size() > myMaxStackSize) { myPositionStack.erase(myPositionStack.begin()); if (myCurrentPointInStack > 0) { --myCurrentPointInStack; } } return true; }
void BookTextView::saveState() { const ZLTextWordCursor &cursor = startCursor(); if (!cursor.isNull()) { ZLIntegerOption(ZLCategoryKey::STATE, myFileName, PARAGRAPH_OPTION_NAME, 0).setValue(cursor.paragraphCursor().index()); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, WORD_OPTION_NAME, 0).setValue(cursor.wordNumber()); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, CHAR_OPTION_NAME, 0).setValue(cursor.charNumber()); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, BUFFER_SIZE, 0).setValue(myPositionStack.size()); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, POSITION_IN_BUFFER, 0).setValue(myCurrentPointInStack); for (unsigned int i = 0; i < myPositionStack.size(); ++i) { std::string bufferParagraph = BUFFER_PARAGRAPH_PREFIX; std::string bufferWord = BUFFER_WORD_PREFIX; ZLStringUtil::appendNumber(bufferParagraph, i); ZLStringUtil::appendNumber(bufferWord, i); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, bufferParagraph, -1).setValue(myPositionStack[i].first); ZLIntegerOption(ZLCategoryKey::STATE, myFileName, bufferWord, -1).setValue(myPositionStack[i].second); } } }
QRect PersistentState::selectionRect() { if (!_persistentCursor.isNull() && _persistentCursor.hasSelection()) { QTextCursor startCursor(editor()->document()); QTextCursor endCursor(editor()->document()); startCursor.setPosition(_persistentCursor.selectionStart()); endCursor.setPosition(_persistentCursor.selectionEnd()); QRect rect = editor()->cursorRect(startCursor).unite(editor()->cursorRect(endCursor)); if (rect.height() > editor()->fontMetrics().height()) { rect.setX(0); rect.setWidth(editor()->width()); } return rect; } return QRect(); }
void BookTextView::saveState() { const ZLTextWordCursor &cursor = startCursor(); if (myBook.isNull()) { return; } if (!cursor.isNull()) { ZLIntegerOption(ZLCategoryKey::STATE, LAST_STATE_GROUP, PARAGRAPH_OPTION_NAME, 0).setValue(cursor.paragraphCursor().index()); ZLIntegerOption(ZLCategoryKey::STATE, LAST_STATE_GROUP, WORD_OPTION_NAME, 0).setValue(cursor.elementIndex()); ZLIntegerOption(ZLCategoryKey::STATE, LAST_STATE_GROUP, CHAR_OPTION_NAME, 0).setValue(cursor.charIndex()); ZLIntegerOption(ZLCategoryKey::STATE, LAST_STATE_GROUP, POSITION_IN_BUFFER, 0).setValue(myCurrentPointInStack); ZLBooleanOption(ZLCategoryKey::STATE, LAST_STATE_GROUP, STATE_VALID, false).setValue(true); if (myStackChanged) { BooksDB::instance().saveBookStateStack(*myBook, myPositionStack); myStackChanged = false; } } }
void CollectionView::removeBook(BookDescriptionPtr book) { if (book.isNull()) { return; } if (book == mySelectedBook) { mySelectedBook = 0; } CollectionModel &cModel = collectionModel(); ZLResourceKey boxKey("removeBookBox"); const std::string message = ZLStringUtil::printf(ZLDialogManager::dialogMessage(boxKey), book->title()); if (ZLDialogManager::instance().questionBox(boxKey, message, ZLDialogManager::YES_BUTTON, ZLDialogManager::NO_BUTTON) == 0) { cModel.removeAllMarks(); BookList().removeFileName(book->fileName()); cModel.removeBook(book); if (cModel.paragraphsNumber() == 0) { setStartCursor(0); } else { size_t index = startCursor().paragraphCursor().index(); if (index >= cModel.paragraphsNumber()) { index = cModel.paragraphsNumber() - 1; } while (!((ZLTextTreeParagraph*)cModel[index])->parent()->isOpen()) { --index; } gotoParagraph(index); } rebuildPaintInfo(true); selectBook(mySelectedBook); application().refreshWindow(); myCollection.rebuild(false); } }
bool NetLibraryView::_onStylusPress(int x, int y) { fbreader().setHyperlinkCursor(false); if (model().isNull()) { return false; } const ZLTextElementArea *imageArea = elementByCoordinates(x, y); if ((imageArea == 0) || (imageArea->Kind != ZLTextElement::IMAGE_ELEMENT)) { return false; } ZLTextWordCursor cursor = startCursor(); cursor.moveToParagraph(imageArea->ParagraphIndex); cursor.moveTo(imageArea->ElementIndex, 0); const ZLTextElement &element = cursor.element(); if (element.kind() != ZLTextElement::IMAGE_ELEMENT) { return false; } const ZLTextImageElement &imageElement = (ZLTextImageElement&)element; const std::string &id = imageElement.id(); shared_ptr<NetworkBookInfo> book = myParagraphToBookMap[imageArea->ParagraphIndex]; if (book.isNull()) { return false; } if ((id == DownloadEpub) || (id == DownloadMobi)) { if (!ZLNetworkManager::instance().connect()) { NetworkOperationRunnable::showErrorMessage( ZLResource::resource("dialog") ["networkError"] ["couldntConnectToNetworkMessage"].value() ); return false; } NetworkBookInfo::URLType format = (id == DownloadEpub) ? NetworkBookInfo::BOOK_EPUB : NetworkBookInfo::BOOK_MOBIPOCKET; DownloadBookRunnable downloader(*book, format); downloader.executeWithUI(); if (downloader.hasErrors()) { downloader.showErrorMessage(); } else { BookDescriptionPtr description = BookDescription::getDescription(downloader.fileName()); WritableBookDescription wDescription(*description); wDescription.clearAuthor(); wDescription.addAuthor(book->Author.DisplayName, book->Author.SortKey); wDescription.title() = book->Title; wDescription.language() = book->Language; for (std::vector<std::string>::const_iterator it = book->Tags.begin(); it != book->Tags.end(); ++it) { wDescription.addTag(*it); } wDescription.saveInfo(); fbreader().openBook(description); fbreader().setMode(FBReader::BOOK_TEXT_MODE); rebuildModel(); } return true; } else if ((id == ReadLocalEpub) || (id == ReadLocalMobi)) { NetworkBookInfo::URLType format = (id == DownloadEpub) ? NetworkBookInfo::BOOK_EPUB : NetworkBookInfo::BOOK_MOBIPOCKET; fbreader().openFile(NetworkLinkCollection::instance().bookFileName(book->URLByType[format])); fbreader().setMode(FBReader::BOOK_TEXT_MODE); return true; } else if (id == OpenInBrowser) { std::string url = book->URLByType[NetworkBookInfo::LINK_HTTP]; shared_ptr<ProgramCollection> collection = fbreader().webBrowserCollection(); if (!url.empty() && !collection.isNull()) { shared_ptr<Program> program = collection->currentProgram(); if (!program.isNull()) { program->run("openLink", url); } } return true; } return false; }
bool ZLTextView::canFindPrevious() const { return !startCursor().isNull() && (myModel->previousMark(startCursor().position()).ParagraphIndex > -1); }
int game(WINDOW *left, WINDOW *bottom, WINDOW *textBox, WINDOW *middle, int sock, unsigned char id, unsigned char players, unsigned char bombs, unsigned char mines, struct data *pData, unsigned char field[16][16]){ unsigned char i, toReceive, toDiscard, chatLen, y, x; unsigned char msgLen, len, isChatting, turn, bombing; //255-64 char buffer[191]; //65 to fit a 0 at the end before printing char chatBuffer[65]; fd_set set; //Initialize what we have to isChatting = 0; toReceive = 0; toDiscard = 0; chatLen = 2; bombing = 0; msgLen = 0; turn = players + 1; chatBuffer[1] = 't'; y = 7; x = 7; //Windows' timeval #ifdef WIN32 struct timeval timeVal; timeVal.tv_sec = 0; timeVal.tv_usec = 250; #endif //Main loop while(mines){ //Build the set FD_ZERO(&set); #ifndef WIN32 FD_SET(fileno(stdin), &set); #endif FD_SET(sock, &set); //Wait for something to happen #ifdef WIN32 if(select(FD_SETSIZE, &set, NULL, NULL, &timeVal) < 0){ #else if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0){ #endif return 1; } //Read the keyboard first #ifndef WIN32 if(FD_ISSET(fileno(stdin), &set)){ #endif //Send the message if the handler says it's OK switch(keyboardHandler(middle, textBox, &chatLen, &isChatting, bombs, &bombing, &y, &x, id, turn, field, chatBuffer)){ //Chat case 1: //Set the size of the message and send it chatBuffer[0] = chatLen - 1; send(sock, chatBuffer, chatLen, 0); //Cleanup isChatting = 0; mvwhline(textBox, 0, 0, ' ', COLS-18); chatLen = 2; wrefresh(textBox); break; //Sweep case 2: buffer[0] = 2; buffer[1] = 's'; buffer[2] = (y << 4) | (x & 15); send(sock, buffer, 3, 0); break; //Bomb case 3: buffer[0] = 2; buffer[1] = 'b'; buffer[2] = (y << 4) | (x & 15); send(sock, buffer, 3, 0); bombs--; updateBombs(left, id, bombs); break; } #ifndef WIN32 } #endif //Receive data //Read the keyboard first if(FD_ISSET(sock, &set)){ //Receive if we can if(toReceive){ len = recv(sock, buffer + (msgLen - toReceive) * sizeof(char), toReceive, 0); if(len > 0){ toReceive -= len; if(toReceive){ continue; } } } //Discard if we have to else if(toDiscard){ len = recv(sock, buffer, toDiscard, 0); if(len > 0){ toDiscard -= len; continue; } } //Get the length of the new message else { len = recv(sock, buffer, 1, 0); //Get the length if we didn't lose connection if(len > 0){ //Adjust the values if((unsigned char) buffer[0] > 64){ msgLen = 64; toReceive = 64; toDiscard = (unsigned char) buffer[0] - 64; } else { msgLen = (unsigned char) buffer[0]; toReceive = (unsigned char) buffer[0]; } continue; } } //Lost connection if(len <= 0){ return 1; } //If we got down here, we're ready to parse //Chat if(buffer[0] == 't'){ addLine(bottom, msgLen, buffer, pData); } //New player else if(buffer[0] == 'c'){ //Copy name for(i = 2; i < msgLen; i++){ //Make it 7-bit if(buffer[i] < 32){ buffer[i] = '?'; } pData[(unsigned char) buffer[1]].name[i - 2] = buffer[i]; } //Null terminate pData[(unsigned char) buffer[1]].name[i-2] = 0; //Update the stats pData[(unsigned char) buffer[1]].stats = 1; //Add player to the left panel addPlayer(left, bottom, (unsigned char) buffer[1], pData); } //Player left else if(buffer[0] == 'l'){ //Update the stats pData[(unsigned char) buffer[1]].stats = 0; //Remove from the window removePlayer(left, bottom, (unsigned char) buffer[1], pData); } //New turn else if(buffer[0] == 'v'){ //Update the stats turn = buffer[1]; //Remove from the window tellTurn(bottom, turn, pData); //Draw the cursor if we're playing now if(turn == id){ bombing = 0; startCursor(middle, &y, &x, bombing, field); } //Hide cursor else { setValue(middle, y, x, field[y][x] & 15, (field[y][x] & 240) >> 4); } } //Player won else if(buffer[0] == 'w'){ return 0; } //Reveal else if(buffer[0] == 'r'){ for(i = 1; i < msgLen; i += 2){ //Update the matrix field[(buffer[i] & 240) >> 4][buffer[i] & 15] = buffer[i+1]; //Update the screen setValue(middle, (buffer[i] & 240) >> 4, buffer[i] & 15, buffer[i+1], turn); //Set the player and update the score if(buffer[i+1] > 8){ field[(buffer[i] & 240) >> 4][buffer[i] & 15] |= turn << 4; pData[turn].score++; updateMines(left, --mines); } } //Update the score on the screen updateScore(left, turn, pData); } }
void ZLTextView::findPrevious() { if (!startCursor().isNull()) { gotoMark(myModel->previousMark(startCursor().position())); } }