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"
16 #include "ParagraphParameters.h"
17 #include "WordLangTuple.h"
19 #include "bufferlist.h"
22 #include "errorlist.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 %1$s\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::resetErrorList()
472 pimpl_->errorlist_.clear();
476 void BufferView::setErrorList(ErrorList const & el)
478 pimpl_->errorlist_ = el;
482 void BufferView::addError(ErrorItem const & ei)
484 pimpl_->errorlist_.push_back(ei);
489 void BufferView::showErrorList(string const & action) const
491 if (getErrorList().size()) {
492 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
493 owner()->getDialogs().show("errorlist", title);
499 BufferView::getErrorList() const
501 return pimpl_->errorlist_;
505 void BufferView::setCursorFromRow(int row)
510 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
512 Paragraph * texrowpar;
515 texrowpar = &*text->ownerParagraphs().begin();
518 texrowpar = &*buffer()->getParFromID(tmpid);
520 text->setCursor(texrowpar, tmppos);
524 bool BufferView::insertInset(Inset * inset, string const & lout)
526 return pimpl_->insertInset(inset, lout);
530 void BufferView::gotoLabel(string const & label)
532 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
533 it != buffer()->inset_iterator_end(); ++it) {
534 vector<string> labels = it->getLabelList();
535 if (find(labels.begin(),labels.end(),label)
538 text->setCursor(it.getPar(), it.getPos());
539 text->selection.cursor = text->cursor;
540 update(text, BufferView::SELECT);
547 void BufferView::undo()
552 owner()->message(_("Undo"));
554 update(text, BufferView::SELECT);
556 owner()->message(_("No further undo information"));
558 update(text, BufferView::SELECT);
563 void BufferView::redo()
568 owner()->message(_("Redo"));
570 update(text, BufferView::SELECT);
572 owner()->message(_("No further redo information"));
574 update(text, BufferView::SELECT);
579 // these functions are for the spellchecker
580 WordLangTuple const BufferView::nextWord(float & value)
584 return WordLangTuple();
587 return text->selectNextWordToSpellcheck(value);
591 void BufferView::selectLastWord()
596 LyXCursor cur = text->selection.cursor;
598 text->selection.cursor = cur;
599 text->selectSelectedWord();
600 toggleSelection(false);
601 update(text, BufferView::SELECT);
605 void BufferView::endOfSpellCheck()
607 if (!available()) return;
610 text->selectSelectedWord();
611 text->clearSelection();
612 update(text, BufferView::SELECT);
616 void BufferView::replaceWord(string const & replacestring)
621 LyXText * tt = getLyXText();
622 update(tt, BufferView::SELECT);
624 // clear the selection (if there is any)
625 toggleSelection(false);
626 update(tt, BufferView::SELECT);
628 // clear the selection (if there is any)
629 toggleSelection(false);
630 tt->replaceSelectionWithString(replacestring);
632 tt->setSelectionRange(replacestring.length());
634 // Go back so that replacement string is also spellchecked
635 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
636 tt->cursorLeft(this);
638 update(tt, BufferView::SELECT);
640 // FIXME: should be done through LFUN
641 buffer()->markDirty();
644 // End of spellchecker stuff
647 bool BufferView::lockInset(UpdatableInset * inset)
651 // don't relock if we're already locked
652 if (theLockingInset() == inset)
654 if (!theLockingInset()) {
655 // first check if it's the inset under the cursor we want lock
656 // should be most of the time
657 if (text->cursor.pos() < text->cursor.par()->size()
658 && text->cursor.par()->getChar(text->cursor.pos()) ==
659 Paragraph::META_INSET) {
660 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
662 theLockingInset(inset);
666 // Then do a deep look of the inset and lock the right one
667 int const id = inset->id();
668 ParagraphList::iterator pit = buffer()->paragraphs.begin();
669 ParagraphList::iterator pend = buffer()->paragraphs.end();
670 for (; pit != pend; ++pit) {
671 InsetList::iterator it = pit->insetlist.begin();
672 InsetList::iterator end = pit->insetlist.end();
673 for (; it != end; ++it) {
674 if (it.getInset() == inset) {
675 text->setCursorIntern(pit, it.getPos());
676 theLockingInset(inset);
679 if (it.getInset()->getInsetFromID(id)) {
680 text->setCursorIntern(pit, it.getPos());
681 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
682 it.getInset()->localDispatch(cmd);
683 return theLockingInset()->lockInsetInInset(this, inset);
689 return theLockingInset()->lockInsetInInset(this, inset);
693 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
695 if (theLockingInset() && available()) {
696 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
697 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
706 void BufferView::hideCursor()
708 screen().hideCursor();
712 int BufferView::unlockInset(UpdatableInset * inset)
716 if (inset && theLockingInset() == inset) {
717 inset->insetUnlock(this);
719 // make sure we update the combo !
720 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
721 // Tell the paragraph dialog that we changed paragraph
722 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
725 } else if (inset && theLockingInset() &&
726 theLockingInset()->unlockInsetInInset(this, inset)) {
727 // Tell the paragraph dialog that we changed paragraph
728 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
729 // owner inset has updated the layout combo
737 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
739 if (!theLockingInset())
740 return; // shouldn't happen
741 if (kind == Undo::EDIT) // in this case insets would not be stored!
745 boost::next(text->cursor.par()));
749 void BufferView::updateInset(Inset * inset)
751 pimpl_->updateInset(inset);
755 bool BufferView::ChangeInsets(Inset::Code code,
756 string const & from, string const & to)
758 bool need_update = false;
759 LyXCursor cursor = text->cursor;
760 LyXCursor tmpcursor = cursor;
761 cursor.par(tmpcursor.par());
762 cursor.pos(tmpcursor.pos());
764 ParIterator end = buffer()->par_iterator_end();
765 for (ParIterator it = buffer()->par_iterator_begin();
767 Paragraph * par = *it;
768 bool changed_inset = false;
769 for (InsetList::iterator it2 = par->insetlist.begin();
770 it2 != par->insetlist.end(); ++it2) {
771 if (it2.getInset()->lyxCode() == code) {
772 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
773 if (inset->getContents() == from) {
774 inset->setContents(to);
775 changed_inset = true;
784 // The test it.size()==1 was needed to prevent crashes.
785 // How to set the cursor corretly when it.size()>1 ??
786 if (it.size() == 1) {
787 text->setCursorIntern(par, 0);
788 text->redoParagraphs(text->cursor,
789 boost::next(text->cursor.par()));
794 text->setCursorIntern(cursor.par(), cursor.pos());
799 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
801 // Check if the label 'from' appears more than once
802 vector<string> labels = buffer()->getLabelList();
804 if (lyx::count(labels.begin(), labels.end(), from) > 1)
807 return ChangeInsets(Inset::REF_CODE, from, to);
811 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
813 typedef pair<string, string> StringPair;
815 vector<StringPair> keys;
816 buffer()->fillWithBibKeys(keys);
817 if (count_if(keys.begin(), keys.end(),
818 lyx::equal_1st_in_pair<StringPair>(from))
822 return ChangeInsets(Inset::CITE_CODE, from, to);
826 UpdatableInset * BufferView::theLockingInset() const
828 // If NULL is not allowed we should put an Assert here. (Lgb)
830 return text->the_locking_inset;
835 void BufferView::theLockingInset(UpdatableInset * inset)
837 text->the_locking_inset = inset;
841 LyXText * BufferView::getLyXText() const
843 if (theLockingInset()) {
844 LyXText * txt = theLockingInset()->getLyXText(this, true);
852 LyXText * BufferView::getParentText(Inset * inset) const
854 if (inset->owner()) {
855 LyXText * txt = inset->getLyXText(this);
856 inset = inset->owner();
857 while (inset && inset->getLyXText(this) == txt)
858 inset = inset->owner();
860 return inset->getLyXText(this);
866 Language const * BufferView::getParentLanguage(Inset * inset) const
868 LyXText * text = getParentText(inset);
869 return text->cursor.par()->getFontSettings(buffer()->params,
870 text->cursor.pos()).language();
874 Encoding const * BufferView::getEncoding() const
876 LyXText * t = getLyXText();
880 LyXCursor const & c = t->cursor;
881 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
882 outerFont(c.par(), t->ownerParagraphs()));
883 return font.language()->encoding();
887 void BufferView::haveSelection(bool sel)
889 pimpl_->workarea().haveSelection(sel);
893 int BufferView::workHeight() const
895 return pimpl_->workarea().workHeight();