3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braustein
7 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "BufferView.h"
18 #include "BufferView_pimpl.h"
21 #include "ParagraphParameters.h"
22 #include "WordLangTuple.h"
24 #include "bufferlist.h"
27 #include "errorlist.h"
28 #include "iterators.h"
30 #include "lyxcursor.h"
33 #include "undo_funcs.h"
35 #include "paragraph_funcs.h"
37 #include "frontends/Alert.h"
38 #include "frontends/Dialogs.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/WorkArea.h"
41 #include "frontends/screen.h"
43 #include "insets/insetcommand.h" // ChangeRefs
44 #include "insets/updatableinset.h"
46 #include "support/FileInfo.h"
47 #include "support/filetools.h"
48 #include "support/types.h"
49 #include "support/lyxalgo.h" // lyx_count
53 extern BufferList bufferlist;
56 using namespace lyx::support;
66 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
67 int width, int height)
68 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
74 BufferView::~BufferView()
81 Buffer * BufferView::buffer() const
83 return pimpl_->buffer_;
87 LyXScreen & BufferView::screen() const
89 return pimpl_->screen();
93 LyXView * BufferView::owner() const
95 return pimpl_->owner_;
99 Painter & BufferView::painter() const
101 return pimpl_->painter();
105 void BufferView::buffer(Buffer * b)
111 bool BufferView::newFile(string const & fn, string const & tn, bool named)
113 return pimpl_->newFile(fn, tn, named);
117 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 return pimpl_->loadLyXFile(fn, tl);
123 void BufferView::reload()
125 string const fn = buffer()->fileName();
126 if (bufferlist.close(buffer(), false))
131 void BufferView::resize()
134 pimpl_->resizeCurrentBuffer();
138 bool BufferView::fitCursor()
140 return pimpl_->fitCursor();
144 void BufferView::update()
150 void BufferView::updateScrollbar()
152 pimpl_->updateScrollbar();
156 void BufferView::scrollDocView(int value)
158 pimpl_->scrollDocView(value);
162 void BufferView::redoCurrentBuffer()
164 pimpl_->redoCurrentBuffer();
168 bool BufferView::available() const
170 return pimpl_->available();
174 Change const BufferView::getCurrentChange()
176 return pimpl_->getCurrentChange();
180 void BufferView::beforeChange(LyXText * text)
182 pimpl_->beforeChange(text);
186 void BufferView::savePosition(unsigned int i)
188 pimpl_->savePosition(i);
192 void BufferView::restorePosition(unsigned int i)
194 pimpl_->restorePosition(i);
198 bool BufferView::isSavedPosition(unsigned int i)
200 return pimpl_->isSavedPosition(i);
204 void BufferView::switchKeyMap()
206 pimpl_->switchKeyMap();
210 void BufferView::insetUnlock()
212 pimpl_->insetUnlock();
216 int BufferView::workWidth() const
218 return pimpl_->workarea().workWidth();
222 void BufferView::center()
228 string const BufferView::getClipboard() const
230 return pimpl_->workarea().getClipboard();
234 void BufferView::stuffClipboard(string const & stuff) const
236 pimpl_->stuffClipboard(stuff);
240 bool BufferView::dispatch(FuncRequest const & ev)
242 return pimpl_->dispatch(ev);
246 void BufferView::scroll(int lines)
248 pimpl_->scroll(lines);
252 // Inserts a file into current document
253 bool BufferView::insertLyXFile(string const & filen)
255 // Copyright CHT Software Service GmbH
258 // Insert a LyXformat - file into current buffer
260 // Moved from lyx_cb.C (Lgb)
265 string const fname = MakeAbsPath(filen);
267 // check if file exist
268 FileInfo const fi(fname);
270 if (!fi.readable()) {
271 string const file = MakeDisplayPath(fname, 50);
273 bformat(_("The specified document\n%1$s\ncould not be read."), file);
274 Alert::error(_("Could not read document"), text);
280 ifstream ifs(fname.c_str());
282 string const file = MakeDisplayPath(fname, 50);
284 bformat(_("Could not open the specified document %1$s\n"), file);
285 Alert::error(_("Could not open file"), text);
289 int const c = ifs.peek();
296 text->breakParagraph(buffer()->paragraphs);
299 // FIXME: huh ? No we won't !
300 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
301 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
303 lyxerr[Debug::INFO] << "Will insert file without header"
305 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
313 void BufferView::showErrorList(string const & action) const
315 if (getErrorList().size()) {
316 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
317 owner()->getDialogs().show("errorlist", title);
318 pimpl_->errorlist_.clear();
324 BufferView::getErrorList() const
326 return pimpl_->errorlist_;
330 void BufferView::setCursorFromRow(int row)
335 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
337 ParagraphList::iterator texrowpar;
340 texrowpar = text->ownerParagraphs().begin();
343 texrowpar = buffer()->getParFromID(tmpid).pit();
345 text->setCursor(texrowpar, tmppos);
349 bool BufferView::insertInset(InsetOld * inset, string const & lout)
351 return pimpl_->insertInset(inset, lout);
355 void BufferView::gotoLabel(string const & label)
357 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
358 it != buffer()->inset_iterator_end(); ++it) {
359 vector<string> labels;
360 it->getLabelList(labels);
361 if (find(labels.begin(),labels.end(),label) != labels.end()) {
363 text->setCursor(it.getPar(), it.getPos());
364 text->selection.cursor = text->cursor;
372 void BufferView::undo()
377 owner()->message(_("Undo"));
380 owner()->message(_("No further undo information"));
386 void BufferView::redo()
391 owner()->message(_("Redo"));
394 owner()->message(_("No further redo information"));
400 // these functions are for the spellchecker
401 WordLangTuple const BufferView::nextWord(float & value)
405 return WordLangTuple();
408 return text->selectNextWordToSpellcheck(value);
412 void BufferView::selectLastWord()
417 LyXCursor cur = text->selection.cursor;
419 text->selection.cursor = cur;
420 text->selectSelectedWord();
425 void BufferView::endOfSpellCheck()
427 if (!available()) return;
430 text->selectSelectedWord();
431 text->clearSelection();
436 void BufferView::replaceWord(string const & replacestring)
441 LyXText * tt = getLyXText();
443 tt->replaceSelectionWithString(replacestring);
444 tt->setSelectionRange(replacestring.length());
446 // Go back so that replacement string is also spellchecked
447 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
448 tt->cursorLeft(this);
450 // FIXME: should be done through LFUN
451 buffer()->markDirty();
456 bool BufferView::lockInset(UpdatableInset * inset)
460 // don't relock if we're already locked
461 if (theLockingInset() == inset)
463 if (!theLockingInset()) {
464 // first check if it's the inset under the cursor we want lock
465 // should be most of the time
466 if (text->cursor.pos() < text->cursor.par()->size()
467 && text->cursor.par()->getChar(text->cursor.pos()) ==
468 Paragraph::META_INSET) {
469 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
471 theLockingInset(inset);
475 // Then do a deep look of the inset and lock the right one
476 int const id = inset->id();
477 ParagraphList::iterator pit = buffer()->paragraphs.begin();
478 ParagraphList::iterator pend = buffer()->paragraphs.end();
479 for (; pit != pend; ++pit) {
480 InsetList::iterator it = pit->insetlist.begin();
481 InsetList::iterator end = pit->insetlist.end();
482 for (; it != end; ++it) {
483 if (it->inset == inset) {
484 text->setCursorIntern(pit, it->pos);
485 theLockingInset(inset);
488 if (it->inset->getInsetFromID(id)) {
489 text->setCursorIntern(pit, it->pos);
490 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
491 it->inset->localDispatch(cmd);
492 return theLockingInset()->lockInsetInInset(this, inset);
498 return theLockingInset()->lockInsetInInset(this, inset);
502 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
504 if (theLockingInset() && available()) {
505 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
506 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
515 void BufferView::hideCursor()
517 screen().hideCursor();
521 int BufferView::unlockInset(UpdatableInset * inset)
525 if (inset && theLockingInset() == inset) {
526 inset->insetUnlock(this);
528 // make sure we update the combo !
529 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
530 // Tell the paragraph dialog that we changed paragraph
531 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
534 } else if (inset && theLockingInset() &&
535 theLockingInset()->unlockInsetInInset(this, inset)) {
536 // Tell the paragraph dialog that we changed paragraph
537 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
538 // owner inset has updated the layout combo
546 void BufferView::updateInset()
548 pimpl_->updateInset();
552 bool BufferView::ChangeInsets(InsetOld::Code code,
553 string const & from, string const & to)
555 bool need_update = false;
556 LyXCursor cursor = text->cursor;
557 LyXCursor tmpcursor = cursor;
558 cursor.par(tmpcursor.par());
559 cursor.pos(tmpcursor.pos());
561 ParIterator end = buffer()->par_iterator_end();
562 for (ParIterator it = buffer()->par_iterator_begin();
564 bool changed_inset = false;
565 for (InsetList::iterator it2 = it->insetlist.begin();
566 it2 != it->insetlist.end(); ++it2) {
567 if (it2->inset->lyxCode() == code) {
568 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
569 if (inset->getContents() == from) {
570 inset->setContents(to);
571 changed_inset = true;
580 // The test it.size()==1 was needed to prevent crashes.
581 // How to set the cursor corretly when it.size()>1 ??
582 if (it.size() == 1) {
583 text->setCursorIntern(it.pit(), 0);
584 text->redoParagraph(text->cursor.par());
588 text->setCursorIntern(cursor.par(), cursor.pos());
593 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
595 // Check if the label 'from' appears more than once
596 vector<string> labels;
597 buffer()->getLabelList(labels);
599 if (lyx::count(labels.begin(), labels.end(), from) > 1)
602 return ChangeInsets(InsetOld::REF_CODE, from, to);
606 UpdatableInset * BufferView::theLockingInset() const
608 // If NULL is not allowed we should put an Assert here. (Lgb)
610 return text->the_locking_inset;
615 void BufferView::theLockingInset(UpdatableInset * inset)
617 text->the_locking_inset = inset;
621 LyXText * BufferView::getLyXText() const
623 if (theLockingInset()) {
624 LyXText * txt = theLockingInset()->getLyXText(this, true);
632 LyXText * BufferView::getParentText(InsetOld * inset) const
634 if (inset->owner()) {
635 LyXText * txt = inset->getLyXText(this);
636 inset = inset->owner();
637 while (inset && inset->getLyXText(this) == txt)
638 inset = inset->owner();
640 return inset->getLyXText(this);
646 Language const * BufferView::getParentLanguage(InsetOld * inset) const
648 LyXText * text = getParentText(inset);
649 return text->cursor.par()->getFontSettings(buffer()->params,
650 text->cursor.pos()).language();
654 Encoding const * BufferView::getEncoding() const
656 LyXText * t = getLyXText();
660 LyXCursor const & c = t->cursor;
661 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
662 outerFont(c.par(), t->ownerParagraphs()));
663 return font.language()->encoding();
667 void BufferView::haveSelection(bool sel)
669 pimpl_->workarea().haveSelection(sel);
673 int BufferView::workHeight() const
675 return pimpl_->workarea().workHeight();