3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
13 #pragma implementation
16 #include "BufferView.h"
17 #include "BufferView_pimpl.h"
19 #include "BufferView_pimpl.h"
21 #include "WordLangTuple.h"
23 #include "bufferlist.h"
26 #include "iterators.h"
28 #include "lyxcursor.h"
31 #include "undo_funcs.h"
33 #include "frontends/Alert.h"
34 #include "frontends/Dialogs.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/WorkArea.h"
37 #include "frontends/screen.h"
39 #include "insets/insetcommand.h" // ChangeRefs
40 #include "insets/inseterror.h"
42 #include "support/FileInfo.h"
43 #include "support/filetools.h"
44 #include "support/lyxfunctional.h" // equal_1st_in_pair
45 #include "support/types.h"
46 #include "support/lyxalgo.h" // lyx_count
50 extern BufferList bufferlist;
62 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
63 int width, int height)
64 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
70 BufferView::~BufferView()
77 Buffer * BufferView::buffer() const
79 return pimpl_->buffer_;
83 LyXScreen & BufferView::screen() const
85 return pimpl_->screen();
89 WorkArea & BufferView::workarea() const
91 return pimpl_->workarea();
95 LyXView * BufferView::owner() const
97 return pimpl_->owner_;
101 Painter & BufferView::painter() const
103 return pimpl_->painter();
107 void BufferView::buffer(Buffer * b)
113 void BufferView::resize()
115 if (pimpl_->buffer_) {
116 pimpl_->resizeCurrentBuffer();
121 void BufferView::repaint()
127 bool BufferView::fitCursor()
129 return pimpl_->fitCursor();
133 void BufferView::update()
139 void BufferView::updateScrollbar()
141 pimpl_->updateScrollbar();
145 void BufferView::scrollDocView(int value)
147 pimpl_->scrollDocView(value);
151 void BufferView::redoCurrentBuffer()
153 pimpl_->redoCurrentBuffer();
157 bool BufferView::available() const
159 return pimpl_->available();
163 void BufferView::beforeChange(LyXText * text)
165 pimpl_->beforeChange(text);
169 void BufferView::savePosition(unsigned int i)
171 pimpl_->savePosition(i);
175 void BufferView::restorePosition(unsigned int i)
177 pimpl_->restorePosition(i);
181 bool BufferView::isSavedPosition(unsigned int i)
183 return pimpl_->isSavedPosition(i);
187 void BufferView::update(LyXText * text, UpdateCodes f)
189 pimpl_->update(text, f);
193 void BufferView::switchKeyMap()
195 pimpl_->switchKeyMap();
199 void BufferView::insetUnlock()
201 pimpl_->insetUnlock();
205 int BufferView::workWidth() const
207 return pimpl_->workarea().workWidth();
211 void BufferView::showCursor()
213 pimpl_->showCursor();
217 void BufferView::hideCursor()
219 pimpl_->hideCursor();
223 void BufferView::toggleSelection(bool b)
225 pimpl_->toggleSelection(b);
229 void BufferView::toggleToggle()
231 pimpl_->toggleToggle();
235 void BufferView::center()
241 string const BufferView::getClipboard() const
243 return pimpl_->workarea().getClipboard();
247 void BufferView::stuffClipboard(string const & stuff) const
249 pimpl_->stuffClipboard(stuff);
253 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
254 BufferView::UpdateCodes uc2)
256 return static_cast<BufferView::UpdateCodes>
257 (static_cast<int>(uc1) | static_cast<int>(uc2));
261 bool BufferView::dispatch(FuncRequest const & ev)
263 return pimpl_->dispatch(ev);
267 int BufferView::scroll(long time)
269 return pimpl_->scroll(time);
273 int BufferView::defaultHeight() const
275 return text->defaultHeight();
280 // Inserts a file into current document
281 bool BufferView::insertLyXFile(string const & filen)
283 // Copyright CHT Software Service GmbH
286 // Insert a LyXformat - file into current buffer
288 // Moved from lyx_cb.C (Lgb)
293 string const fname = MakeAbsPath(filen);
295 // check if file exist
296 FileInfo const fi(fname);
298 if (!fi.readable()) {
299 Alert::alert(_("Error!"),
300 _("Specified file is unreadable: "),
301 MakeDisplayPath(fname, 50));
307 ifstream ifs(fname.c_str());
309 Alert::alert(_("Error!"),
310 _("Cannot open specified file: "),
311 MakeDisplayPath(fname, 50));
315 int const c = ifs.peek();
323 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
324 res = buffer()->readFile(lex, text->cursor.par());
326 lyxerr[Debug::INFO] << "Will insert file without header"
328 res = buffer()->readLyXformat2(lex, text->cursor.par());
336 bool BufferView::removeAutoInsets()
338 // keep track of which pos and par the cursor was on
339 Paragraph * cursor_par = text->cursor.par();
340 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
341 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
342 pos_type cursor_pos = text->cursor.pos();
346 // Trap the deletion of the paragraph the cursor is in.
347 // Iterate until we find a paragraph that won't be immediately deleted.
348 // In reality this should mean we only execute the body of the while
349 // loop once at most. However for safety we iterate rather than just
350 // make this an if () conditional.
351 while ((cursor_par_prev || cursor_par_next)
352 && text->setCursor(this,
353 cursor_par_prev ? cursor_par_prev : cursor_par_next,
355 // We just removed cursor_par so have to fix the "cursor"
356 if (cursor_par_prev) {
360 cursor_par = cursor_par_prev;
361 cursor_pos = cursor_par->size();
365 cursor_par = cursor_par_next;
368 cursor_par_prev = cursor_par->previous();
369 cursor_par_next = cursor_par->next();
372 // Iterate through the paragraphs removing autoDelete insets as we go.
373 // If the paragraph ends up empty after all the autoDelete insets are
374 // removed that paragraph will be removed by the next setCursor() call.
375 ParIterator it = buffer()->par_iterator_begin();
376 ParIterator end = buffer()->par_iterator_end();
377 for (; it != end; ++it) {
378 Paragraph * par = *it;
379 Paragraph * par_prev = par ? par->previous() : 0;
380 bool removed = false;
382 if (text->setCursor(this, par, 0)
383 && cursor_par == par_prev) {
384 // The previous setCursor line was deleted and that
385 // was the cursor_par line. This can only happen if an
386 // error box was the sole item on cursor_par.
387 // It is possible for cursor_par_prev to be stray if
388 // the line it pointed to only had a error box on it
389 // so we have to set it to a known correct value.
390 // This is often the same value it already had.
391 cursor_par_prev = par->previous();
392 if (cursor_par_prev) {
393 // '|' = par, '.' = cursor_par, 'E' = error box
394 // First step below may occur before while{}
396 // E -> .E -> |.E -> . -> |b
399 cursor_par = cursor_par_prev;
400 cursor_pos = cursor_par_prev->size();
401 cursor_par_prev = cursor_par->previous();
402 // cursor_par_next remains the same
403 } else if (cursor_par_next) {
404 // First step below may occur before while{}
406 // E -> |.E -> |. -> . -> .|a
408 cursor_par = cursor_par_next;
410 // cursor_par_prev remains unset
411 cursor_par_next = cursor_par->next();
413 // I can't find a way to trigger this
414 // so it should be unreachable code
415 // unless the buffer is corrupted.
416 lyxerr << "BufferView::removeAutoInsets() is bad\n";
420 InsetList::iterator pit = par->insetlist.begin();
421 InsetList::iterator pend = par->insetlist.end();
423 while (pit != pend) {
424 if (pit.getInset()->autoDelete()) {
426 pos_type const pos = pit.getPos();
429 // We just invalidated par's inset iterators so
430 // we get the next valid iterator position
431 pit = par->insetlist.insetIterator(pos);
432 // and ensure we have a valid end iterator.
433 pend = par->insetlist.end();
435 if (cursor_par == par) {
436 // update the saved cursor position
437 if (cursor_pos > pos)
446 text->redoParagraph(this);
450 // It is possible that the last line is empty if it was cursor_par
451 // and/or only had an error inset on it. So we set the cursor to the
452 // start of the doc to force its removal and ensure a valid saved cursor
453 if (text->setCursor(this, text->ownerParagraph(), 0)
454 && 0 == cursor_par_next) {
455 cursor_par = cursor_par_prev;
456 cursor_pos = cursor_par->size();
457 } else if (cursor_pos > cursor_par->size()) {
458 // Some C-Enter lines were removed by the setCursor call which
459 // then invalidated cursor_pos. It could still be "wrong" because
460 // the cursor may appear to have jumped but since we collapsed
461 // some C-Enter lines this should be a reasonable compromise.
462 cursor_pos = cursor_par->size();
465 // restore the original cursor in its corrected location.
466 text->setCursorIntern(this, cursor_par, cursor_pos);
472 void BufferView::insertErrors(TeXErrors & terr)
474 // Save the cursor position
475 LyXCursor cursor = text->cursor;
477 TeXErrors::Errors::const_iterator cit = terr.begin();
478 TeXErrors::Errors::const_iterator end = terr.end();
479 for (; cit != end; ++cit) {
480 string const desctext(cit->error_desc);
481 string const errortext(cit->error_text);
482 string const msgtxt = desctext + '\n' + errortext;
483 int const errorrow = cit->error_in_line;
485 // Insert error string for row number
489 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
490 buffer()->texrow.increasePos(tmpid, tmppos);
493 Paragraph * texrowpar = 0;
496 texrowpar = text->ownerParagraph();
499 texrowpar = buffer()->getParFromID(tmpid);
506 InsetError * new_inset = new InsetError(msgtxt);
507 text->setCursorIntern(this, texrowpar, tmppos);
508 text->insertInset(this, new_inset);
509 text->fullRebreak(this);
512 // Restore the cursor position
513 text->setCursorIntern(this, cursor.par(), cursor.pos());
517 void BufferView::setCursorFromRow(int row)
522 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
524 Paragraph * texrowpar;
527 texrowpar = text->ownerParagraph();
530 texrowpar = buffer()->getParFromID(tmpid);
532 text->setCursor(this, texrowpar, tmppos);
536 bool BufferView::insertInset(Inset * inset, string const & lout)
538 return pimpl_->insertInset(inset, lout);
542 // This is also a buffer property (ale)
543 // Not so sure about that. a goto Label function can not be buffer local, just
544 // think how this will work in a multiwindow/buffer environment, all the
545 // cursors in all the views showing this buffer will move. (Lgb)
546 // OK, then no cursor action should be allowed in buffer. (ale)
547 bool BufferView::gotoLabel(string const & label)
549 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
550 it != buffer()->inset_iterator_end(); ++it) {
551 vector<string> labels = it->getLabelList();
552 if (find(labels.begin(),labels.end(),label)
555 text->setCursor(this, it.getPar(), it.getPos());
556 text->selection.cursor = text->cursor;
557 update(text, BufferView::SELECT|BufferView::FITCUR);
565 void BufferView::menuUndo()
570 owner()->message(_("Undo"));
573 update(text, BufferView::SELECT|BufferView::FITCUR);
575 owner()->message(_("No further undo information"));
577 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
582 void BufferView::menuRedo()
587 owner()->message(_("Redo"));
590 update(text, BufferView::SELECT|BufferView::FITCUR);
592 owner()->message(_("No further redo information"));
594 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
599 void BufferView::copyEnvironment()
602 text->copyEnvironmentType();
603 owner()->message(_("Paragraph environment type copied"));
608 void BufferView::pasteEnvironment()
611 text->pasteEnvironmentType(this);
612 owner()->message(_("Paragraph environment type set"));
613 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
618 // these functions are for the spellchecker
619 WordLangTuple const BufferView::nextWord(float & value)
623 return WordLangTuple();
626 return text->selectNextWordToSpellcheck(this, value);
630 void BufferView::selectLastWord()
635 LyXCursor cur = text->selection.cursor;
638 text->selection.cursor = cur;
639 text->selectSelectedWord(this);
640 toggleSelection(false);
641 update(text, BufferView::SELECT|BufferView::FITCUR);
645 void BufferView::endOfSpellCheck()
647 if (!available()) return;
651 text->selectSelectedWord(this);
652 text->clearSelection();
653 update(text, BufferView::SELECT|BufferView::FITCUR);
657 void BufferView::replaceWord(string const & replacestring)
662 LyXText * tt = getLyXText();
664 update(tt, BufferView::SELECT|BufferView::FITCUR);
666 // clear the selection (if there is any)
667 toggleSelection(false);
668 update(tt, BufferView::SELECT|BufferView::FITCUR);
670 // clear the selection (if there is any)
671 toggleSelection(false);
672 tt->replaceSelectionWithString(this, replacestring);
674 tt->setSelectionOverString(this, replacestring);
676 // Go back so that replacement string is also spellchecked
677 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
678 tt->cursorLeft(this);
680 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
682 // End of spellchecker stuff
685 bool BufferView::lockInset(UpdatableInset * inset)
689 // don't relock if we're already locked
690 if (theLockingInset() == inset)
692 if (!theLockingInset()) {
693 // first check if it's the inset under the cursor we want lock
694 // should be most of the time
695 char const c = text->cursor.par()->getChar(text->cursor.pos());
696 if (c == Paragraph::META_INSET) {
697 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
699 theLockingInset(inset);
703 // Then do a deep look of the inset and lock the right one
704 int const id = inset->id();
705 ParagraphList::iterator pit = buffer()->paragraphs.begin();
706 ParagraphList::iterator pend = buffer()->paragraphs.end();
707 for (; pit != pend; ++pit) {
708 InsetList::iterator it = pit->insetlist.begin();
709 InsetList::iterator end = pit->insetlist.end();
710 for (; it != end; ++it) {
711 if (it.getInset() == inset) {
712 text->setCursorIntern(this, &*pit, it.getPos());
713 theLockingInset(inset);
716 if (it.getInset()->getInsetFromID(id)) {
717 text->setCursorIntern(this, &*pit, it.getPos());
718 it.getInset()->edit(this);
719 return theLockingInset()->lockInsetInInset(this, inset);
725 return theLockingInset()->lockInsetInInset(this, inset);
729 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
731 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
732 LyXCursor cursor = text->cursor;
733 Inset * locking_inset = theLockingInset()->getLockingInset();
735 if ((cursor.pos() - 1 >= 0) &&
736 cursor.par()->isInset(cursor.pos() - 1) &&
737 (cursor.par()->getInset(cursor.pos() - 1) ==
739 text->setCursor(this, cursor,
740 cursor.par(), cursor.pos() - 1);
741 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
742 LyXText * txt = getLyXText();
743 if (locking_inset->isTextInset() &&
744 locking_inset->lyxCode() != Inset::ERT_CODE &&
745 (txt->real_current_font.language() !=
746 buffer()->params.language
747 || txt->real_current_font.isVisibleRightToLeft()
748 != buffer()->params.language->RightToLeft()))
749 shape = (txt->real_current_font.isVisibleRightToLeft())
750 ? LyXScreen::REVERSED_L_SHAPE
751 : LyXScreen::L_SHAPE;
752 y += cursor.iy() + theLockingInset()->insetInInsetY();
753 screen().showManualCursor(text, x, y, asc, desc,
759 void BufferView::hideLockedInsetCursor()
761 if (theLockingInset() && available()) {
762 screen().hideCursor();
767 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
769 if (theLockingInset() && available()) {
770 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
771 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
780 int BufferView::unlockInset(UpdatableInset * inset)
784 if (inset && theLockingInset() == inset) {
785 inset->insetUnlock(this);
787 // make sure we update the combo !
788 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
789 // Tell the paragraph dialog that we changed paragraph
790 owner()->getDialogs().updateParagraph();
793 } else if (inset && theLockingInset() &&
794 theLockingInset()->unlockInsetInInset(this, inset)) {
795 // Tell the paragraph dialog that we changed paragraph
796 owner()->getDialogs().updateParagraph();
797 // owner inset has updated the layout combo
801 return bufferlist.unlockInset(inset);
805 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
807 if (!theLockingInset())
808 return; // shouldn't happen
809 if (kind == Undo::EDIT) // in this case insets would not be stored!
813 text->cursor.par()->next());
817 void BufferView::updateInset(Inset * inset, bool mark_dirty)
819 pimpl_->updateInset(inset, mark_dirty);
823 bool BufferView::ChangeInsets(Inset::Code code,
824 string const & from, string const & to)
826 bool need_update = false;
827 LyXCursor cursor = text->cursor;
828 LyXCursor tmpcursor = cursor;
829 cursor.par(tmpcursor.par());
830 cursor.pos(tmpcursor.pos());
832 ParIterator end = buffer()->par_iterator_end();
833 for (ParIterator it = buffer()->par_iterator_begin();
835 Paragraph * par = *it;
836 bool changed_inset = false;
837 for (InsetList::iterator it2 = par->insetlist.begin();
838 it2 != par->insetlist.end(); ++it2) {
839 if (it2.getInset()->lyxCode() == code) {
840 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
841 if (inset->getContents() == from) {
842 inset->setContents(to);
843 changed_inset = true;
852 // The test it.size()==1 was needed to prevent crashes.
853 // How to set the cursor corretly when it.size()>1 ??
854 if (it.size() == 1) {
855 text->setCursorIntern(this, par, 0);
856 text->redoParagraphs(this, text->cursor,
857 text->cursor.par()->next());
858 text->fullRebreak(this);
862 text->setCursorIntern(this, cursor.par(), cursor.pos());
867 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
869 // Check if the label 'from' appears more than once
870 vector<string> labels = buffer()->getLabelList();
872 if (lyx::count(labels.begin(), labels.end(), from) > 1)
875 return ChangeInsets(Inset::REF_CODE, from, to);
879 bool BufferView::ChangeCitationsIfUnique(string const & from,
882 typedef pair<string, string> StringPair;
884 vector<StringPair> keys = buffer()->getBibkeyList();
885 if (count_if(keys.begin(), keys.end(),
886 lyx::equal_1st_in_pair<StringPair>(from))
890 return ChangeInsets(Inset::CITE_CODE, from, to);
894 UpdatableInset * BufferView::theLockingInset() const
896 // If NULL is not allowed we should put an Assert here. (Lgb)
898 return text->the_locking_inset;
903 void BufferView::theLockingInset(UpdatableInset * inset)
905 text->the_locking_inset = inset;
909 LyXText * BufferView::getLyXText() const
911 if (theLockingInset()) {
912 LyXText * txt = theLockingInset()->getLyXText(this, true);
920 LyXText * BufferView::getParentText(Inset * inset) const
922 if (inset->owner()) {
923 LyXText * txt = inset->getLyXText(this);
924 inset = inset->owner();
925 while (inset && inset->getLyXText(this) == txt)
926 inset = inset->owner();
928 return inset->getLyXText(this);
934 Language const * BufferView::getParentLanguage(Inset * inset) const
936 LyXText * text = getParentText(inset);
937 return text->cursor.par()->getFontSettings(buffer()->params,
938 text->cursor.pos()).language();