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 "ParagraphParameters.h"
18 #include "WordLangTuple.h"
20 #include "bufferlist.h"
23 #include "iterators.h"
25 #include "lyxcursor.h"
28 #include "undo_funcs.h"
30 #include "paragraph_funcs.h"
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/inseterror.h"
40 #include "insets/updatableinset.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::reload()
109 string const fn = buffer()->fileName();
110 if (bufferlist.close(buffer(), false))
111 buffer(bufferlist.loadLyXFile(fn));
115 void BufferView::resize()
117 if (pimpl_->buffer_) {
118 pimpl_->resizeCurrentBuffer();
123 void BufferView::repaint()
129 bool BufferView::fitCursor()
131 return pimpl_->fitCursor();
135 void BufferView::update()
141 void BufferView::updateScrollbar()
143 pimpl_->updateScrollbar();
147 void BufferView::scrollDocView(int value)
149 pimpl_->scrollDocView(value);
153 void BufferView::redoCurrentBuffer()
155 pimpl_->redoCurrentBuffer();
159 bool BufferView::available() const
161 return pimpl_->available();
165 Change const BufferView::getCurrentChange()
167 return pimpl_->getCurrentChange();
171 void BufferView::beforeChange(LyXText * text)
173 pimpl_->beforeChange(text);
177 void BufferView::savePosition(unsigned int i)
179 pimpl_->savePosition(i);
183 void BufferView::restorePosition(unsigned int i)
185 pimpl_->restorePosition(i);
189 bool BufferView::isSavedPosition(unsigned int i)
191 return pimpl_->isSavedPosition(i);
195 void BufferView::update(LyXText * text, UpdateCodes f)
197 pimpl_->update(text, f);
201 void BufferView::update(UpdateCodes f)
207 void BufferView::switchKeyMap()
209 pimpl_->switchKeyMap();
213 void BufferView::insetUnlock()
215 pimpl_->insetUnlock();
219 int BufferView::workWidth() const
221 return pimpl_->workarea().workWidth();
225 void BufferView::toggleSelection(bool b)
227 pimpl_->toggleSelection(b);
231 void BufferView::toggleToggle()
233 pimpl_->toggleToggle();
237 void BufferView::center()
243 string const BufferView::getClipboard() const
245 return pimpl_->workarea().getClipboard();
249 void BufferView::stuffClipboard(string const & stuff) const
251 pimpl_->stuffClipboard(stuff);
255 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
256 BufferView::UpdateCodes uc2)
258 return static_cast<BufferView::UpdateCodes>
259 (static_cast<int>(uc1) | static_cast<int>(uc2));
263 bool BufferView::dispatch(FuncRequest const & ev)
265 return pimpl_->dispatch(ev);
269 void BufferView::scroll(int lines)
271 pimpl_->scroll(lines);
275 // Inserts a file into current document
276 bool BufferView::insertLyXFile(string const & filen)
278 // Copyright CHT Software Service GmbH
281 // Insert a LyXformat - file into current buffer
283 // Moved from lyx_cb.C (Lgb)
288 string const fname = MakeAbsPath(filen);
290 // check if file exist
291 FileInfo const fi(fname);
293 if (!fi.readable()) {
294 string const file = MakeDisplayPath(fname, 50);
296 bformat(_("The specified document\n%1$s\ncould not be read."), file);
297 Alert::error(_("Could not read document"), text);
303 ifstream ifs(fname.c_str());
305 string const file = MakeDisplayPath(fname, 50);
307 bformat(_("Could not open the specified document\n"), file);
308 Alert::error(_("Could not open file"), text);
312 int const c = ifs.peek();
320 // FIXME: huh ? No we won't !
321 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
322 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
324 lyxerr[Debug::INFO] << "Will insert file without header"
326 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
334 bool BufferView::removeAutoInsets()
336 // keep track of which pos and par the cursor was on
337 Paragraph * cursor_par = &*text->cursor.par();
338 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
339 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
340 pos_type cursor_pos = text->cursor.pos();
344 // Trap the deletion of the paragraph the cursor is in.
345 // Iterate until we find a paragraph that won't be immediately deleted.
346 // In reality this should mean we only execute the body of the while
347 // loop once at most. However for safety we iterate rather than just
348 // make this an if () conditional.
349 while ((cursor_par_prev || cursor_par_next)
351 cursor_par_prev ? cursor_par_prev : cursor_par_next,
353 // We just removed cursor_par so have to fix the "cursor"
354 if (cursor_par_prev) {
358 cursor_par = cursor_par_prev;
359 cursor_pos = cursor_par->size();
363 cursor_par = cursor_par_next;
366 cursor_par_prev = cursor_par->previous();
367 cursor_par_next = cursor_par->next();
370 // Iterate through the paragraphs removing autoDelete insets as we go.
371 // If the paragraph ends up empty after all the autoDelete insets are
372 // removed that paragraph will be removed by the next setCursor() call.
373 ParIterator it = buffer()->par_iterator_begin();
374 ParIterator end = buffer()->par_iterator_end();
375 for (; it != end; ++it) {
376 Paragraph * par = *it;
377 Paragraph * par_prev = par ? par->previous() : 0;
378 bool removed = false;
380 if (text->setCursor(par, 0)
381 && cursor_par == par_prev) {
382 // The previous setCursor line was deleted and that
383 // was the cursor_par line. This can only happen if an
384 // error box was the sole item on cursor_par.
385 // It is possible for cursor_par_prev to be stray if
386 // the line it pointed to only had a error box on it
387 // so we have to set it to a known correct value.
388 // This is often the same value it already had.
389 cursor_par_prev = par->previous();
390 if (cursor_par_prev) {
391 // '|' = par, '.' = cursor_par, 'E' = error box
392 // First step below may occur before while{}
394 // E -> .E -> |.E -> . -> |b
397 cursor_par = cursor_par_prev;
398 cursor_pos = cursor_par_prev->size();
399 cursor_par_prev = cursor_par->previous();
400 // cursor_par_next remains the same
401 } else if (cursor_par_next) {
402 // First step below may occur before while{}
404 // E -> |.E -> |. -> . -> .|a
406 cursor_par = cursor_par_next;
408 // cursor_par_prev remains unset
409 cursor_par_next = cursor_par->next();
411 // I can't find a way to trigger this
412 // so it should be unreachable code
413 // unless the buffer is corrupted.
414 lyxerr << "BufferView::removeAutoInsets() is bad\n";
418 InsetList::iterator pit = par->insetlist.begin();
419 InsetList::iterator pend = par->insetlist.end();
421 while (pit != pend) {
422 if (pit.getInset()->autoDelete()) {
424 pos_type const pos = pit.getPos();
427 // We just invalidated par's inset iterators so
428 // we get the next valid iterator position
429 pit = par->insetlist.insetIterator(pos);
430 // and ensure we have a valid end iterator.
431 pend = par->insetlist.end();
433 if (cursor_par == par) {
434 // update the saved cursor position
435 if (cursor_pos > pos)
444 text->redoParagraph();
448 // It is possible that the last line is empty if it was cursor_par
449 // and/or only had an error inset on it. So we set the cursor to the
450 // start of the doc to force its removal and ensure a valid saved cursor
451 if (text->setCursor(&*text->ownerParagraphs().begin(), 0)
452 && 0 == cursor_par_next) {
453 cursor_par = cursor_par_prev;
454 cursor_pos = cursor_par->size();
455 } else if (cursor_pos > cursor_par->size()) {
456 // Some C-Enter lines were removed by the setCursor call which
457 // then invalidated cursor_pos. It could still be "wrong" because
458 // the cursor may appear to have jumped but since we collapsed
459 // some C-Enter lines this should be a reasonable compromise.
460 cursor_pos = cursor_par->size();
463 // restore the original cursor in its corrected location.
464 text->setCursorIntern(cursor_par, cursor_pos);
470 void BufferView::insertErrors(TeXErrors & terr)
472 // Save the cursor position
473 LyXCursor cursor = text->cursor;
475 TeXErrors::Errors::const_iterator cit = terr.begin();
476 TeXErrors::Errors::const_iterator end = terr.end();
477 for (; cit != end; ++cit) {
478 string const desctext(cit->error_desc);
479 string const errortext(cit->error_text);
480 string const msgtxt = desctext + '\n' + errortext;
481 int const errorrow = cit->error_in_line;
483 // Insert error string for row number
487 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
488 buffer()->texrow.increasePos(tmpid, tmppos);
491 Paragraph * texrowpar = 0;
494 texrowpar = &*text->ownerParagraphs().begin();
497 texrowpar = &*buffer()->getParFromID(tmpid);
504 InsetError * new_inset = new InsetError(msgtxt);
505 text->setCursorIntern(texrowpar, tmppos);
506 text->insertInset(new_inset);
510 // Restore the cursor position
511 text->setCursorIntern(cursor.par(), cursor.pos());
515 void BufferView::setCursorFromRow(int row)
520 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
522 Paragraph * texrowpar;
525 texrowpar = &*text->ownerParagraphs().begin();
528 texrowpar = &*buffer()->getParFromID(tmpid);
530 text->setCursor(texrowpar, tmppos);
534 bool BufferView::insertInset(Inset * inset, string const & lout)
536 return pimpl_->insertInset(inset, lout);
540 void 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(it.getPar(), it.getPos());
549 text->selection.cursor = text->cursor;
550 update(text, BufferView::SELECT);
557 void BufferView::undo()
562 owner()->message(_("Undo"));
564 update(text, BufferView::SELECT);
566 owner()->message(_("No further undo information"));
568 update(text, BufferView::SELECT);
573 void BufferView::redo()
578 owner()->message(_("Redo"));
580 update(text, BufferView::SELECT);
582 owner()->message(_("No further redo information"));
584 update(text, BufferView::SELECT);
589 // these functions are for the spellchecker
590 WordLangTuple const BufferView::nextWord(float & value)
594 return WordLangTuple();
597 return text->selectNextWordToSpellcheck(value);
601 void BufferView::selectLastWord()
606 LyXCursor cur = text->selection.cursor;
608 text->selection.cursor = cur;
609 text->selectSelectedWord();
610 toggleSelection(false);
611 update(text, BufferView::SELECT);
615 void BufferView::endOfSpellCheck()
617 if (!available()) return;
620 text->selectSelectedWord();
621 text->clearSelection();
622 update(text, BufferView::SELECT);
626 void BufferView::replaceWord(string const & replacestring)
631 LyXText * tt = getLyXText();
632 update(tt, BufferView::SELECT);
634 // clear the selection (if there is any)
635 toggleSelection(false);
636 update(tt, BufferView::SELECT);
638 // clear the selection (if there is any)
639 toggleSelection(false);
640 tt->replaceSelectionWithString(replacestring);
642 tt->setSelectionRange(replacestring.length());
644 // Go back so that replacement string is also spellchecked
645 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
646 tt->cursorLeft(this);
648 update(tt, BufferView::SELECT);
650 // FIXME: should be done through LFUN
651 buffer()->markDirty();
654 // End of spellchecker stuff
657 bool BufferView::lockInset(UpdatableInset * inset)
661 // don't relock if we're already locked
662 if (theLockingInset() == inset)
664 if (!theLockingInset()) {
665 // first check if it's the inset under the cursor we want lock
666 // should be most of the time
667 if (text->cursor.pos() < text->cursor.par()->size()
668 && text->cursor.par()->getChar(text->cursor.pos()) ==
669 Paragraph::META_INSET) {
670 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
672 theLockingInset(inset);
676 // Then do a deep look of the inset and lock the right one
677 int const id = inset->id();
678 ParagraphList::iterator pit = buffer()->paragraphs.begin();
679 ParagraphList::iterator pend = buffer()->paragraphs.end();
680 for (; pit != pend; ++pit) {
681 InsetList::iterator it = pit->insetlist.begin();
682 InsetList::iterator end = pit->insetlist.end();
683 for (; it != end; ++it) {
684 if (it.getInset() == inset) {
685 text->setCursorIntern(pit, it.getPos());
686 theLockingInset(inset);
689 if (it.getInset()->getInsetFromID(id)) {
690 text->setCursorIntern(pit, it.getPos());
691 it.getInset()->edit(this);
692 return theLockingInset()->lockInsetInInset(this, inset);
698 return theLockingInset()->lockInsetInInset(this, inset);
702 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
704 if (theLockingInset() && available()) {
705 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
706 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
715 void BufferView::hideCursor()
717 screen().hideCursor();
721 int BufferView::unlockInset(UpdatableInset * inset)
725 if (inset && theLockingInset() == inset) {
726 inset->insetUnlock(this);
728 // make sure we update the combo !
729 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
730 // Tell the paragraph dialog that we changed paragraph
731 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
734 } else if (inset && theLockingInset() &&
735 theLockingInset()->unlockInsetInInset(this, inset)) {
736 // Tell the paragraph dialog that we changed paragraph
737 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
738 // owner inset has updated the layout combo
746 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
748 if (!theLockingInset())
749 return; // shouldn't happen
750 if (kind == Undo::EDIT) // in this case insets would not be stored!
754 boost::next(text->cursor.par()));
758 void BufferView::updateInset(Inset * inset)
760 pimpl_->updateInset(inset);
764 bool BufferView::ChangeInsets(Inset::Code code,
765 string const & from, string const & to)
767 bool need_update = false;
768 LyXCursor cursor = text->cursor;
769 LyXCursor tmpcursor = cursor;
770 cursor.par(tmpcursor.par());
771 cursor.pos(tmpcursor.pos());
773 ParIterator end = buffer()->par_iterator_end();
774 for (ParIterator it = buffer()->par_iterator_begin();
776 Paragraph * par = *it;
777 bool changed_inset = false;
778 for (InsetList::iterator it2 = par->insetlist.begin();
779 it2 != par->insetlist.end(); ++it2) {
780 if (it2.getInset()->lyxCode() == code) {
781 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
782 if (inset->getContents() == from) {
783 inset->setContents(to);
784 changed_inset = true;
793 // The test it.size()==1 was needed to prevent crashes.
794 // How to set the cursor corretly when it.size()>1 ??
795 if (it.size() == 1) {
796 text->setCursorIntern(par, 0);
797 text->redoParagraphs(text->cursor,
798 boost::next(text->cursor.par()));
803 text->setCursorIntern(cursor.par(), cursor.pos());
808 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
810 // Check if the label 'from' appears more than once
811 vector<string> labels = buffer()->getLabelList();
813 if (lyx::count(labels.begin(), labels.end(), from) > 1)
816 return ChangeInsets(Inset::REF_CODE, from, to);
820 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
822 typedef pair<string, string> StringPair;
824 vector<StringPair> keys;
825 buffer()->fillWithBibKeys(keys);
826 if (count_if(keys.begin(), keys.end(),
827 lyx::equal_1st_in_pair<StringPair>(from))
831 return ChangeInsets(Inset::CITE_CODE, from, to);
835 UpdatableInset * BufferView::theLockingInset() const
837 // If NULL is not allowed we should put an Assert here. (Lgb)
839 return text->the_locking_inset;
844 void BufferView::theLockingInset(UpdatableInset * inset)
846 text->the_locking_inset = inset;
850 LyXText * BufferView::getLyXText() const
852 if (theLockingInset()) {
853 LyXText * txt = theLockingInset()->getLyXText(this, true);
861 LyXText * BufferView::getParentText(Inset * inset) const
863 if (inset->owner()) {
864 LyXText * txt = inset->getLyXText(this);
865 inset = inset->owner();
866 while (inset && inset->getLyXText(this) == txt)
867 inset = inset->owner();
869 return inset->getLyXText(this);
875 Language const * BufferView::getParentLanguage(Inset * inset) const
877 LyXText * text = getParentText(inset);
878 return text->cursor.par()->getFontSettings(buffer()->params,
879 text->cursor.pos()).language();
883 Encoding const * BufferView::getEncoding() const
885 LyXText * t = getLyXText();
889 LyXCursor const & c = t->cursor;
890 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
891 outerFont(c.par(), t->ownerParagraphs()));
892 return font.language()->encoding();
896 void BufferView::haveSelection(bool sel)
898 pimpl_->workarea().haveSelection(sel);
902 int BufferView::workHeight() const
904 return pimpl_->workarea().workHeight();