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::alert(_("Error!"),
304 _("Cannot open specified file: "),
305 MakeDisplayPath(fname, 50));
309 int const c = ifs.peek();
317 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
318 res = buffer()->readFile(lex, fname, text->cursor.par());
320 lyxerr[Debug::INFO] << "Will insert file without header"
322 res = buffer()->readLyXformat2(lex, text->cursor.par());
330 bool BufferView::removeAutoInsets()
332 // keep track of which pos and par the cursor was on
333 Paragraph * cursor_par = text->cursor.par();
334 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
335 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
336 pos_type cursor_pos = text->cursor.pos();
340 // Trap the deletion of the paragraph the cursor is in.
341 // Iterate until we find a paragraph that won't be immediately deleted.
342 // In reality this should mean we only execute the body of the while
343 // loop once at most. However for safety we iterate rather than just
344 // make this an if () conditional.
345 while ((cursor_par_prev || cursor_par_next)
346 && text->setCursor(this,
347 cursor_par_prev ? cursor_par_prev : cursor_par_next,
349 // We just removed cursor_par so have to fix the "cursor"
350 if (cursor_par_prev) {
354 cursor_par = cursor_par_prev;
355 cursor_pos = cursor_par->size();
359 cursor_par = cursor_par_next;
362 cursor_par_prev = cursor_par->previous();
363 cursor_par_next = cursor_par->next();
366 // Iterate through the paragraphs removing autoDelete insets as we go.
367 // If the paragraph ends up empty after all the autoDelete insets are
368 // removed that paragraph will be removed by the next setCursor() call.
369 ParIterator it = buffer()->par_iterator_begin();
370 ParIterator end = buffer()->par_iterator_end();
371 for (; it != end; ++it) {
372 Paragraph * par = *it;
373 Paragraph * par_prev = par ? par->previous() : 0;
374 bool removed = false;
376 if (text->setCursor(this, par, 0)
377 && cursor_par == par_prev) {
378 // The previous setCursor line was deleted and that
379 // was the cursor_par line. This can only happen if an
380 // error box was the sole item on cursor_par.
381 // It is possible for cursor_par_prev to be stray if
382 // the line it pointed to only had a error box on it
383 // so we have to set it to a known correct value.
384 // This is often the same value it already had.
385 cursor_par_prev = par->previous();
386 if (cursor_par_prev) {
387 // '|' = par, '.' = cursor_par, 'E' = error box
388 // First step below may occur before while{}
390 // E -> .E -> |.E -> . -> |b
393 cursor_par = cursor_par_prev;
394 cursor_pos = cursor_par_prev->size();
395 cursor_par_prev = cursor_par->previous();
396 // cursor_par_next remains the same
397 } else if (cursor_par_next) {
398 // First step below may occur before while{}
400 // E -> |.E -> |. -> . -> .|a
402 cursor_par = cursor_par_next;
404 // cursor_par_prev remains unset
405 cursor_par_next = cursor_par->next();
407 // I can't find a way to trigger this
408 // so it should be unreachable code
409 // unless the buffer is corrupted.
410 lyxerr << "BufferView::removeAutoInsets() is bad\n";
414 InsetList::iterator pit = par->insetlist.begin();
415 InsetList::iterator pend = par->insetlist.end();
417 while (pit != pend) {
418 if (pit.getInset()->autoDelete()) {
420 pos_type const pos = pit.getPos();
423 // We just invalidated par's inset iterators so
424 // we get the next valid iterator position
425 pit = par->insetlist.insetIterator(pos);
426 // and ensure we have a valid end iterator.
427 pend = par->insetlist.end();
429 if (cursor_par == par) {
430 // update the saved cursor position
431 if (cursor_pos > pos)
440 text->redoParagraph(this);
444 // It is possible that the last line is empty if it was cursor_par
445 // and/or only had an error inset on it. So we set the cursor to the
446 // start of the doc to force its removal and ensure a valid saved cursor
447 if (text->setCursor(this, text->ownerParagraph(), 0)
448 && 0 == cursor_par_next) {
449 cursor_par = cursor_par_prev;
450 cursor_pos = cursor_par->size();
451 } else if (cursor_pos > cursor_par->size()) {
452 // Some C-Enter lines were removed by the setCursor call which
453 // then invalidated cursor_pos. It could still be "wrong" because
454 // the cursor may appear to have jumped but since we collapsed
455 // some C-Enter lines this should be a reasonable compromise.
456 cursor_pos = cursor_par->size();
459 // restore the original cursor in its corrected location.
460 text->setCursorIntern(this, cursor_par, cursor_pos);
466 void BufferView::insertErrors(TeXErrors & terr)
468 // Save the cursor position
469 LyXCursor cursor = text->cursor;
471 TeXErrors::Errors::const_iterator cit = terr.begin();
472 TeXErrors::Errors::const_iterator end = terr.end();
473 for (; cit != end; ++cit) {
474 string const desctext(cit->error_desc);
475 string const errortext(cit->error_text);
476 string const msgtxt = desctext + '\n' + errortext;
477 int const errorrow = cit->error_in_line;
479 // Insert error string for row number
483 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
484 buffer()->texrow.increasePos(tmpid, tmppos);
487 Paragraph * texrowpar = 0;
490 texrowpar = text->ownerParagraph();
493 texrowpar = buffer()->getParFromID(tmpid);
500 InsetError * new_inset = new InsetError(msgtxt);
501 text->setCursorIntern(this, texrowpar, tmppos);
502 text->insertInset(this, new_inset);
503 text->fullRebreak(this);
506 // Restore the cursor position
507 text->setCursorIntern(this, cursor.par(), cursor.pos());
511 void BufferView::setCursorFromRow(int row)
516 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
518 Paragraph * texrowpar;
521 texrowpar = text->ownerParagraph();
524 texrowpar = buffer()->getParFromID(tmpid);
526 text->setCursor(this, texrowpar, tmppos);
530 bool BufferView::insertInset(Inset * inset, string const & lout)
532 return pimpl_->insertInset(inset, lout);
536 // This is also a buffer property (ale)
537 // Not so sure about that. a goto Label function can not be buffer local, just
538 // think how this will work in a multiwindow/buffer environment, all the
539 // cursors in all the views showing this buffer will move. (Lgb)
540 // OK, then no cursor action should be allowed in buffer. (ale)
541 bool BufferView::gotoLabel(string const & label)
543 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
544 it != buffer()->inset_iterator_end(); ++it) {
545 vector<string> labels = it->getLabelList();
546 if (find(labels.begin(),labels.end(),label)
549 text->setCursor(this, it.getPar(), it.getPos());
550 text->selection.cursor = text->cursor;
551 update(text, BufferView::SELECT|BufferView::FITCUR);
559 void BufferView::undo()
564 owner()->message(_("Undo"));
567 update(text, BufferView::SELECT|BufferView::FITCUR);
569 owner()->message(_("No further undo information"));
571 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
576 void BufferView::redo()
581 owner()->message(_("Redo"));
584 update(text, BufferView::SELECT|BufferView::FITCUR);
586 owner()->message(_("No further redo information"));
588 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
593 void BufferView::copyEnvironment()
596 text->copyEnvironmentType();
597 owner()->message(_("Paragraph environment type copied"));
602 void BufferView::pasteEnvironment()
605 text->pasteEnvironmentType(this);
606 owner()->message(_("Paragraph environment type set"));
607 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
612 // these functions are for the spellchecker
613 WordLangTuple const BufferView::nextWord(float & value)
617 return WordLangTuple();
620 return text->selectNextWordToSpellcheck(this, value);
624 void BufferView::selectLastWord()
629 LyXCursor cur = text->selection.cursor;
632 text->selection.cursor = cur;
633 text->selectSelectedWord(this);
634 toggleSelection(false);
635 update(text, BufferView::SELECT|BufferView::FITCUR);
639 void BufferView::endOfSpellCheck()
641 if (!available()) return;
645 text->selectSelectedWord(this);
646 text->clearSelection();
647 update(text, BufferView::SELECT|BufferView::FITCUR);
651 void BufferView::replaceWord(string const & replacestring)
656 LyXText * tt = getLyXText();
658 update(tt, BufferView::SELECT|BufferView::FITCUR);
660 // clear the selection (if there is any)
661 toggleSelection(false);
662 update(tt, BufferView::SELECT|BufferView::FITCUR);
664 // clear the selection (if there is any)
665 toggleSelection(false);
666 tt->replaceSelectionWithString(this, replacestring);
668 tt->setSelectionOverString(this, replacestring);
670 // Go back so that replacement string is also spellchecked
671 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
672 tt->cursorLeft(this);
674 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
676 // End of spellchecker stuff
679 bool BufferView::lockInset(UpdatableInset * inset)
683 // don't relock if we're already locked
684 if (theLockingInset() == inset)
686 if (!theLockingInset()) {
687 // first check if it's the inset under the cursor we want lock
688 // should be most of the time
689 char const c = text->cursor.par()->getChar(text->cursor.pos());
690 if (c == Paragraph::META_INSET) {
691 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
693 theLockingInset(inset);
697 // Then do a deep look of the inset and lock the right one
698 int const id = inset->id();
699 ParagraphList::iterator pit = buffer()->paragraphs.begin();
700 ParagraphList::iterator pend = buffer()->paragraphs.end();
701 for (; pit != pend; ++pit) {
702 InsetList::iterator it = pit->insetlist.begin();
703 InsetList::iterator end = pit->insetlist.end();
704 for (; it != end; ++it) {
705 if (it.getInset() == inset) {
706 text->setCursorIntern(this, &*pit, it.getPos());
707 theLockingInset(inset);
710 if (it.getInset()->getInsetFromID(id)) {
711 text->setCursorIntern(this, &*pit, it.getPos());
712 it.getInset()->edit(this);
713 return theLockingInset()->lockInsetInInset(this, inset);
719 return theLockingInset()->lockInsetInInset(this, inset);
723 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
725 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
726 LyXCursor cursor = text->cursor;
727 Inset * locking_inset = theLockingInset()->getLockingInset();
729 if ((cursor.pos() - 1 >= 0) &&
730 cursor.par()->isInset(cursor.pos() - 1) &&
731 (cursor.par()->getInset(cursor.pos() - 1) ==
733 text->setCursor(this, cursor,
734 cursor.par(), cursor.pos() - 1);
735 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
736 LyXText * txt = getLyXText();
737 if (locking_inset->isTextInset() &&
738 locking_inset->lyxCode() != Inset::ERT_CODE &&
739 (txt->real_current_font.language() !=
740 buffer()->params.language
741 || txt->real_current_font.isVisibleRightToLeft()
742 != buffer()->params.language->RightToLeft()))
743 shape = (txt->real_current_font.isVisibleRightToLeft())
744 ? LyXScreen::REVERSED_L_SHAPE
745 : LyXScreen::L_SHAPE;
746 y += cursor.iy() + theLockingInset()->insetInInsetY();
747 screen().showManualCursor(text, x, y, asc, desc,
753 void BufferView::hideLockedInsetCursor()
755 if (theLockingInset() && available()) {
756 screen().hideCursor();
761 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
763 if (theLockingInset() && available()) {
764 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
765 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
774 int BufferView::unlockInset(UpdatableInset * inset)
778 if (inset && theLockingInset() == inset) {
779 inset->insetUnlock(this);
781 // make sure we update the combo !
782 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
783 // Tell the paragraph dialog that we changed paragraph
784 owner()->getDialogs().updateParagraph();
787 } else if (inset && theLockingInset() &&
788 theLockingInset()->unlockInsetInInset(this, inset)) {
789 // Tell the paragraph dialog that we changed paragraph
790 owner()->getDialogs().updateParagraph();
791 // owner inset has updated the layout combo
795 return bufferlist.unlockInset(inset);
799 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
801 if (!theLockingInset())
802 return; // shouldn't happen
803 if (kind == Undo::EDIT) // in this case insets would not be stored!
807 text->cursor.par()->next());
811 void BufferView::updateInset(Inset * inset, bool mark_dirty)
813 pimpl_->updateInset(inset, mark_dirty);
817 bool BufferView::ChangeInsets(Inset::Code code,
818 string const & from, string const & to)
820 bool need_update = false;
821 LyXCursor cursor = text->cursor;
822 LyXCursor tmpcursor = cursor;
823 cursor.par(tmpcursor.par());
824 cursor.pos(tmpcursor.pos());
826 ParIterator end = buffer()->par_iterator_end();
827 for (ParIterator it = buffer()->par_iterator_begin();
829 Paragraph * par = *it;
830 bool changed_inset = false;
831 for (InsetList::iterator it2 = par->insetlist.begin();
832 it2 != par->insetlist.end(); ++it2) {
833 if (it2.getInset()->lyxCode() == code) {
834 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
835 if (inset->getContents() == from) {
836 inset->setContents(to);
837 changed_inset = true;
846 // The test it.size()==1 was needed to prevent crashes.
847 // How to set the cursor corretly when it.size()>1 ??
848 if (it.size() == 1) {
849 text->setCursorIntern(this, par, 0);
850 text->redoParagraphs(this, text->cursor,
851 text->cursor.par()->next());
852 text->fullRebreak(this);
856 text->setCursorIntern(this, cursor.par(), cursor.pos());
861 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
863 // Check if the label 'from' appears more than once
864 vector<string> labels = buffer()->getLabelList();
866 if (lyx::count(labels.begin(), labels.end(), from) > 1)
869 return ChangeInsets(Inset::REF_CODE, from, to);
873 bool BufferView::ChangeCitationsIfUnique(string const & from,
876 typedef pair<string, string> StringPair;
878 vector<StringPair> keys = buffer()->getBibkeyList();
879 if (count_if(keys.begin(), keys.end(),
880 lyx::equal_1st_in_pair<StringPair>(from))
884 return ChangeInsets(Inset::CITE_CODE, from, to);
888 UpdatableInset * BufferView::theLockingInset() const
890 // If NULL is not allowed we should put an Assert here. (Lgb)
892 return text->the_locking_inset;
897 void BufferView::theLockingInset(UpdatableInset * inset)
899 text->the_locking_inset = inset;
903 LyXText * BufferView::getLyXText() const
905 if (theLockingInset()) {
906 LyXText * txt = theLockingInset()->getLyXText(this, true);
914 LyXText * BufferView::getParentText(Inset * inset) const
916 if (inset->owner()) {
917 LyXText * txt = inset->getLyXText(this);
918 inset = inset->owner();
919 while (inset && inset->getLyXText(this) == txt)
920 inset = inset->owner();
922 return inset->getLyXText(this);
928 Language const * BufferView::getParentLanguage(Inset * inset) const
930 LyXText * text = getParentText(inset);
931 return text->cursor.par()->getFontSettings(buffer()->params,
932 text->cursor.pos()).language();
936 Encoding const * BufferView::getEncoding() const
938 LyXText * t = getLyXText();
942 LyXCursor const & c= t->cursor;
943 LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
944 return font.language()->encoding();
948 void BufferView::haveSelection(bool sel)
950 pimpl_->workarea().haveSelection(sel);
954 int BufferView::workHeight() const
956 return pimpl_->workarea().workHeight();