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 // Inserts a file into current document
271 bool BufferView::insertLyXFile(string const & filen)
273 // Copyright CHT Software Service GmbH
276 // Insert a LyXformat - file into current buffer
278 // Moved from lyx_cb.C (Lgb)
283 string const fname = MakeAbsPath(filen);
285 // check if file exist
286 FileInfo const fi(fname);
288 if (!fi.readable()) {
289 Alert::alert(_("Error!"),
290 _("Specified file is unreadable: "),
291 MakeDisplayPath(fname, 50));
297 ifstream ifs(fname.c_str());
299 Alert::err_alert(_("Error! Cannot open specified file:"),
300 MakeDisplayPath(fname, 50));
304 int const c = ifs.peek();
312 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
313 res = buffer()->readFile(lex, fname, text->cursor.par());
315 lyxerr[Debug::INFO] << "Will insert file without header"
317 res = buffer()->readLyXformat2(lex, text->cursor.par());
325 bool BufferView::removeAutoInsets()
327 // keep track of which pos and par the cursor was on
328 Paragraph * cursor_par = text->cursor.par();
329 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
330 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
331 pos_type cursor_pos = text->cursor.pos();
335 // Trap the deletion of the paragraph the cursor is in.
336 // Iterate until we find a paragraph that won't be immediately deleted.
337 // In reality this should mean we only execute the body of the while
338 // loop once at most. However for safety we iterate rather than just
339 // make this an if () conditional.
340 while ((cursor_par_prev || cursor_par_next)
341 && text->setCursor(this,
342 cursor_par_prev ? cursor_par_prev : cursor_par_next,
344 // We just removed cursor_par so have to fix the "cursor"
345 if (cursor_par_prev) {
349 cursor_par = cursor_par_prev;
350 cursor_pos = cursor_par->size();
354 cursor_par = cursor_par_next;
357 cursor_par_prev = cursor_par->previous();
358 cursor_par_next = cursor_par->next();
361 // Iterate through the paragraphs removing autoDelete insets as we go.
362 // If the paragraph ends up empty after all the autoDelete insets are
363 // removed that paragraph will be removed by the next setCursor() call.
364 ParIterator it = buffer()->par_iterator_begin();
365 ParIterator end = buffer()->par_iterator_end();
366 for (; it != end; ++it) {
367 Paragraph * par = *it;
368 Paragraph * par_prev = par ? par->previous() : 0;
369 bool removed = false;
371 if (text->setCursor(this, par, 0)
372 && cursor_par == par_prev) {
373 // The previous setCursor line was deleted and that
374 // was the cursor_par line. This can only happen if an
375 // error box was the sole item on cursor_par.
376 // It is possible for cursor_par_prev to be stray if
377 // the line it pointed to only had a error box on it
378 // so we have to set it to a known correct value.
379 // This is often the same value it already had.
380 cursor_par_prev = par->previous();
381 if (cursor_par_prev) {
382 // '|' = par, '.' = cursor_par, 'E' = error box
383 // First step below may occur before while{}
385 // E -> .E -> |.E -> . -> |b
388 cursor_par = cursor_par_prev;
389 cursor_pos = cursor_par_prev->size();
390 cursor_par_prev = cursor_par->previous();
391 // cursor_par_next remains the same
392 } else if (cursor_par_next) {
393 // First step below may occur before while{}
395 // E -> |.E -> |. -> . -> .|a
397 cursor_par = cursor_par_next;
399 // cursor_par_prev remains unset
400 cursor_par_next = cursor_par->next();
402 // I can't find a way to trigger this
403 // so it should be unreachable code
404 // unless the buffer is corrupted.
405 lyxerr << "BufferView::removeAutoInsets() is bad\n";
409 InsetList::iterator pit = par->insetlist.begin();
410 InsetList::iterator pend = par->insetlist.end();
412 while (pit != pend) {
413 if (pit.getInset()->autoDelete()) {
415 pos_type const pos = pit.getPos();
418 // We just invalidated par's inset iterators so
419 // we get the next valid iterator position
420 pit = par->insetlist.insetIterator(pos);
421 // and ensure we have a valid end iterator.
422 pend = par->insetlist.end();
424 if (cursor_par == par) {
425 // update the saved cursor position
426 if (cursor_pos > pos)
435 text->redoParagraph(this);
439 // It is possible that the last line is empty if it was cursor_par
440 // and/or only had an error inset on it. So we set the cursor to the
441 // start of the doc to force its removal and ensure a valid saved cursor
442 if (text->setCursor(this, text->ownerParagraph(), 0)
443 && 0 == cursor_par_next) {
444 cursor_par = cursor_par_prev;
445 cursor_pos = cursor_par->size();
446 } else if (cursor_pos > cursor_par->size()) {
447 // Some C-Enter lines were removed by the setCursor call which
448 // then invalidated cursor_pos. It could still be "wrong" because
449 // the cursor may appear to have jumped but since we collapsed
450 // some C-Enter lines this should be a reasonable compromise.
451 cursor_pos = cursor_par->size();
454 // restore the original cursor in its corrected location.
455 text->setCursorIntern(this, cursor_par, cursor_pos);
461 void BufferView::insertErrors(TeXErrors & terr)
463 // Save the cursor position
464 LyXCursor cursor = text->cursor;
466 TeXErrors::Errors::const_iterator cit = terr.begin();
467 TeXErrors::Errors::const_iterator end = terr.end();
468 for (; cit != end; ++cit) {
469 string const desctext(cit->error_desc);
470 string const errortext(cit->error_text);
471 string const msgtxt = desctext + '\n' + errortext;
472 int const errorrow = cit->error_in_line;
474 // Insert error string for row number
478 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
479 buffer()->texrow.increasePos(tmpid, tmppos);
482 Paragraph * texrowpar = 0;
485 texrowpar = text->ownerParagraph();
488 texrowpar = buffer()->getParFromID(tmpid);
495 InsetError * new_inset = new InsetError(msgtxt);
496 text->setCursorIntern(this, texrowpar, tmppos);
497 text->insertInset(this, new_inset);
498 text->fullRebreak(this);
501 // Restore the cursor position
502 text->setCursorIntern(this, cursor.par(), cursor.pos());
506 void BufferView::setCursorFromRow(int row)
511 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
513 Paragraph * texrowpar;
516 texrowpar = text->ownerParagraph();
519 texrowpar = buffer()->getParFromID(tmpid);
521 text->setCursor(this, texrowpar, tmppos);
525 bool BufferView::insertInset(Inset * inset, string const & lout)
527 return pimpl_->insertInset(inset, lout);
531 // This is also a buffer property (ale)
532 // Not so sure about that. a goto Label function can not be buffer local, just
533 // think how this will work in a multiwindow/buffer environment, all the
534 // cursors in all the views showing this buffer will move. (Lgb)
535 // OK, then no cursor action should be allowed in buffer. (ale)
536 bool BufferView::gotoLabel(string const & label)
538 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
539 it != buffer()->inset_iterator_end(); ++it) {
540 vector<string> labels = it->getLabelList();
541 if (find(labels.begin(),labels.end(),label)
544 text->setCursor(this, it.getPar(), it.getPos());
545 text->selection.cursor = text->cursor;
546 update(text, BufferView::SELECT|BufferView::FITCUR);
554 void BufferView::undo()
559 owner()->message(_("Undo"));
562 update(text, BufferView::SELECT|BufferView::FITCUR);
564 owner()->message(_("No further undo information"));
566 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
571 void BufferView::redo()
576 owner()->message(_("Redo"));
579 update(text, BufferView::SELECT|BufferView::FITCUR);
581 owner()->message(_("No further redo information"));
583 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
588 void BufferView::copyEnvironment()
591 text->copyEnvironmentType();
592 owner()->message(_("Paragraph environment type copied"));
597 void BufferView::pasteEnvironment()
600 text->pasteEnvironmentType(this);
601 owner()->message(_("Paragraph environment type set"));
602 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
607 // these functions are for the spellchecker
608 WordLangTuple const BufferView::nextWord(float & value)
612 return WordLangTuple();
615 return text->selectNextWordToSpellcheck(this, value);
619 void BufferView::selectLastWord()
624 LyXCursor cur = text->selection.cursor;
627 text->selection.cursor = cur;
628 text->selectSelectedWord(this);
629 toggleSelection(false);
630 update(text, BufferView::SELECT|BufferView::FITCUR);
634 void BufferView::endOfSpellCheck()
636 if (!available()) return;
640 text->selectSelectedWord(this);
641 text->clearSelection();
642 update(text, BufferView::SELECT|BufferView::FITCUR);
646 void BufferView::replaceWord(string const & replacestring)
651 LyXText * tt = getLyXText();
653 update(tt, BufferView::SELECT|BufferView::FITCUR);
655 // clear the selection (if there is any)
656 toggleSelection(false);
657 update(tt, BufferView::SELECT|BufferView::FITCUR);
659 // clear the selection (if there is any)
660 toggleSelection(false);
661 tt->replaceSelectionWithString(this, replacestring);
663 tt->setSelectionRange(this, replacestring.length());
665 // Go back so that replacement string is also spellchecked
666 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
667 tt->cursorLeft(this);
669 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
671 // End of spellchecker stuff
674 bool BufferView::lockInset(UpdatableInset * inset)
678 // don't relock if we're already locked
679 if (theLockingInset() == inset)
681 if (!theLockingInset()) {
682 // first check if it's the inset under the cursor we want lock
683 // should be most of the time
684 char const c = text->cursor.par()->getChar(text->cursor.pos());
685 if (c == Paragraph::META_INSET) {
686 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
688 theLockingInset(inset);
692 // Then do a deep look of the inset and lock the right one
693 int const id = inset->id();
694 ParagraphList::iterator pit = buffer()->paragraphs.begin();
695 ParagraphList::iterator pend = buffer()->paragraphs.end();
696 for (; pit != pend; ++pit) {
697 InsetList::iterator it = pit->insetlist.begin();
698 InsetList::iterator end = pit->insetlist.end();
699 for (; it != end; ++it) {
700 if (it.getInset() == inset) {
701 text->setCursorIntern(this, &*pit, it.getPos());
702 theLockingInset(inset);
705 if (it.getInset()->getInsetFromID(id)) {
706 text->setCursorIntern(this, &*pit, it.getPos());
707 it.getInset()->edit(this);
708 return theLockingInset()->lockInsetInInset(this, inset);
714 return theLockingInset()->lockInsetInInset(this, inset);
718 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
720 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
721 LyXCursor cursor = text->cursor;
722 Inset * locking_inset = theLockingInset()->getLockingInset();
724 if ((cursor.pos() - 1 >= 0) &&
725 cursor.par()->isInset(cursor.pos() - 1) &&
726 (cursor.par()->getInset(cursor.pos() - 1) ==
728 text->setCursor(this, cursor,
729 cursor.par(), cursor.pos() - 1);
730 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
731 LyXText * txt = getLyXText();
732 if (locking_inset->isTextInset() &&
733 locking_inset->lyxCode() != Inset::ERT_CODE &&
734 (txt->real_current_font.language() !=
735 buffer()->params.language
736 || txt->real_current_font.isVisibleRightToLeft()
737 != buffer()->params.language->RightToLeft()))
738 shape = (txt->real_current_font.isVisibleRightToLeft())
739 ? LyXScreen::REVERSED_L_SHAPE
740 : LyXScreen::L_SHAPE;
741 y += cursor.iy() + theLockingInset()->insetInInsetY();
742 screen().showManualCursor(text, x, y, asc, desc,
748 void BufferView::hideLockedInsetCursor()
750 if (theLockingInset() && available()) {
751 screen().hideCursor();
756 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
758 if (theLockingInset() && available()) {
759 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
760 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
769 int BufferView::unlockInset(UpdatableInset * inset)
773 if (inset && theLockingInset() == inset) {
774 inset->insetUnlock(this);
776 // make sure we update the combo !
777 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
778 // Tell the paragraph dialog that we changed paragraph
779 owner()->getDialogs().updateParagraph();
782 } else if (inset && theLockingInset() &&
783 theLockingInset()->unlockInsetInInset(this, inset)) {
784 // Tell the paragraph dialog that we changed paragraph
785 owner()->getDialogs().updateParagraph();
786 // owner inset has updated the layout combo
794 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
796 if (!theLockingInset())
797 return; // shouldn't happen
798 if (kind == Undo::EDIT) // in this case insets would not be stored!
802 text->cursor.par()->next());
806 void BufferView::updateInset(Inset * inset, bool mark_dirty)
808 pimpl_->updateInset(inset, mark_dirty);
812 bool BufferView::ChangeInsets(Inset::Code code,
813 string const & from, string const & to)
815 bool need_update = false;
816 LyXCursor cursor = text->cursor;
817 LyXCursor tmpcursor = cursor;
818 cursor.par(tmpcursor.par());
819 cursor.pos(tmpcursor.pos());
821 ParIterator end = buffer()->par_iterator_end();
822 for (ParIterator it = buffer()->par_iterator_begin();
824 Paragraph * par = *it;
825 bool changed_inset = false;
826 for (InsetList::iterator it2 = par->insetlist.begin();
827 it2 != par->insetlist.end(); ++it2) {
828 if (it2.getInset()->lyxCode() == code) {
829 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
830 if (inset->getContents() == from) {
831 inset->setContents(to);
832 changed_inset = true;
841 // The test it.size()==1 was needed to prevent crashes.
842 // How to set the cursor corretly when it.size()>1 ??
843 if (it.size() == 1) {
844 text->setCursorIntern(this, par, 0);
845 text->redoParagraphs(this, text->cursor,
846 text->cursor.par()->next());
847 text->fullRebreak(this);
851 text->setCursorIntern(this, cursor.par(), cursor.pos());
856 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
858 // Check if the label 'from' appears more than once
859 vector<string> labels = buffer()->getLabelList();
861 if (lyx::count(labels.begin(), labels.end(), from) > 1)
864 return ChangeInsets(Inset::REF_CODE, from, to);
868 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
870 typedef pair<string, string> StringPair;
872 vector<StringPair> keys;
873 buffer()->fillWithBibKeys(keys);
874 if (count_if(keys.begin(), keys.end(),
875 lyx::equal_1st_in_pair<StringPair>(from))
879 return ChangeInsets(Inset::CITE_CODE, from, to);
883 UpdatableInset * BufferView::theLockingInset() const
885 // If NULL is not allowed we should put an Assert here. (Lgb)
887 return text->the_locking_inset;
892 void BufferView::theLockingInset(UpdatableInset * inset)
894 text->the_locking_inset = inset;
898 LyXText * BufferView::getLyXText() const
900 if (theLockingInset()) {
901 LyXText * txt = theLockingInset()->getLyXText(this, true);
909 LyXText * BufferView::getParentText(Inset * inset) const
911 if (inset->owner()) {
912 LyXText * txt = inset->getLyXText(this);
913 inset = inset->owner();
914 while (inset && inset->getLyXText(this) == txt)
915 inset = inset->owner();
917 return inset->getLyXText(this);
923 Language const * BufferView::getParentLanguage(Inset * inset) const
925 LyXText * text = getParentText(inset);
926 return text->cursor.par()->getFontSettings(buffer()->params,
927 text->cursor.pos()).language();
931 Encoding const * BufferView::getEncoding() const
933 LyXText * t = getLyXText();
937 LyXCursor const & c= t->cursor;
938 LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
939 return font.language()->encoding();
943 void BufferView::haveSelection(bool sel)
945 pimpl_->workarea().haveSelection(sel);
949 int BufferView::workHeight() const
951 return pimpl_->workarea().workHeight();