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"
19 #include "bufferlist.h"
20 #include "BufferView_pimpl.h"
21 #include "funcrequest.h"
23 #include "iterators.h"
25 #include "lyxlayout.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
30 #include "undo_funcs.h"
31 #include "WordLangTuple.h"
33 #include "frontends/Alert.h"
34 #include "frontends/Dialogs.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/screen.h"
37 #include "frontends/WorkArea.h"
39 #include "insets/insetcommand.h" // ChangeRefs
40 #include "insets/updatableinset.h"
42 #include "support/filetools.h"
43 #include "support/lyxalgo.h" // lyx_count
46 extern BufferList bufferlist;
48 using namespace lyx::support;
54 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
55 int width, int height)
56 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
62 BufferView::~BufferView()
69 Buffer * BufferView::buffer() const
71 return pimpl_->buffer_;
75 LyXScreen & BufferView::screen() const
77 return pimpl_->screen();
81 LyXView * BufferView::owner() const
83 return pimpl_->owner_;
87 Painter & BufferView::painter() const
89 return pimpl_->painter();
93 void BufferView::buffer(Buffer * b)
99 bool BufferView::newFile(string const & fn, string const & tn, bool named)
101 return pimpl_->newFile(fn, tn, named);
105 bool BufferView::loadLyXFile(string const & fn, bool tl)
107 return pimpl_->loadLyXFile(fn, tl);
111 void BufferView::reload()
113 string const fn = buffer()->fileName();
114 if (bufferlist.close(buffer(), false))
119 void BufferView::resize()
122 pimpl_->resizeCurrentBuffer();
126 bool BufferView::fitCursor()
128 return pimpl_->fitCursor();
132 void BufferView::update()
138 void BufferView::updateScrollbar()
140 pimpl_->updateScrollbar();
144 void BufferView::scrollDocView(int value)
146 pimpl_->scrollDocView(value);
150 void BufferView::redoCurrentBuffer()
152 pimpl_->redoCurrentBuffer();
156 bool BufferView::available() const
158 return pimpl_->available();
162 Change const BufferView::getCurrentChange()
164 return pimpl_->getCurrentChange();
168 void BufferView::beforeChange(LyXText * text)
170 pimpl_->beforeChange(text);
174 void BufferView::savePosition(unsigned int i)
176 pimpl_->savePosition(i);
180 void BufferView::restorePosition(unsigned int i)
182 pimpl_->restorePosition(i);
186 bool BufferView::isSavedPosition(unsigned int i)
188 return pimpl_->isSavedPosition(i);
192 void BufferView::switchKeyMap()
194 pimpl_->switchKeyMap();
198 void BufferView::insetUnlock()
200 pimpl_->insetUnlock();
204 int BufferView::workWidth() const
206 return pimpl_->workarea().workWidth();
210 void BufferView::center()
216 int BufferView::top_y() const
218 return pimpl_->top_y();
222 void BufferView::top_y(int y)
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)
262 BOOST_ASSERT(!filen.empty());
264 string const fname = MakeAbsPath(filen);
268 text->breakParagraph(buffer()->paragraphs());
270 bool res = buffer()->readFile(fname, text->cursor.par());
277 void BufferView::showErrorList(string const & action) const
279 if (getErrorList().size()) {
280 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
281 owner()->getDialogs().show("errorlist", title);
282 pimpl_->errorlist_.clear();
288 BufferView::getErrorList() const
290 return pimpl_->errorlist_;
294 void BufferView::setCursorFromRow(int row)
299 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
301 ParagraphList::iterator texrowpar;
304 texrowpar = text->ownerParagraphs().begin();
307 texrowpar = buffer()->getParFromID(tmpid).pit();
309 text->setCursor(texrowpar, tmppos);
313 bool BufferView::insertInset(InsetOld * inset, string const & lout)
315 return pimpl_->insertInset(inset, lout);
319 void BufferView::gotoLabel(string const & label)
321 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
322 it != buffer()->inset_iterator_end(); ++it) {
323 vector<string> labels;
324 it->getLabelList(labels);
325 if (find(labels.begin(),labels.end(),label) != labels.end()) {
327 text->setCursor(it.getPar(), it.getPos());
328 text->selection.cursor = text->cursor;
336 void BufferView::undo()
341 owner()->message(_("Undo"));
344 owner()->message(_("No further undo information"));
350 void BufferView::redo()
355 owner()->message(_("Redo"));
358 owner()->message(_("No further redo information"));
364 // these functions are for the spellchecker
365 WordLangTuple const BufferView::nextWord(float & value)
369 return WordLangTuple();
372 return text->selectNextWordToSpellcheck(value);
376 void BufferView::selectLastWord()
381 LyXCursor cur = text->selection.cursor;
383 text->selection.cursor = cur;
384 text->selectSelectedWord();
389 void BufferView::endOfSpellCheck()
391 if (!available()) return;
394 text->selectSelectedWord();
395 text->clearSelection();
400 void BufferView::replaceWord(string const & replacestring)
405 LyXText * tt = getLyXText();
407 tt->replaceSelectionWithString(replacestring);
408 tt->setSelectionRange(replacestring.length());
410 // Go back so that replacement string is also spellchecked
411 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
412 tt->cursorLeft(this);
414 // FIXME: should be done through LFUN
415 buffer()->markDirty();
420 bool BufferView::lockInset(UpdatableInset * inset)
424 // don't relock if we're already locked
425 if (theLockingInset() == inset)
427 if (!theLockingInset()) {
428 // first check if it's the inset under the cursor we want lock
429 // should be most of the time
430 if (text->cursor.pos() < text->cursor.par()->size()
431 && text->cursor.par()->getChar(text->cursor.pos()) ==
432 Paragraph::META_INSET) {
433 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
435 theLockingInset(inset);
439 // Then do a deep look of the inset and lock the right one
440 int const id = inset->id();
441 ParagraphList::iterator pit = buffer()->paragraphs().begin();
442 ParagraphList::iterator pend = buffer()->paragraphs().end();
443 for (; pit != pend; ++pit) {
444 InsetList::iterator it = pit->insetlist.begin();
445 InsetList::iterator end = pit->insetlist.end();
446 for (; it != end; ++it) {
447 if (it->inset == inset) {
448 text->setCursorIntern(pit, it->pos);
449 theLockingInset(inset);
452 if (it->inset->getInsetFromID(id)) {
453 text->setCursorIntern(pit, it->pos);
454 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
455 it->inset->localDispatch(cmd);
456 return theLockingInset()->lockInsetInInset(this, inset);
462 return theLockingInset()->lockInsetInInset(this, inset);
466 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
468 if (theLockingInset() && available()) {
469 y += text->cursor.y() + theLockingInset()->insetInInsetY();
470 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
479 void BufferView::hideCursor()
481 screen().hideCursor();
485 int BufferView::unlockInset(UpdatableInset * inset)
489 if (inset && theLockingInset() == inset) {
490 inset->insetUnlock(this);
492 // make sure we update the combo !
493 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
494 // Tell the paragraph dialog that we changed paragraph
495 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
498 } else if (inset && theLockingInset() &&
499 theLockingInset()->unlockInsetInInset(this, inset)) {
500 // Tell the paragraph dialog that we changed paragraph
501 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
502 // owner inset has updated the layout combo
510 void BufferView::updateInset(InsetOld const * inset)
512 pimpl_->updateInset(inset);
516 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
518 // Check if the label 'from' appears more than once
519 vector<string> labels;
520 buffer()->getLabelList(labels);
522 if (lyx::count(labels.begin(), labels.end(), from) > 1)
525 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
529 UpdatableInset * BufferView::theLockingInset() const
531 // If NULL is not allowed we should put an Assert here. (Lgb)
533 return text->the_locking_inset;
538 void BufferView::theLockingInset(UpdatableInset * inset)
540 text->the_locking_inset = inset;
544 LyXText * BufferView::getLyXText() const
546 if (theLockingInset()) {
547 LyXText * txt = theLockingInset()->getLyXText(this, true);
555 Language const * BufferView::getParentLanguage(InsetOld * inset) const
557 Paragraph const & par = ownerPar(*buffer(), inset);
558 return par.getFontSettings(buffer()->params(),
559 par.getPositionOfInset(inset)).language();
563 Encoding const * BufferView::getEncoding() const
565 LyXText * t = getLyXText();
569 LyXCursor const & c = t->cursor;
570 LyXFont const font = c.par()->getFont(buffer()->params(), c.pos(),
571 outerFont(c.par(), t->ownerParagraphs()));
572 return font.language()->encoding();
576 void BufferView::haveSelection(bool sel)
578 pimpl_->workarea().haveSelection(sel);
582 int BufferView::workHeight() const
584 return pimpl_->workarea().workHeight();