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 "iterators.h"
24 #include "lyxcursor.h"
27 #include "undo_funcs.h"
29 #include "paragraph_funcs.h"
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/WorkArea.h"
35 #include "frontends/screen.h"
37 #include "insets/insetcommand.h" // ChangeRefs
38 #include "insets/inseterror.h"
39 #include "insets/updatableinset.h"
41 #include "support/FileInfo.h"
42 #include "support/filetools.h"
43 #include "support/lyxfunctional.h" // equal_1st_in_pair
44 #include "support/types.h"
45 #include "support/lyxalgo.h" // lyx_count
49 extern BufferList bufferlist;
61 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
62 int width, int height)
63 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
69 BufferView::~BufferView()
76 Buffer * BufferView::buffer() const
78 return pimpl_->buffer_;
82 LyXScreen & BufferView::screen() const
84 return pimpl_->screen();
88 LyXView * BufferView::owner() const
90 return pimpl_->owner_;
94 Painter & BufferView::painter() const
96 return pimpl_->painter();
100 void BufferView::buffer(Buffer * b)
106 void BufferView::reload()
108 string const fn = buffer()->fileName();
109 if (bufferlist.close(buffer(), false))
110 buffer(bufferlist.loadLyXFile(fn));
114 void BufferView::resize()
116 if (pimpl_->buffer_) {
117 pimpl_->resizeCurrentBuffer();
122 void BufferView::repaint()
128 bool BufferView::fitCursor()
130 return pimpl_->fitCursor();
134 void BufferView::update()
140 void BufferView::updateScrollbar()
142 pimpl_->updateScrollbar();
146 void BufferView::scrollDocView(int value)
148 pimpl_->scrollDocView(value);
152 void BufferView::redoCurrentBuffer()
154 pimpl_->redoCurrentBuffer();
158 bool BufferView::available() const
160 return pimpl_->available();
164 Change const BufferView::getCurrentChange()
166 return pimpl_->getCurrentChange();
170 void BufferView::beforeChange(LyXText * text)
172 pimpl_->beforeChange(text);
176 void BufferView::savePosition(unsigned int i)
178 pimpl_->savePosition(i);
182 void BufferView::restorePosition(unsigned int i)
184 pimpl_->restorePosition(i);
188 bool BufferView::isSavedPosition(unsigned int i)
190 return pimpl_->isSavedPosition(i);
194 void BufferView::update(LyXText * text, UpdateCodes f)
196 pimpl_->update(text, f);
200 void BufferView::update(UpdateCodes f)
206 void BufferView::switchKeyMap()
208 pimpl_->switchKeyMap();
212 void BufferView::insetUnlock()
214 pimpl_->insetUnlock();
218 int BufferView::workWidth() const
220 return pimpl_->workarea().workWidth();
224 void BufferView::toggleSelection(bool b)
226 pimpl_->toggleSelection(b);
230 void BufferView::toggleToggle()
232 pimpl_->toggleToggle();
236 void BufferView::center()
242 string const BufferView::getClipboard() const
244 return pimpl_->workarea().getClipboard();
248 void BufferView::stuffClipboard(string const & stuff) const
250 pimpl_->stuffClipboard(stuff);
254 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
255 BufferView::UpdateCodes uc2)
257 return static_cast<BufferView::UpdateCodes>
258 (static_cast<int>(uc1) | static_cast<int>(uc2));
262 bool BufferView::dispatch(FuncRequest const & ev)
264 return pimpl_->dispatch(ev);
268 void BufferView::scroll(int lines)
270 pimpl_->scroll(lines);
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 string const file = MakeDisplayPath(fname, 50);
295 bformat(_("The specified document\n%1$s\ncould not be read."), file);
296 Alert::error(_("Could not read document"), text);
302 ifstream ifs(fname.c_str());
304 string const file = MakeDisplayPath(fname, 50);
306 bformat(_("Could not open the specified document %1$s\n"), file);
307 Alert::error(_("Could not open file"), text);
311 int const c = ifs.peek();
319 // FIXME: huh ? No we won't !
320 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
321 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
323 lyxerr[Debug::INFO] << "Will insert file without header"
325 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
333 bool BufferView::removeAutoInsets()
335 // keep track of which pos and par the cursor was on
336 Paragraph * cursor_par = &*text->cursor.par();
337 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
338 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
339 pos_type cursor_pos = text->cursor.pos();
343 // Trap the deletion of the paragraph the cursor is in.
344 // Iterate until we find a paragraph that won't be immediately deleted.
345 // In reality this should mean we only execute the body of the while
346 // loop once at most. However for safety we iterate rather than just
347 // make this an if () conditional.
348 while ((cursor_par_prev || cursor_par_next)
350 cursor_par_prev ? cursor_par_prev : cursor_par_next,
352 // We just removed cursor_par so have to fix the "cursor"
353 if (cursor_par_prev) {
357 cursor_par = cursor_par_prev;
358 cursor_pos = cursor_par->size();
362 cursor_par = cursor_par_next;
365 cursor_par_prev = cursor_par->previous();
366 cursor_par_next = cursor_par->next();
369 // Iterate through the paragraphs removing autoDelete insets as we go.
370 // If the paragraph ends up empty after all the autoDelete insets are
371 // removed that paragraph will be removed by the next setCursor() call.
372 ParIterator it = buffer()->par_iterator_begin();
373 ParIterator end = buffer()->par_iterator_end();
374 for (; it != end; ++it) {
375 Paragraph * par = *it;
376 Paragraph * par_prev = par ? par->previous() : 0;
377 bool removed = false;
379 if (text->setCursor(par, 0)
380 && cursor_par == par_prev) {
381 // The previous setCursor line was deleted and that
382 // was the cursor_par line. This can only happen if an
383 // error box was the sole item on cursor_par.
384 // It is possible for cursor_par_prev to be stray if
385 // the line it pointed to only had a error box on it
386 // so we have to set it to a known correct value.
387 // This is often the same value it already had.
388 cursor_par_prev = par->previous();
389 if (cursor_par_prev) {
390 // '|' = par, '.' = cursor_par, 'E' = error box
391 // First step below may occur before while{}
393 // E -> .E -> |.E -> . -> |b
396 cursor_par = cursor_par_prev;
397 cursor_pos = cursor_par_prev->size();
398 cursor_par_prev = cursor_par->previous();
399 // cursor_par_next remains the same
400 } else if (cursor_par_next) {
401 // First step below may occur before while{}
403 // E -> |.E -> |. -> . -> .|a
405 cursor_par = cursor_par_next;
407 // cursor_par_prev remains unset
408 cursor_par_next = cursor_par->next();
410 // I can't find a way to trigger this
411 // so it should be unreachable code
412 // unless the buffer is corrupted.
413 lyxerr << "BufferView::removeAutoInsets() is bad\n";
417 InsetList::iterator pit = par->insetlist.begin();
418 InsetList::iterator pend = par->insetlist.end();
420 while (pit != pend) {
421 if (pit.getInset()->autoDelete()) {
423 pos_type const pos = pit.getPos();
426 // We just invalidated par's inset iterators so
427 // we get the next valid iterator position
428 pit = par->insetlist.insetIterator(pos);
429 // and ensure we have a valid end iterator.
430 pend = par->insetlist.end();
432 if (cursor_par == par) {
433 // update the saved cursor position
434 if (cursor_pos > pos)
443 text->redoParagraph();
447 // It is possible that the last line is empty if it was cursor_par
448 // and/or only had an error inset on it. So we set the cursor to the
449 // start of the doc to force its removal and ensure a valid saved cursor
450 if (text->setCursor(&*text->ownerParagraphs().begin(), 0)
451 && 0 == cursor_par_next) {
452 cursor_par = cursor_par_prev;
453 cursor_pos = cursor_par->size();
454 } else if (cursor_pos > cursor_par->size()) {
455 // Some C-Enter lines were removed by the setCursor call which
456 // then invalidated cursor_pos. It could still be "wrong" because
457 // the cursor may appear to have jumped but since we collapsed
458 // some C-Enter lines this should be a reasonable compromise.
459 cursor_pos = cursor_par->size();
462 // restore the original cursor in its corrected location.
463 text->setCursorIntern(cursor_par, cursor_pos);
469 void BufferView::showErrorList()
471 owner()->getDialogs().show("errorlist");
475 void BufferView::setCursorFromRow(int row)
480 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
482 Paragraph * texrowpar;
485 texrowpar = &*text->ownerParagraphs().begin();
488 texrowpar = &*buffer()->getParFromID(tmpid);
490 text->setCursor(texrowpar, tmppos);
494 bool BufferView::insertInset(Inset * inset, string const & lout)
496 return pimpl_->insertInset(inset, lout);
500 void BufferView::gotoLabel(string const & label)
502 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
503 it != buffer()->inset_iterator_end(); ++it) {
504 vector<string> labels = it->getLabelList();
505 if (find(labels.begin(),labels.end(),label)
508 text->setCursor(it.getPar(), it.getPos());
509 text->selection.cursor = text->cursor;
510 update(text, BufferView::SELECT);
517 void BufferView::undo()
522 owner()->message(_("Undo"));
524 update(text, BufferView::SELECT);
526 owner()->message(_("No further undo information"));
528 update(text, BufferView::SELECT);
533 void BufferView::redo()
538 owner()->message(_("Redo"));
540 update(text, BufferView::SELECT);
542 owner()->message(_("No further redo information"));
544 update(text, BufferView::SELECT);
549 // these functions are for the spellchecker
550 WordLangTuple const BufferView::nextWord(float & value)
554 return WordLangTuple();
557 return text->selectNextWordToSpellcheck(value);
561 void BufferView::selectLastWord()
566 LyXCursor cur = text->selection.cursor;
568 text->selection.cursor = cur;
569 text->selectSelectedWord();
570 toggleSelection(false);
571 update(text, BufferView::SELECT);
575 void BufferView::endOfSpellCheck()
577 if (!available()) return;
580 text->selectSelectedWord();
581 text->clearSelection();
582 update(text, BufferView::SELECT);
586 void BufferView::replaceWord(string const & replacestring)
591 LyXText * tt = getLyXText();
592 update(tt, BufferView::SELECT);
594 // clear the selection (if there is any)
595 toggleSelection(false);
596 update(tt, BufferView::SELECT);
598 // clear the selection (if there is any)
599 toggleSelection(false);
600 tt->replaceSelectionWithString(replacestring);
602 tt->setSelectionRange(replacestring.length());
604 // Go back so that replacement string is also spellchecked
605 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
606 tt->cursorLeft(this);
608 update(tt, BufferView::SELECT);
610 // FIXME: should be done through LFUN
611 buffer()->markDirty();
614 // End of spellchecker stuff
617 bool BufferView::lockInset(UpdatableInset * inset)
621 // don't relock if we're already locked
622 if (theLockingInset() == inset)
624 if (!theLockingInset()) {
625 // first check if it's the inset under the cursor we want lock
626 // should be most of the time
627 if (text->cursor.pos() < text->cursor.par()->size()
628 && text->cursor.par()->getChar(text->cursor.pos()) ==
629 Paragraph::META_INSET) {
630 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
632 theLockingInset(inset);
636 // Then do a deep look of the inset and lock the right one
637 int const id = inset->id();
638 ParagraphList::iterator pit = buffer()->paragraphs.begin();
639 ParagraphList::iterator pend = buffer()->paragraphs.end();
640 for (; pit != pend; ++pit) {
641 InsetList::iterator it = pit->insetlist.begin();
642 InsetList::iterator end = pit->insetlist.end();
643 for (; it != end; ++it) {
644 if (it.getInset() == inset) {
645 text->setCursorIntern(pit, it.getPos());
646 theLockingInset(inset);
649 if (it.getInset()->getInsetFromID(id)) {
650 text->setCursorIntern(pit, it.getPos());
651 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
652 it.getInset()->localDispatch(cmd);
653 return theLockingInset()->lockInsetInInset(this, inset);
659 return theLockingInset()->lockInsetInInset(this, inset);
663 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
665 if (theLockingInset() && available()) {
666 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
667 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
676 void BufferView::hideCursor()
678 screen().hideCursor();
682 int BufferView::unlockInset(UpdatableInset * inset)
686 if (inset && theLockingInset() == inset) {
687 inset->insetUnlock(this);
689 // make sure we update the combo !
690 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
691 // Tell the paragraph dialog that we changed paragraph
692 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
695 } else if (inset && theLockingInset() &&
696 theLockingInset()->unlockInsetInInset(this, inset)) {
697 // Tell the paragraph dialog that we changed paragraph
698 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
699 // owner inset has updated the layout combo
707 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
709 if (!theLockingInset())
710 return; // shouldn't happen
711 if (kind == Undo::EDIT) // in this case insets would not be stored!
715 boost::next(text->cursor.par()));
719 void BufferView::updateInset(Inset * inset)
721 pimpl_->updateInset(inset);
725 bool BufferView::ChangeInsets(Inset::Code code,
726 string const & from, string const & to)
728 bool need_update = false;
729 LyXCursor cursor = text->cursor;
730 LyXCursor tmpcursor = cursor;
731 cursor.par(tmpcursor.par());
732 cursor.pos(tmpcursor.pos());
734 ParIterator end = buffer()->par_iterator_end();
735 for (ParIterator it = buffer()->par_iterator_begin();
737 Paragraph * par = *it;
738 bool changed_inset = false;
739 for (InsetList::iterator it2 = par->insetlist.begin();
740 it2 != par->insetlist.end(); ++it2) {
741 if (it2.getInset()->lyxCode() == code) {
742 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
743 if (inset->getContents() == from) {
744 inset->setContents(to);
745 changed_inset = true;
754 // The test it.size()==1 was needed to prevent crashes.
755 // How to set the cursor corretly when it.size()>1 ??
756 if (it.size() == 1) {
757 text->setCursorIntern(par, 0);
758 text->redoParagraphs(text->cursor,
759 boost::next(text->cursor.par()));
764 text->setCursorIntern(cursor.par(), cursor.pos());
769 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
771 // Check if the label 'from' appears more than once
772 vector<string> labels = buffer()->getLabelList();
774 if (lyx::count(labels.begin(), labels.end(), from) > 1)
777 return ChangeInsets(Inset::REF_CODE, from, to);
781 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
783 typedef pair<string, string> StringPair;
785 vector<StringPair> keys;
786 buffer()->fillWithBibKeys(keys);
787 if (count_if(keys.begin(), keys.end(),
788 lyx::equal_1st_in_pair<StringPair>(from))
792 return ChangeInsets(Inset::CITE_CODE, from, to);
796 UpdatableInset * BufferView::theLockingInset() const
798 // If NULL is not allowed we should put an Assert here. (Lgb)
800 return text->the_locking_inset;
805 void BufferView::theLockingInset(UpdatableInset * inset)
807 text->the_locking_inset = inset;
811 LyXText * BufferView::getLyXText() const
813 if (theLockingInset()) {
814 LyXText * txt = theLockingInset()->getLyXText(this, true);
822 LyXText * BufferView::getParentText(Inset * inset) const
824 if (inset->owner()) {
825 LyXText * txt = inset->getLyXText(this);
826 inset = inset->owner();
827 while (inset && inset->getLyXText(this) == txt)
828 inset = inset->owner();
830 return inset->getLyXText(this);
836 Language const * BufferView::getParentLanguage(Inset * inset) const
838 LyXText * text = getParentText(inset);
839 return text->cursor.par()->getFontSettings(buffer()->params,
840 text->cursor.pos()).language();
844 Encoding const * BufferView::getEncoding() const
846 LyXText * t = getLyXText();
850 LyXCursor const & c = t->cursor;
851 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
852 outerFont(c.par(), t->ownerParagraphs()));
853 return font.language()->encoding();
857 void BufferView::haveSelection(bool sel)
859 pimpl_->workarea().haveSelection(sel);
863 int BufferView::workHeight() const
865 return pimpl_->workarea().workHeight();