3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
15 #include "BufferView_pimpl.h"
17 #include "WordLangTuple.h"
19 #include "bufferlist.h"
22 #include "iterators.h"
24 #include "lyxcursor.h"
27 #include "undo_funcs.h"
30 #include "frontends/Alert.h"
31 #include "frontends/Dialogs.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/WorkArea.h"
34 #include "frontends/screen.h"
36 #include "insets/insetcommand.h" // ChangeRefs
37 #include "insets/inseterror.h"
39 #include "support/FileInfo.h"
40 #include "support/filetools.h"
41 #include "support/lyxfunctional.h" // equal_1st_in_pair
42 #include "support/types.h"
43 #include "support/lyxalgo.h" // lyx_count
47 extern BufferList bufferlist;
59 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
60 int width, int height)
61 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
67 BufferView::~BufferView()
74 Buffer * BufferView::buffer() const
76 return pimpl_->buffer_;
80 LyXScreen & BufferView::screen() const
82 return pimpl_->screen();
86 LyXView * BufferView::owner() const
88 return pimpl_->owner_;
92 Painter & BufferView::painter() const
94 return pimpl_->painter();
98 void BufferView::buffer(Buffer * b)
104 void BufferView::resize()
106 if (pimpl_->buffer_) {
107 pimpl_->resizeCurrentBuffer();
112 void BufferView::repaint()
118 bool BufferView::fitCursor()
120 return pimpl_->fitCursor();
124 void BufferView::update()
130 void BufferView::updateScrollbar()
132 pimpl_->updateScrollbar();
136 void BufferView::scrollDocView(int value)
138 pimpl_->scrollDocView(value);
142 void BufferView::redoCurrentBuffer()
144 pimpl_->redoCurrentBuffer();
148 bool BufferView::available() const
150 return pimpl_->available();
154 Change const BufferView::getCurrentChange()
156 return pimpl_->getCurrentChange();
160 void BufferView::beforeChange(LyXText * text)
162 pimpl_->beforeChange(text);
166 void BufferView::savePosition(unsigned int i)
168 pimpl_->savePosition(i);
172 void BufferView::restorePosition(unsigned int i)
174 pimpl_->restorePosition(i);
178 bool BufferView::isSavedPosition(unsigned int i)
180 return pimpl_->isSavedPosition(i);
184 void BufferView::update(LyXText * text, UpdateCodes f)
186 pimpl_->update(text, f);
190 void BufferView::switchKeyMap()
192 pimpl_->switchKeyMap();
196 void BufferView::insetUnlock()
198 pimpl_->insetUnlock();
202 int BufferView::workWidth() const
204 return pimpl_->workarea().workWidth();
208 void BufferView::showCursor()
210 pimpl_->showCursor();
214 void BufferView::hideCursor()
216 pimpl_->hideCursor();
220 void BufferView::toggleSelection(bool b)
222 pimpl_->toggleSelection(b);
226 void BufferView::toggleToggle()
228 pimpl_->toggleToggle();
232 void BufferView::center()
238 string const BufferView::getClipboard() const
240 return pimpl_->workarea().getClipboard();
244 void BufferView::stuffClipboard(string const & stuff) const
246 pimpl_->stuffClipboard(stuff);
250 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
251 BufferView::UpdateCodes uc2)
253 return static_cast<BufferView::UpdateCodes>
254 (static_cast<int>(uc1) | static_cast<int>(uc2));
258 bool BufferView::dispatch(FuncRequest const & ev)
260 return pimpl_->dispatch(ev);
264 void BufferView::scroll(int lines)
266 pimpl_->scroll(lines);
270 int BufferView::defaultHeight() const
272 return text->defaultHeight();
277 // Inserts a file into current document
278 bool BufferView::insertLyXFile(string const & filen)
280 // Copyright CHT Software Service GmbH
283 // Insert a LyXformat - file into current buffer
285 // Moved from lyx_cb.C (Lgb)
290 string const fname = MakeAbsPath(filen);
292 // check if file exist
293 FileInfo const fi(fname);
295 if (!fi.readable()) {
296 Alert::alert(_("Error!"),
297 _("Specified file is unreadable: "),
298 MakeDisplayPath(fname, 50));
304 ifstream ifs(fname.c_str());
306 Alert::err_alert(_("Error! Cannot open specified file:"),
307 MakeDisplayPath(fname, 50));
311 int const c = ifs.peek();
319 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
320 res = buffer()->readFile(lex, fname, text->cursor.par());
322 lyxerr[Debug::INFO] << "Will insert file without header"
324 res = buffer()->readLyXformat2(lex, text->cursor.par());
332 bool BufferView::removeAutoInsets()
334 // keep track of which pos and par the cursor was on
335 Paragraph * cursor_par = text->cursor.par();
336 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
337 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
338 pos_type cursor_pos = text->cursor.pos();
342 // Trap the deletion of the paragraph the cursor is in.
343 // Iterate until we find a paragraph that won't be immediately deleted.
344 // In reality this should mean we only execute the body of the while
345 // loop once at most. However for safety we iterate rather than just
346 // make this an if () conditional.
347 while ((cursor_par_prev || cursor_par_next)
348 && text->setCursor(this,
349 cursor_par_prev ? cursor_par_prev : cursor_par_next,
351 // We just removed cursor_par so have to fix the "cursor"
352 if (cursor_par_prev) {
356 cursor_par = cursor_par_prev;
357 cursor_pos = cursor_par->size();
361 cursor_par = cursor_par_next;
364 cursor_par_prev = cursor_par->previous();
365 cursor_par_next = cursor_par->next();
368 // Iterate through the paragraphs removing autoDelete insets as we go.
369 // If the paragraph ends up empty after all the autoDelete insets are
370 // removed that paragraph will be removed by the next setCursor() call.
371 ParIterator it = buffer()->par_iterator_begin();
372 ParIterator end = buffer()->par_iterator_end();
373 for (; it != end; ++it) {
374 Paragraph * par = *it;
375 Paragraph * par_prev = par ? par->previous() : 0;
376 bool removed = false;
378 if (text->setCursor(this, par, 0)
379 && cursor_par == par_prev) {
380 // The previous setCursor line was deleted and that
381 // was the cursor_par line. This can only happen if an
382 // error box was the sole item on cursor_par.
383 // It is possible for cursor_par_prev to be stray if
384 // the line it pointed to only had a error box on it
385 // so we have to set it to a known correct value.
386 // This is often the same value it already had.
387 cursor_par_prev = par->previous();
388 if (cursor_par_prev) {
389 // '|' = par, '.' = cursor_par, 'E' = error box
390 // First step below may occur before while{}
392 // E -> .E -> |.E -> . -> |b
395 cursor_par = cursor_par_prev;
396 cursor_pos = cursor_par_prev->size();
397 cursor_par_prev = cursor_par->previous();
398 // cursor_par_next remains the same
399 } else if (cursor_par_next) {
400 // First step below may occur before while{}
402 // E -> |.E -> |. -> . -> .|a
404 cursor_par = cursor_par_next;
406 // cursor_par_prev remains unset
407 cursor_par_next = cursor_par->next();
409 // I can't find a way to trigger this
410 // so it should be unreachable code
411 // unless the buffer is corrupted.
412 lyxerr << "BufferView::removeAutoInsets() is bad\n";
416 InsetList::iterator pit = par->insetlist.begin();
417 InsetList::iterator pend = par->insetlist.end();
419 while (pit != pend) {
420 if (pit.getInset()->autoDelete()) {
422 pos_type const pos = pit.getPos();
425 // We just invalidated par's inset iterators so
426 // we get the next valid iterator position
427 pit = par->insetlist.insetIterator(pos);
428 // and ensure we have a valid end iterator.
429 pend = par->insetlist.end();
431 if (cursor_par == par) {
432 // update the saved cursor position
433 if (cursor_pos > pos)
442 text->redoParagraph(this);
446 // It is possible that the last line is empty if it was cursor_par
447 // and/or only had an error inset on it. So we set the cursor to the
448 // start of the doc to force its removal and ensure a valid saved cursor
449 if (text->setCursor(this, text->ownerParagraph(), 0)
450 && 0 == cursor_par_next) {
451 cursor_par = cursor_par_prev;
452 cursor_pos = cursor_par->size();
453 } else if (cursor_pos > cursor_par->size()) {
454 // Some C-Enter lines were removed by the setCursor call which
455 // then invalidated cursor_pos. It could still be "wrong" because
456 // the cursor may appear to have jumped but since we collapsed
457 // some C-Enter lines this should be a reasonable compromise.
458 cursor_pos = cursor_par->size();
461 // restore the original cursor in its corrected location.
462 text->setCursorIntern(this, cursor_par, cursor_pos);
468 void BufferView::insertErrors(TeXErrors & terr)
470 // Save the cursor position
471 LyXCursor cursor = text->cursor;
473 TeXErrors::Errors::const_iterator cit = terr.begin();
474 TeXErrors::Errors::const_iterator end = terr.end();
475 for (; cit != end; ++cit) {
476 string const desctext(cit->error_desc);
477 string const errortext(cit->error_text);
478 string const msgtxt = desctext + '\n' + errortext;
479 int const errorrow = cit->error_in_line;
481 // Insert error string for row number
485 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
486 buffer()->texrow.increasePos(tmpid, tmppos);
489 Paragraph * texrowpar = 0;
492 texrowpar = text->ownerParagraph();
495 texrowpar = buffer()->getParFromID(tmpid);
502 InsetError * new_inset = new InsetError(msgtxt);
503 text->setCursorIntern(this, texrowpar, tmppos);
504 text->insertInset(this, new_inset);
505 text->fullRebreak(this);
508 // Restore the cursor position
509 text->setCursorIntern(this, cursor.par(), cursor.pos());
513 void BufferView::setCursorFromRow(int row)
518 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
520 Paragraph * texrowpar;
523 texrowpar = text->ownerParagraph();
526 texrowpar = buffer()->getParFromID(tmpid);
528 text->setCursor(this, texrowpar, tmppos);
532 bool BufferView::insertInset(Inset * inset, string const & lout)
534 return pimpl_->insertInset(inset, lout);
538 // This is also a buffer property (ale)
539 // Not so sure about that. a goto Label function can not be buffer local, just
540 // think how this will work in a multiwindow/buffer environment, all the
541 // cursors in all the views showing this buffer will move. (Lgb)
542 // OK, then no cursor action should be allowed in buffer. (ale)
543 bool BufferView::gotoLabel(string const & label)
545 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
546 it != buffer()->inset_iterator_end(); ++it) {
547 vector<string> labels = it->getLabelList();
548 if (find(labels.begin(),labels.end(),label)
551 text->setCursor(this, it.getPar(), it.getPos());
552 text->selection.cursor = text->cursor;
553 update(text, BufferView::SELECT|BufferView::FITCUR);
561 void BufferView::undo()
566 owner()->message(_("Undo"));
569 update(text, BufferView::SELECT|BufferView::FITCUR);
571 owner()->message(_("No further undo information"));
573 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
578 void BufferView::redo()
583 owner()->message(_("Redo"));
586 update(text, BufferView::SELECT|BufferView::FITCUR);
588 owner()->message(_("No further redo information"));
590 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
595 void BufferView::copyEnvironment()
598 text->copyEnvironmentType();
599 owner()->message(_("Paragraph environment type copied"));
604 void BufferView::pasteEnvironment()
607 text->pasteEnvironmentType(this);
608 owner()->message(_("Paragraph environment type set"));
609 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
614 // these functions are for the spellchecker
615 WordLangTuple const BufferView::nextWord(float & value)
619 return WordLangTuple();
622 return text->selectNextWordToSpellcheck(this, value);
626 void BufferView::selectLastWord()
631 LyXCursor cur = text->selection.cursor;
634 text->selection.cursor = cur;
635 text->selectSelectedWord(this);
636 toggleSelection(false);
637 update(text, BufferView::SELECT|BufferView::FITCUR);
641 void BufferView::endOfSpellCheck()
643 if (!available()) return;
647 text->selectSelectedWord(this);
648 text->clearSelection();
649 update(text, BufferView::SELECT|BufferView::FITCUR);
653 void BufferView::replaceWord(string const & replacestring)
658 LyXText * tt = getLyXText();
660 update(tt, BufferView::SELECT|BufferView::FITCUR);
662 // clear the selection (if there is any)
663 toggleSelection(false);
664 update(tt, BufferView::SELECT|BufferView::FITCUR);
666 // clear the selection (if there is any)
667 toggleSelection(false);
668 tt->replaceSelectionWithString(this, replacestring);
670 tt->setSelectionRange(this, replacestring.length());
672 // Go back so that replacement string is also spellchecked
673 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
674 tt->cursorLeft(this);
676 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
678 // End of spellchecker stuff
681 bool BufferView::lockInset(UpdatableInset * inset)
685 // don't relock if we're already locked
686 if (theLockingInset() == inset)
688 if (!theLockingInset()) {
689 // first check if it's the inset under the cursor we want lock
690 // should be most of the time
691 char const c = text->cursor.par()->getChar(text->cursor.pos());
692 if (c == Paragraph::META_INSET) {
693 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
695 theLockingInset(inset);
699 // Then do a deep look of the inset and lock the right one
700 int const id = inset->id();
701 ParagraphList::iterator pit = buffer()->paragraphs.begin();
702 ParagraphList::iterator pend = buffer()->paragraphs.end();
703 for (; pit != pend; ++pit) {
704 InsetList::iterator it = pit->insetlist.begin();
705 InsetList::iterator end = pit->insetlist.end();
706 for (; it != end; ++it) {
707 if (it.getInset() == inset) {
708 text->setCursorIntern(this, &*pit, it.getPos());
709 theLockingInset(inset);
712 if (it.getInset()->getInsetFromID(id)) {
713 text->setCursorIntern(this, &*pit, it.getPos());
714 it.getInset()->edit(this);
715 return theLockingInset()->lockInsetInInset(this, inset);
721 return theLockingInset()->lockInsetInInset(this, inset);
725 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
727 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
728 LyXCursor cursor = text->cursor;
729 Inset * locking_inset = theLockingInset()->getLockingInset();
731 if ((cursor.pos() - 1 >= 0) &&
732 cursor.par()->isInset(cursor.pos() - 1) &&
733 (cursor.par()->getInset(cursor.pos() - 1) ==
735 text->setCursor(this, cursor,
736 cursor.par(), cursor.pos() - 1);
737 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
738 LyXText * txt = getLyXText();
739 if (locking_inset->isTextInset() &&
740 locking_inset->lyxCode() != Inset::ERT_CODE &&
741 (txt->real_current_font.language() !=
742 buffer()->params.language
743 || txt->real_current_font.isVisibleRightToLeft()
744 != buffer()->params.language->RightToLeft()))
745 shape = (txt->real_current_font.isVisibleRightToLeft())
746 ? LyXScreen::REVERSED_L_SHAPE
747 : LyXScreen::L_SHAPE;
748 y += cursor.iy() + theLockingInset()->insetInInsetY();
749 screen().showManualCursor(text, x, y, asc, desc,
755 void BufferView::hideLockedInsetCursor()
757 if (theLockingInset() && available()) {
758 screen().hideCursor();
763 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
765 if (theLockingInset() && available()) {
766 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
767 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
776 int BufferView::unlockInset(UpdatableInset * inset)
780 if (inset && theLockingInset() == inset) {
781 inset->insetUnlock(this);
783 // make sure we update the combo !
784 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
785 // Tell the paragraph dialog that we changed paragraph
786 owner()->getDialogs().updateParagraph();
789 } else if (inset && theLockingInset() &&
790 theLockingInset()->unlockInsetInInset(this, inset)) {
791 // Tell the paragraph dialog that we changed paragraph
792 owner()->getDialogs().updateParagraph();
793 // owner inset has updated the layout combo
797 return bufferlist.unlockInset(inset);
801 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
803 if (!theLockingInset())
804 return; // shouldn't happen
805 if (kind == Undo::EDIT) // in this case insets would not be stored!
809 text->cursor.par()->next());
813 void BufferView::updateInset(Inset * inset, bool mark_dirty)
815 pimpl_->updateInset(inset, mark_dirty);
819 bool BufferView::ChangeInsets(Inset::Code code,
820 string const & from, string const & to)
822 bool need_update = false;
823 LyXCursor cursor = text->cursor;
824 LyXCursor tmpcursor = cursor;
825 cursor.par(tmpcursor.par());
826 cursor.pos(tmpcursor.pos());
828 ParIterator end = buffer()->par_iterator_end();
829 for (ParIterator it = buffer()->par_iterator_begin();
831 Paragraph * par = *it;
832 bool changed_inset = false;
833 for (InsetList::iterator it2 = par->insetlist.begin();
834 it2 != par->insetlist.end(); ++it2) {
835 if (it2.getInset()->lyxCode() == code) {
836 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
837 if (inset->getContents() == from) {
838 inset->setContents(to);
839 changed_inset = true;
848 // The test it.size()==1 was needed to prevent crashes.
849 // How to set the cursor corretly when it.size()>1 ??
850 if (it.size() == 1) {
851 text->setCursorIntern(this, par, 0);
852 text->redoParagraphs(this, text->cursor,
853 text->cursor.par()->next());
854 text->fullRebreak(this);
858 text->setCursorIntern(this, cursor.par(), cursor.pos());
863 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
865 // Check if the label 'from' appears more than once
866 vector<string> labels = buffer()->getLabelList();
868 if (lyx::count(labels.begin(), labels.end(), from) > 1)
871 return ChangeInsets(Inset::REF_CODE, from, to);
875 bool BufferView::ChangeCitationsIfUnique(string const & from,
878 typedef pair<string, string> StringPair;
880 vector<StringPair> keys = buffer()->getBibkeyList();
881 if (count_if(keys.begin(), keys.end(),
882 lyx::equal_1st_in_pair<StringPair>(from))
886 return ChangeInsets(Inset::CITE_CODE, from, to);
890 UpdatableInset * BufferView::theLockingInset() const
892 // If NULL is not allowed we should put an Assert here. (Lgb)
894 return text->the_locking_inset;
899 void BufferView::theLockingInset(UpdatableInset * inset)
901 text->the_locking_inset = inset;
905 LyXText * BufferView::getLyXText() const
907 if (theLockingInset()) {
908 LyXText * txt = theLockingInset()->getLyXText(this, true);
916 LyXText * BufferView::getParentText(Inset * inset) const
918 if (inset->owner()) {
919 LyXText * txt = inset->getLyXText(this);
920 inset = inset->owner();
921 while (inset && inset->getLyXText(this) == txt)
922 inset = inset->owner();
924 return inset->getLyXText(this);
930 Language const * BufferView::getParentLanguage(Inset * inset) const
932 LyXText * text = getParentText(inset);
933 return text->cursor.par()->getFontSettings(buffer()->params,
934 text->cursor.pos()).language();
938 Encoding const * BufferView::getEncoding() const
940 LyXText * t = getLyXText();
944 LyXCursor const & c= t->cursor;
945 LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
946 return font.language()->encoding();
950 void BufferView::haveSelection(bool sel)
952 pimpl_->workarea().haveSelection(sel);
956 int BufferView::workHeight() const
958 return pimpl_->workarea().workHeight();