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"
38 #include "insets/updatableinset.h"
40 #include "support/FileInfo.h"
41 #include "support/filetools.h"
42 #include "support/lyxfunctional.h" // equal_1st_in_pair
43 #include "support/types.h"
44 #include "support/lyxalgo.h" // lyx_count
48 extern BufferList bufferlist;
60 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
61 int width, int height)
62 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
68 BufferView::~BufferView()
75 Buffer * BufferView::buffer() const
77 return pimpl_->buffer_;
81 LyXScreen & BufferView::screen() const
83 return pimpl_->screen();
87 LyXView * BufferView::owner() const
89 return pimpl_->owner_;
93 Painter & BufferView::painter() const
95 return pimpl_->painter();
99 void BufferView::buffer(Buffer * b)
105 void BufferView::resize()
107 if (pimpl_->buffer_) {
108 pimpl_->resizeCurrentBuffer();
113 void BufferView::repaint()
119 bool BufferView::fitCursor()
121 return pimpl_->fitCursor();
125 void BufferView::update()
131 void BufferView::updateScrollbar()
133 pimpl_->updateScrollbar();
137 void BufferView::scrollDocView(int value)
139 pimpl_->scrollDocView(value);
143 void BufferView::redoCurrentBuffer()
145 pimpl_->redoCurrentBuffer();
149 bool BufferView::available() const
151 return pimpl_->available();
155 Change const BufferView::getCurrentChange()
157 return pimpl_->getCurrentChange();
161 void BufferView::beforeChange(LyXText * text)
163 pimpl_->beforeChange(text);
167 void BufferView::savePosition(unsigned int i)
169 pimpl_->savePosition(i);
173 void BufferView::restorePosition(unsigned int i)
175 pimpl_->restorePosition(i);
179 bool BufferView::isSavedPosition(unsigned int i)
181 return pimpl_->isSavedPosition(i);
185 void BufferView::update(LyXText * text, UpdateCodes f)
187 pimpl_->update(text, f);
191 void BufferView::switchKeyMap()
193 pimpl_->switchKeyMap();
197 void BufferView::insetUnlock()
199 pimpl_->insetUnlock();
203 int BufferView::workWidth() const
205 return pimpl_->workarea().workWidth();
209 void BufferView::showCursor()
211 pimpl_->showCursor();
215 void BufferView::hideCursor()
217 pimpl_->hideCursor();
221 void BufferView::toggleSelection(bool b)
223 pimpl_->toggleSelection(b);
227 void BufferView::toggleToggle()
229 pimpl_->toggleToggle();
233 void BufferView::center()
239 string const BufferView::getClipboard() const
241 return pimpl_->workarea().getClipboard();
245 void BufferView::stuffClipboard(string const & stuff) const
247 pimpl_->stuffClipboard(stuff);
251 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
252 BufferView::UpdateCodes uc2)
254 return static_cast<BufferView::UpdateCodes>
255 (static_cast<int>(uc1) | static_cast<int>(uc2));
259 bool BufferView::dispatch(FuncRequest const & ev)
261 return pimpl_->dispatch(ev);
265 void BufferView::scroll(int lines)
267 pimpl_->scroll(lines);
271 // Inserts a file into current document
272 bool BufferView::insertLyXFile(string const & filen)
274 // Copyright CHT Software Service GmbH
277 // Insert a LyXformat - file into current buffer
279 // Moved from lyx_cb.C (Lgb)
284 string const fname = MakeAbsPath(filen);
286 // check if file exist
287 FileInfo const fi(fname);
289 if (!fi.readable()) {
290 Alert::alert(_("Error!"),
291 _("Specified file is unreadable: "),
292 MakeDisplayPath(fname, 50));
298 ifstream ifs(fname.c_str());
300 Alert::err_alert(_("Error! Cannot open specified file:"),
301 MakeDisplayPath(fname, 50));
305 int const c = ifs.peek();
313 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
314 res = buffer()->readFile(lex, fname, text->cursor.par());
316 lyxerr[Debug::INFO] << "Will insert file without header"
318 res = buffer()->readLyXformat2(lex, text->cursor.par());
326 bool BufferView::removeAutoInsets()
328 // keep track of which pos and par the cursor was on
329 Paragraph * cursor_par = text->cursor.par();
330 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
331 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
332 pos_type cursor_pos = text->cursor.pos();
336 // Trap the deletion of the paragraph the cursor is in.
337 // Iterate until we find a paragraph that won't be immediately deleted.
338 // In reality this should mean we only execute the body of the while
339 // loop once at most. However for safety we iterate rather than just
340 // make this an if () conditional.
341 while ((cursor_par_prev || cursor_par_next)
342 && text->setCursor(this,
343 cursor_par_prev ? cursor_par_prev : cursor_par_next,
345 // We just removed cursor_par so have to fix the "cursor"
346 if (cursor_par_prev) {
350 cursor_par = cursor_par_prev;
351 cursor_pos = cursor_par->size();
355 cursor_par = cursor_par_next;
358 cursor_par_prev = cursor_par->previous();
359 cursor_par_next = cursor_par->next();
362 // Iterate through the paragraphs removing autoDelete insets as we go.
363 // If the paragraph ends up empty after all the autoDelete insets are
364 // removed that paragraph will be removed by the next setCursor() call.
365 ParIterator it = buffer()->par_iterator_begin();
366 ParIterator end = buffer()->par_iterator_end();
367 for (; it != end; ++it) {
368 Paragraph * par = *it;
369 Paragraph * par_prev = par ? par->previous() : 0;
370 bool removed = false;
372 if (text->setCursor(this, par, 0)
373 && cursor_par == par_prev) {
374 // The previous setCursor line was deleted and that
375 // was the cursor_par line. This can only happen if an
376 // error box was the sole item on cursor_par.
377 // It is possible for cursor_par_prev to be stray if
378 // the line it pointed to only had a error box on it
379 // so we have to set it to a known correct value.
380 // This is often the same value it already had.
381 cursor_par_prev = par->previous();
382 if (cursor_par_prev) {
383 // '|' = par, '.' = cursor_par, 'E' = error box
384 // First step below may occur before while{}
386 // E -> .E -> |.E -> . -> |b
389 cursor_par = cursor_par_prev;
390 cursor_pos = cursor_par_prev->size();
391 cursor_par_prev = cursor_par->previous();
392 // cursor_par_next remains the same
393 } else if (cursor_par_next) {
394 // First step below may occur before while{}
396 // E -> |.E -> |. -> . -> .|a
398 cursor_par = cursor_par_next;
400 // cursor_par_prev remains unset
401 cursor_par_next = cursor_par->next();
403 // I can't find a way to trigger this
404 // so it should be unreachable code
405 // unless the buffer is corrupted.
406 lyxerr << "BufferView::removeAutoInsets() is bad\n";
410 InsetList::iterator pit = par->insetlist.begin();
411 InsetList::iterator pend = par->insetlist.end();
413 while (pit != pend) {
414 if (pit.getInset()->autoDelete()) {
416 pos_type const pos = pit.getPos();
419 // We just invalidated par's inset iterators so
420 // we get the next valid iterator position
421 pit = par->insetlist.insetIterator(pos);
422 // and ensure we have a valid end iterator.
423 pend = par->insetlist.end();
425 if (cursor_par == par) {
426 // update the saved cursor position
427 if (cursor_pos > pos)
436 text->redoParagraph(this);
440 // It is possible that the last line is empty if it was cursor_par
441 // and/or only had an error inset on it. So we set the cursor to the
442 // start of the doc to force its removal and ensure a valid saved cursor
443 if (text->setCursor(this, text->ownerParagraph(), 0)
444 && 0 == cursor_par_next) {
445 cursor_par = cursor_par_prev;
446 cursor_pos = cursor_par->size();
447 } else if (cursor_pos > cursor_par->size()) {
448 // Some C-Enter lines were removed by the setCursor call which
449 // then invalidated cursor_pos. It could still be "wrong" because
450 // the cursor may appear to have jumped but since we collapsed
451 // some C-Enter lines this should be a reasonable compromise.
452 cursor_pos = cursor_par->size();
455 // restore the original cursor in its corrected location.
456 text->setCursorIntern(this, cursor_par, cursor_pos);
462 void BufferView::insertErrors(TeXErrors & terr)
464 // Save the cursor position
465 LyXCursor cursor = text->cursor;
467 TeXErrors::Errors::const_iterator cit = terr.begin();
468 TeXErrors::Errors::const_iterator end = terr.end();
469 for (; cit != end; ++cit) {
470 string const desctext(cit->error_desc);
471 string const errortext(cit->error_text);
472 string const msgtxt = desctext + '\n' + errortext;
473 int const errorrow = cit->error_in_line;
475 // Insert error string for row number
479 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
480 buffer()->texrow.increasePos(tmpid, tmppos);
483 Paragraph * texrowpar = 0;
486 texrowpar = text->ownerParagraph();
489 texrowpar = buffer()->getParFromID(tmpid);
496 InsetError * new_inset = new InsetError(msgtxt);
497 text->setCursorIntern(this, texrowpar, tmppos);
498 text->insertInset(this, new_inset);
499 text->fullRebreak(this);
502 // Restore the cursor position
503 text->setCursorIntern(this, cursor.par(), cursor.pos());
507 void BufferView::setCursorFromRow(int row)
512 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
514 Paragraph * texrowpar;
517 texrowpar = text->ownerParagraph();
520 texrowpar = buffer()->getParFromID(tmpid);
522 text->setCursor(this, texrowpar, tmppos);
526 bool BufferView::insertInset(Inset * inset, string const & lout)
528 return pimpl_->insertInset(inset, lout);
532 // This is also a buffer property (ale)
533 // Not so sure about that. a goto Label function can not be buffer local, just
534 // think how this will work in a multiwindow/buffer environment, all the
535 // cursors in all the views showing this buffer will move. (Lgb)
536 // OK, then no cursor action should be allowed in buffer. (ale)
537 bool BufferView::gotoLabel(string const & label)
539 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
540 it != buffer()->inset_iterator_end(); ++it) {
541 vector<string> labels = it->getLabelList();
542 if (find(labels.begin(),labels.end(),label)
545 text->setCursor(this, it.getPar(), it.getPos());
546 text->selection.cursor = text->cursor;
547 update(text, BufferView::SELECT|BufferView::FITCUR);
555 void BufferView::undo()
560 owner()->message(_("Undo"));
563 update(text, BufferView::SELECT|BufferView::FITCUR);
565 owner()->message(_("No further undo information"));
567 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
572 void BufferView::redo()
577 owner()->message(_("Redo"));
580 update(text, BufferView::SELECT|BufferView::FITCUR);
582 owner()->message(_("No further redo information"));
584 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
589 void BufferView::copyEnvironment()
592 text->copyEnvironmentType();
593 owner()->message(_("Paragraph environment type copied"));
598 void BufferView::pasteEnvironment()
601 text->pasteEnvironmentType(this);
602 owner()->message(_("Paragraph environment type set"));
603 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
608 // these functions are for the spellchecker
609 WordLangTuple const BufferView::nextWord(float & value)
613 return WordLangTuple();
616 return text->selectNextWordToSpellcheck(this, value);
620 void BufferView::selectLastWord()
625 LyXCursor cur = text->selection.cursor;
628 text->selection.cursor = cur;
629 text->selectSelectedWord(this);
630 toggleSelection(false);
631 update(text, BufferView::SELECT|BufferView::FITCUR);
635 void BufferView::endOfSpellCheck()
637 if (!available()) return;
641 text->selectSelectedWord(this);
642 text->clearSelection();
643 update(text, BufferView::SELECT|BufferView::FITCUR);
647 void BufferView::replaceWord(string const & replacestring)
652 LyXText * tt = getLyXText();
654 update(tt, BufferView::SELECT|BufferView::FITCUR);
656 // clear the selection (if there is any)
657 toggleSelection(false);
658 update(tt, BufferView::SELECT|BufferView::FITCUR);
660 // clear the selection (if there is any)
661 toggleSelection(false);
662 tt->replaceSelectionWithString(this, replacestring);
664 tt->setSelectionRange(this, replacestring.length());
666 // Go back so that replacement string is also spellchecked
667 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
668 tt->cursorLeft(this);
670 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
672 // End of spellchecker stuff
675 bool BufferView::lockInset(UpdatableInset * inset)
679 // don't relock if we're already locked
680 if (theLockingInset() == inset)
682 if (!theLockingInset()) {
683 // first check if it's the inset under the cursor we want lock
684 // should be most of the time
685 char const c = text->cursor.par()->getChar(text->cursor.pos());
686 if (c == Paragraph::META_INSET) {
687 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
689 theLockingInset(inset);
693 // Then do a deep look of the inset and lock the right one
694 int const id = inset->id();
695 ParagraphList::iterator pit = buffer()->paragraphs.begin();
696 ParagraphList::iterator pend = buffer()->paragraphs.end();
697 for (; pit != pend; ++pit) {
698 InsetList::iterator it = pit->insetlist.begin();
699 InsetList::iterator end = pit->insetlist.end();
700 for (; it != end; ++it) {
701 if (it.getInset() == inset) {
702 text->setCursorIntern(this, &*pit, it.getPos());
703 theLockingInset(inset);
706 if (it.getInset()->getInsetFromID(id)) {
707 text->setCursorIntern(this, &*pit, it.getPos());
708 it.getInset()->edit(this);
709 return theLockingInset()->lockInsetInInset(this, inset);
715 return theLockingInset()->lockInsetInInset(this, inset);
719 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
721 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
722 LyXCursor cursor = text->cursor;
723 Inset * locking_inset = theLockingInset()->getLockingInset();
725 if ((cursor.pos() - 1 >= 0) &&
726 cursor.par()->isInset(cursor.pos() - 1) &&
727 (cursor.par()->getInset(cursor.pos() - 1) ==
729 text->setCursor(this, cursor,
730 cursor.par(), cursor.pos() - 1);
731 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
732 LyXText * txt = getLyXText();
733 if (locking_inset->isTextInset() &&
734 locking_inset->lyxCode() != Inset::ERT_CODE &&
735 (txt->real_current_font.language() !=
736 buffer()->params.language
737 || txt->real_current_font.isVisibleRightToLeft()
738 != buffer()->params.language->RightToLeft()))
739 shape = (txt->real_current_font.isVisibleRightToLeft())
740 ? LyXScreen::REVERSED_L_SHAPE
741 : LyXScreen::L_SHAPE;
742 y += cursor.iy() + theLockingInset()->insetInInsetY();
743 screen().showManualCursor(text, x, y, asc, desc,
749 void BufferView::hideLockedInsetCursor()
751 if (theLockingInset() && available()) {
752 screen().hideCursor();
757 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
759 if (theLockingInset() && available()) {
760 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
761 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
770 int BufferView::unlockInset(UpdatableInset * inset)
774 if (inset && theLockingInset() == inset) {
775 inset->insetUnlock(this);
777 // make sure we update the combo !
778 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
779 // Tell the paragraph dialog that we changed paragraph
780 owner()->getDialogs().updateParagraph();
783 } else if (inset && theLockingInset() &&
784 theLockingInset()->unlockInsetInInset(this, inset)) {
785 // Tell the paragraph dialog that we changed paragraph
786 owner()->getDialogs().updateParagraph();
787 // owner inset has updated the layout combo
795 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
797 if (!theLockingInset())
798 return; // shouldn't happen
799 if (kind == Undo::EDIT) // in this case insets would not be stored!
803 text->cursor.par()->next());
807 void BufferView::updateInset(Inset * inset, bool mark_dirty)
809 pimpl_->updateInset(inset, mark_dirty);
813 bool BufferView::ChangeInsets(Inset::Code code,
814 string const & from, string const & to)
816 bool need_update = false;
817 LyXCursor cursor = text->cursor;
818 LyXCursor tmpcursor = cursor;
819 cursor.par(tmpcursor.par());
820 cursor.pos(tmpcursor.pos());
822 ParIterator end = buffer()->par_iterator_end();
823 for (ParIterator it = buffer()->par_iterator_begin();
825 Paragraph * par = *it;
826 bool changed_inset = false;
827 for (InsetList::iterator it2 = par->insetlist.begin();
828 it2 != par->insetlist.end(); ++it2) {
829 if (it2.getInset()->lyxCode() == code) {
830 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
831 if (inset->getContents() == from) {
832 inset->setContents(to);
833 changed_inset = true;
842 // The test it.size()==1 was needed to prevent crashes.
843 // How to set the cursor corretly when it.size()>1 ??
844 if (it.size() == 1) {
845 text->setCursorIntern(this, par, 0);
846 text->redoParagraphs(this, text->cursor,
847 text->cursor.par()->next());
848 text->fullRebreak(this);
852 text->setCursorIntern(this, cursor.par(), cursor.pos());
857 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
859 // Check if the label 'from' appears more than once
860 vector<string> labels = buffer()->getLabelList();
862 if (lyx::count(labels.begin(), labels.end(), from) > 1)
865 return ChangeInsets(Inset::REF_CODE, from, to);
869 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
871 typedef pair<string, string> StringPair;
873 vector<StringPair> keys;
874 buffer()->fillWithBibKeys(keys);
875 if (count_if(keys.begin(), keys.end(),
876 lyx::equal_1st_in_pair<StringPair>(from))
880 return ChangeInsets(Inset::CITE_CODE, from, to);
884 UpdatableInset * BufferView::theLockingInset() const
886 // If NULL is not allowed we should put an Assert here. (Lgb)
888 return text->the_locking_inset;
893 void BufferView::theLockingInset(UpdatableInset * inset)
895 text->the_locking_inset = inset;
899 LyXText * BufferView::getLyXText() const
901 if (theLockingInset()) {
902 LyXText * txt = theLockingInset()->getLyXText(this, true);
910 LyXText * BufferView::getParentText(Inset * inset) const
912 if (inset->owner()) {
913 LyXText * txt = inset->getLyXText(this);
914 inset = inset->owner();
915 while (inset && inset->getLyXText(this) == txt)
916 inset = inset->owner();
918 return inset->getLyXText(this);
924 Language const * BufferView::getParentLanguage(Inset * inset) const
926 LyXText * text = getParentText(inset);
927 return text->cursor.par()->getFontSettings(buffer()->params,
928 text->cursor.pos()).language();
932 Encoding const * BufferView::getEncoding() const
934 LyXText * t = getLyXText();
938 LyXCursor const & c= t->cursor;
939 LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
940 return font.language()->encoding();
944 void BufferView::haveSelection(bool sel)
946 pimpl_->workarea().haveSelection(sel);
950 int BufferView::workHeight() const
952 return pimpl_->workarea().workHeight();