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::showErrorList()
472 owner()->getDialogs().show("errorlist");
476 void BufferView::setCursorFromRow(int row)
481 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
483 Paragraph * texrowpar;
486 texrowpar = &*text->ownerParagraphs().begin();
489 texrowpar = &*buffer()->getParFromID(tmpid);
491 text->setCursor(texrowpar, tmppos);
495 bool BufferView::insertInset(Inset * inset, string const & lout)
497 return pimpl_->insertInset(inset, lout);
501 void BufferView::gotoLabel(string const & label)
503 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
504 it != buffer()->inset_iterator_end(); ++it) {
505 vector<string> labels = it->getLabelList();
506 if (find(labels.begin(),labels.end(),label)
509 text->setCursor(it.getPar(), it.getPos());
510 text->selection.cursor = text->cursor;
511 update(text, BufferView::SELECT);
518 void BufferView::undo()
523 owner()->message(_("Undo"));
525 update(text, BufferView::SELECT);
527 owner()->message(_("No further undo information"));
529 update(text, BufferView::SELECT);
534 void BufferView::redo()
539 owner()->message(_("Redo"));
541 update(text, BufferView::SELECT);
543 owner()->message(_("No further redo information"));
545 update(text, BufferView::SELECT);
550 // these functions are for the spellchecker
551 WordLangTuple const BufferView::nextWord(float & value)
555 return WordLangTuple();
558 return text->selectNextWordToSpellcheck(value);
562 void BufferView::selectLastWord()
567 LyXCursor cur = text->selection.cursor;
569 text->selection.cursor = cur;
570 text->selectSelectedWord();
571 toggleSelection(false);
572 update(text, BufferView::SELECT);
576 void BufferView::endOfSpellCheck()
578 if (!available()) return;
581 text->selectSelectedWord();
582 text->clearSelection();
583 update(text, BufferView::SELECT);
587 void BufferView::replaceWord(string const & replacestring)
592 LyXText * tt = getLyXText();
593 update(tt, BufferView::SELECT);
595 // clear the selection (if there is any)
596 toggleSelection(false);
597 update(tt, BufferView::SELECT);
599 // clear the selection (if there is any)
600 toggleSelection(false);
601 tt->replaceSelectionWithString(replacestring);
603 tt->setSelectionRange(replacestring.length());
605 // Go back so that replacement string is also spellchecked
606 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
607 tt->cursorLeft(this);
609 update(tt, BufferView::SELECT);
611 // FIXME: should be done through LFUN
612 buffer()->markDirty();
615 // End of spellchecker stuff
618 bool BufferView::lockInset(UpdatableInset * inset)
622 // don't relock if we're already locked
623 if (theLockingInset() == inset)
625 if (!theLockingInset()) {
626 // first check if it's the inset under the cursor we want lock
627 // should be most of the time
628 if (text->cursor.pos() < text->cursor.par()->size()
629 && text->cursor.par()->getChar(text->cursor.pos()) ==
630 Paragraph::META_INSET) {
631 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
633 theLockingInset(inset);
637 // Then do a deep look of the inset and lock the right one
638 int const id = inset->id();
639 ParagraphList::iterator pit = buffer()->paragraphs.begin();
640 ParagraphList::iterator pend = buffer()->paragraphs.end();
641 for (; pit != pend; ++pit) {
642 InsetList::iterator it = pit->insetlist.begin();
643 InsetList::iterator end = pit->insetlist.end();
644 for (; it != end; ++it) {
645 if (it.getInset() == inset) {
646 text->setCursorIntern(pit, it.getPos());
647 theLockingInset(inset);
650 if (it.getInset()->getInsetFromID(id)) {
651 text->setCursorIntern(pit, it.getPos());
652 it.getInset()->edit(this);
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();