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 LyXView * BufferView::owner() const
91 return pimpl_->owner_;
95 Painter & BufferView::painter() const
97 return pimpl_->painter();
101 void BufferView::buffer(Buffer * b)
107 void BufferView::resize()
109 if (pimpl_->buffer_) {
110 pimpl_->resizeCurrentBuffer();
115 void BufferView::repaint()
121 bool BufferView::fitCursor()
123 return pimpl_->fitCursor();
127 void BufferView::update()
133 void BufferView::updateScrollbar()
135 pimpl_->updateScrollbar();
139 void BufferView::scrollDocView(int value)
141 pimpl_->scrollDocView(value);
145 void BufferView::redoCurrentBuffer()
147 pimpl_->redoCurrentBuffer();
151 bool BufferView::available() const
153 return pimpl_->available();
157 void BufferView::beforeChange(LyXText * text)
159 pimpl_->beforeChange(text);
163 void BufferView::savePosition(unsigned int i)
165 pimpl_->savePosition(i);
169 void BufferView::restorePosition(unsigned int i)
171 pimpl_->restorePosition(i);
175 bool BufferView::isSavedPosition(unsigned int i)
177 return pimpl_->isSavedPosition(i);
181 void BufferView::update(LyXText * text, UpdateCodes f)
183 pimpl_->update(text, f);
187 void BufferView::switchKeyMap()
189 pimpl_->switchKeyMap();
193 void BufferView::insetUnlock()
195 pimpl_->insetUnlock();
199 int BufferView::workWidth() const
201 return pimpl_->workarea().workWidth();
205 void BufferView::showCursor()
207 pimpl_->showCursor();
211 void BufferView::hideCursor()
213 pimpl_->hideCursor();
217 void BufferView::toggleSelection(bool b)
219 pimpl_->toggleSelection(b);
223 void BufferView::toggleToggle()
225 pimpl_->toggleToggle();
229 void BufferView::center()
235 string const BufferView::getClipboard() const
237 return pimpl_->workarea().getClipboard();
241 void BufferView::stuffClipboard(string const & stuff) const
243 pimpl_->stuffClipboard(stuff);
247 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
248 BufferView::UpdateCodes uc2)
250 return static_cast<BufferView::UpdateCodes>
251 (static_cast<int>(uc1) | static_cast<int>(uc2));
255 bool BufferView::dispatch(FuncRequest const & ev)
257 return pimpl_->dispatch(ev);
261 void BufferView::scroll(int lines)
263 pimpl_->scroll(lines);
267 int BufferView::defaultHeight() const
269 return text->defaultHeight();
274 // Inserts a file into current document
275 bool BufferView::insertLyXFile(string const & filen)
277 // Copyright CHT Software Service GmbH
280 // Insert a LyXformat - file into current buffer
282 // Moved from lyx_cb.C (Lgb)
287 string const fname = MakeAbsPath(filen);
289 // check if file exist
290 FileInfo const fi(fname);
292 if (!fi.readable()) {
293 Alert::alert(_("Error!"),
294 _("Specified file is unreadable: "),
295 MakeDisplayPath(fname, 50));
301 ifstream ifs(fname.c_str());
303 Alert::err_alert(_("Error! Cannot open specified file:"),
304 MakeDisplayPath(fname, 50));
308 int const c = ifs.peek();
316 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
317 res = buffer()->readFile(lex, fname, text->cursor.par());
319 lyxerr[Debug::INFO] << "Will insert file without header"
321 res = buffer()->readLyXformat2(lex, text->cursor.par());
329 bool BufferView::removeAutoInsets()
331 // keep track of which pos and par the cursor was on
332 Paragraph * cursor_par = text->cursor.par();
333 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
334 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
335 pos_type cursor_pos = text->cursor.pos();
339 // Trap the deletion of the paragraph the cursor is in.
340 // Iterate until we find a paragraph that won't be immediately deleted.
341 // In reality this should mean we only execute the body of the while
342 // loop once at most. However for safety we iterate rather than just
343 // make this an if () conditional.
344 while ((cursor_par_prev || cursor_par_next)
345 && text->setCursor(this,
346 cursor_par_prev ? cursor_par_prev : cursor_par_next,
348 // We just removed cursor_par so have to fix the "cursor"
349 if (cursor_par_prev) {
353 cursor_par = cursor_par_prev;
354 cursor_pos = cursor_par->size();
358 cursor_par = cursor_par_next;
361 cursor_par_prev = cursor_par->previous();
362 cursor_par_next = cursor_par->next();
365 // Iterate through the paragraphs removing autoDelete insets as we go.
366 // If the paragraph ends up empty after all the autoDelete insets are
367 // removed that paragraph will be removed by the next setCursor() call.
368 ParIterator it = buffer()->par_iterator_begin();
369 ParIterator end = buffer()->par_iterator_end();
370 for (; it != end; ++it) {
371 Paragraph * par = *it;
372 Paragraph * par_prev = par ? par->previous() : 0;
373 bool removed = false;
375 if (text->setCursor(this, par, 0)
376 && cursor_par == par_prev) {
377 // The previous setCursor line was deleted and that
378 // was the cursor_par line. This can only happen if an
379 // error box was the sole item on cursor_par.
380 // It is possible for cursor_par_prev to be stray if
381 // the line it pointed to only had a error box on it
382 // so we have to set it to a known correct value.
383 // This is often the same value it already had.
384 cursor_par_prev = par->previous();
385 if (cursor_par_prev) {
386 // '|' = par, '.' = cursor_par, 'E' = error box
387 // First step below may occur before while{}
389 // E -> .E -> |.E -> . -> |b
392 cursor_par = cursor_par_prev;
393 cursor_pos = cursor_par_prev->size();
394 cursor_par_prev = cursor_par->previous();
395 // cursor_par_next remains the same
396 } else if (cursor_par_next) {
397 // First step below may occur before while{}
399 // E -> |.E -> |. -> . -> .|a
401 cursor_par = cursor_par_next;
403 // cursor_par_prev remains unset
404 cursor_par_next = cursor_par->next();
406 // I can't find a way to trigger this
407 // so it should be unreachable code
408 // unless the buffer is corrupted.
409 lyxerr << "BufferView::removeAutoInsets() is bad\n";
413 InsetList::iterator pit = par->insetlist.begin();
414 InsetList::iterator pend = par->insetlist.end();
416 while (pit != pend) {
417 if (pit.getInset()->autoDelete()) {
419 pos_type const pos = pit.getPos();
422 // We just invalidated par's inset iterators so
423 // we get the next valid iterator position
424 pit = par->insetlist.insetIterator(pos);
425 // and ensure we have a valid end iterator.
426 pend = par->insetlist.end();
428 if (cursor_par == par) {
429 // update the saved cursor position
430 if (cursor_pos > pos)
439 text->redoParagraph(this);
443 // It is possible that the last line is empty if it was cursor_par
444 // and/or only had an error inset on it. So we set the cursor to the
445 // start of the doc to force its removal and ensure a valid saved cursor
446 if (text->setCursor(this, text->ownerParagraph(), 0)
447 && 0 == cursor_par_next) {
448 cursor_par = cursor_par_prev;
449 cursor_pos = cursor_par->size();
450 } else if (cursor_pos > cursor_par->size()) {
451 // Some C-Enter lines were removed by the setCursor call which
452 // then invalidated cursor_pos. It could still be "wrong" because
453 // the cursor may appear to have jumped but since we collapsed
454 // some C-Enter lines this should be a reasonable compromise.
455 cursor_pos = cursor_par->size();
458 // restore the original cursor in its corrected location.
459 text->setCursorIntern(this, cursor_par, cursor_pos);
465 void BufferView::insertErrors(TeXErrors & terr)
467 // Save the cursor position
468 LyXCursor cursor = text->cursor;
470 TeXErrors::Errors::const_iterator cit = terr.begin();
471 TeXErrors::Errors::const_iterator end = terr.end();
472 for (; cit != end; ++cit) {
473 string const desctext(cit->error_desc);
474 string const errortext(cit->error_text);
475 string const msgtxt = desctext + '\n' + errortext;
476 int const errorrow = cit->error_in_line;
478 // Insert error string for row number
482 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
483 buffer()->texrow.increasePos(tmpid, tmppos);
486 Paragraph * texrowpar = 0;
489 texrowpar = text->ownerParagraph();
492 texrowpar = buffer()->getParFromID(tmpid);
499 InsetError * new_inset = new InsetError(msgtxt);
500 text->setCursorIntern(this, texrowpar, tmppos);
501 text->insertInset(this, new_inset);
502 text->fullRebreak(this);
505 // Restore the cursor position
506 text->setCursorIntern(this, cursor.par(), cursor.pos());
510 void BufferView::setCursorFromRow(int row)
515 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
517 Paragraph * texrowpar;
520 texrowpar = text->ownerParagraph();
523 texrowpar = buffer()->getParFromID(tmpid);
525 text->setCursor(this, texrowpar, tmppos);
529 bool BufferView::insertInset(Inset * inset, string const & lout)
531 return pimpl_->insertInset(inset, lout);
535 // This is also a buffer property (ale)
536 // Not so sure about that. a goto Label function can not be buffer local, just
537 // think how this will work in a multiwindow/buffer environment, all the
538 // cursors in all the views showing this buffer will move. (Lgb)
539 // OK, then no cursor action should be allowed in buffer. (ale)
540 bool BufferView::gotoLabel(string const & label)
542 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
543 it != buffer()->inset_iterator_end(); ++it) {
544 vector<string> labels = it->getLabelList();
545 if (find(labels.begin(),labels.end(),label)
548 text->setCursor(this, it.getPar(), it.getPos());
549 text->selection.cursor = text->cursor;
550 update(text, BufferView::SELECT|BufferView::FITCUR);
558 void BufferView::undo()
563 owner()->message(_("Undo"));
566 update(text, BufferView::SELECT|BufferView::FITCUR);
568 owner()->message(_("No further undo information"));
570 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
575 void BufferView::redo()
580 owner()->message(_("Redo"));
583 update(text, BufferView::SELECT|BufferView::FITCUR);
585 owner()->message(_("No further redo information"));
587 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
592 void BufferView::copyEnvironment()
595 text->copyEnvironmentType();
596 owner()->message(_("Paragraph environment type copied"));
601 void BufferView::pasteEnvironment()
604 text->pasteEnvironmentType(this);
605 owner()->message(_("Paragraph environment type set"));
606 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
611 // these functions are for the spellchecker
612 WordLangTuple const BufferView::nextWord(float & value)
616 return WordLangTuple();
619 return text->selectNextWordToSpellcheck(this, value);
623 void BufferView::selectLastWord()
628 LyXCursor cur = text->selection.cursor;
631 text->selection.cursor = cur;
632 text->selectSelectedWord(this);
633 toggleSelection(false);
634 update(text, BufferView::SELECT|BufferView::FITCUR);
638 void BufferView::endOfSpellCheck()
640 if (!available()) return;
644 text->selectSelectedWord(this);
645 text->clearSelection();
646 update(text, BufferView::SELECT|BufferView::FITCUR);
650 void BufferView::replaceWord(string const & replacestring)
655 LyXText * tt = getLyXText();
657 update(tt, BufferView::SELECT|BufferView::FITCUR);
659 // clear the selection (if there is any)
660 toggleSelection(false);
661 update(tt, BufferView::SELECT|BufferView::FITCUR);
663 // clear the selection (if there is any)
664 toggleSelection(false);
665 tt->replaceSelectionWithString(this, replacestring);
667 tt->setSelectionOverString(this, replacestring);
669 // Go back so that replacement string is also spellchecked
670 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
671 tt->cursorLeft(this);
673 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
675 // End of spellchecker stuff
678 bool BufferView::lockInset(UpdatableInset * inset)
682 // don't relock if we're already locked
683 if (theLockingInset() == inset)
685 if (!theLockingInset()) {
686 // first check if it's the inset under the cursor we want lock
687 // should be most of the time
688 char const c = text->cursor.par()->getChar(text->cursor.pos());
689 if (c == Paragraph::META_INSET) {
690 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
692 theLockingInset(inset);
696 // Then do a deep look of the inset and lock the right one
697 int const id = inset->id();
698 ParagraphList::iterator pit = buffer()->paragraphs.begin();
699 ParagraphList::iterator pend = buffer()->paragraphs.end();
700 for (; pit != pend; ++pit) {
701 InsetList::iterator it = pit->insetlist.begin();
702 InsetList::iterator end = pit->insetlist.end();
703 for (; it != end; ++it) {
704 if (it.getInset() == inset) {
705 text->setCursorIntern(this, &*pit, it.getPos());
706 theLockingInset(inset);
709 if (it.getInset()->getInsetFromID(id)) {
710 text->setCursorIntern(this, &*pit, it.getPos());
711 it.getInset()->edit(this);
712 return theLockingInset()->lockInsetInInset(this, inset);
718 return theLockingInset()->lockInsetInInset(this, inset);
722 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
724 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
725 LyXCursor cursor = text->cursor;
726 Inset * locking_inset = theLockingInset()->getLockingInset();
728 if ((cursor.pos() - 1 >= 0) &&
729 cursor.par()->isInset(cursor.pos() - 1) &&
730 (cursor.par()->getInset(cursor.pos() - 1) ==
732 text->setCursor(this, cursor,
733 cursor.par(), cursor.pos() - 1);
734 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
735 LyXText * txt = getLyXText();
736 if (locking_inset->isTextInset() &&
737 locking_inset->lyxCode() != Inset::ERT_CODE &&
738 (txt->real_current_font.language() !=
739 buffer()->params.language
740 || txt->real_current_font.isVisibleRightToLeft()
741 != buffer()->params.language->RightToLeft()))
742 shape = (txt->real_current_font.isVisibleRightToLeft())
743 ? LyXScreen::REVERSED_L_SHAPE
744 : LyXScreen::L_SHAPE;
745 y += cursor.iy() + theLockingInset()->insetInInsetY();
746 screen().showManualCursor(text, x, y, asc, desc,
752 void BufferView::hideLockedInsetCursor()
754 if (theLockingInset() && available()) {
755 screen().hideCursor();
760 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
762 if (theLockingInset() && available()) {
763 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
764 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
773 int BufferView::unlockInset(UpdatableInset * inset)
777 if (inset && theLockingInset() == inset) {
778 inset->insetUnlock(this);
780 // make sure we update the combo !
781 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
782 // Tell the paragraph dialog that we changed paragraph
783 owner()->getDialogs().updateParagraph();
786 } else if (inset && theLockingInset() &&
787 theLockingInset()->unlockInsetInInset(this, inset)) {
788 // Tell the paragraph dialog that we changed paragraph
789 owner()->getDialogs().updateParagraph();
790 // owner inset has updated the layout combo
794 return bufferlist.unlockInset(inset);
798 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
800 if (!theLockingInset())
801 return; // shouldn't happen
802 if (kind == Undo::EDIT) // in this case insets would not be stored!
806 text->cursor.par()->next());
810 void BufferView::updateInset(Inset * inset, bool mark_dirty)
812 pimpl_->updateInset(inset, mark_dirty);
816 bool BufferView::ChangeInsets(Inset::Code code,
817 string const & from, string const & to)
819 bool need_update = false;
820 LyXCursor cursor = text->cursor;
821 LyXCursor tmpcursor = cursor;
822 cursor.par(tmpcursor.par());
823 cursor.pos(tmpcursor.pos());
825 ParIterator end = buffer()->par_iterator_end();
826 for (ParIterator it = buffer()->par_iterator_begin();
828 Paragraph * par = *it;
829 bool changed_inset = false;
830 for (InsetList::iterator it2 = par->insetlist.begin();
831 it2 != par->insetlist.end(); ++it2) {
832 if (it2.getInset()->lyxCode() == code) {
833 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
834 if (inset->getContents() == from) {
835 inset->setContents(to);
836 changed_inset = true;
845 // The test it.size()==1 was needed to prevent crashes.
846 // How to set the cursor corretly when it.size()>1 ??
847 if (it.size() == 1) {
848 text->setCursorIntern(this, par, 0);
849 text->redoParagraphs(this, text->cursor,
850 text->cursor.par()->next());
851 text->fullRebreak(this);
855 text->setCursorIntern(this, cursor.par(), cursor.pos());
860 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
862 // Check if the label 'from' appears more than once
863 vector<string> labels = buffer()->getLabelList();
865 if (lyx::count(labels.begin(), labels.end(), from) > 1)
868 return ChangeInsets(Inset::REF_CODE, from, to);
872 bool BufferView::ChangeCitationsIfUnique(string const & from,
875 typedef pair<string, string> StringPair;
877 vector<StringPair> keys = buffer()->getBibkeyList();
878 if (count_if(keys.begin(), keys.end(),
879 lyx::equal_1st_in_pair<StringPair>(from))
883 return ChangeInsets(Inset::CITE_CODE, from, to);
887 UpdatableInset * BufferView::theLockingInset() const
889 // If NULL is not allowed we should put an Assert here. (Lgb)
891 return text->the_locking_inset;
896 void BufferView::theLockingInset(UpdatableInset * inset)
898 text->the_locking_inset = inset;
902 LyXText * BufferView::getLyXText() const
904 if (theLockingInset()) {
905 LyXText * txt = theLockingInset()->getLyXText(this, true);
913 LyXText * BufferView::getParentText(Inset * inset) const
915 if (inset->owner()) {
916 LyXText * txt = inset->getLyXText(this);
917 inset = inset->owner();
918 while (inset && inset->getLyXText(this) == txt)
919 inset = inset->owner();
921 return inset->getLyXText(this);
927 Language const * BufferView::getParentLanguage(Inset * inset) const
929 LyXText * text = getParentText(inset);
930 return text->cursor.par()->getFontSettings(buffer()->params,
931 text->cursor.pos()).language();
935 Encoding const * BufferView::getEncoding() const
937 LyXText * t = getLyXText();
941 LyXCursor const & c= t->cursor;
942 LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
943 return font.language()->encoding();
947 void BufferView::haveSelection(bool sel)
949 pimpl_->workarea().haveSelection(sel);
953 int BufferView::workHeight() const
955 return pimpl_->workarea().workHeight();