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"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "undo_funcs.h"
29 #include "WordLangTuple.h"
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/screen.h"
35 #include "frontends/WorkArea.h"
37 #include "insets/insetcommand.h" // ChangeRefs
38 #include "insets/updatableinset.h"
40 #include "support/filetools.h"
41 #include "support/LAssert.h"
42 #include "support/lyxalgo.h" // lyx_count
45 extern BufferList bufferlist;
47 using namespace lyx::support;
53 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
54 int width, int height)
55 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
61 BufferView::~BufferView()
68 Buffer * BufferView::buffer() const
70 return pimpl_->buffer_;
74 LyXScreen & BufferView::screen() const
76 return pimpl_->screen();
80 LyXView * BufferView::owner() const
82 return pimpl_->owner_;
86 Painter & BufferView::painter() const
88 return pimpl_->painter();
92 void BufferView::buffer(Buffer * b)
98 bool BufferView::newFile(string const & fn, string const & tn, bool named)
100 return pimpl_->newFile(fn, tn, named);
104 bool BufferView::loadLyXFile(string const & fn, bool tl)
106 return pimpl_->loadLyXFile(fn, tl);
110 void BufferView::reload()
112 string const fn = buffer()->fileName();
113 if (bufferlist.close(buffer(), false))
118 void BufferView::resize()
121 pimpl_->resizeCurrentBuffer();
125 bool BufferView::fitCursor()
127 return pimpl_->fitCursor();
131 void BufferView::update()
137 void BufferView::updateScrollbar()
139 pimpl_->updateScrollbar();
143 void BufferView::scrollDocView(int value)
145 pimpl_->scrollDocView(value);
149 void BufferView::redoCurrentBuffer()
151 pimpl_->redoCurrentBuffer();
155 bool BufferView::available() const
157 return pimpl_->available();
161 Change const BufferView::getCurrentChange()
163 return pimpl_->getCurrentChange();
167 void BufferView::beforeChange(LyXText * text)
169 pimpl_->beforeChange(text);
173 void BufferView::savePosition(unsigned int i)
175 pimpl_->savePosition(i);
179 void BufferView::restorePosition(unsigned int i)
181 pimpl_->restorePosition(i);
185 bool BufferView::isSavedPosition(unsigned int i)
187 return pimpl_->isSavedPosition(i);
191 void BufferView::switchKeyMap()
193 pimpl_->switchKeyMap();
197 void BufferView::insetUnlock()
199 pimpl_->insetUnlock();
203 int BufferView::workWidth() const
205 return pimpl_->workarea().workWidth();
209 void BufferView::center()
215 int BufferView::top_y() const
217 return pimpl_->top_y();
221 void BufferView::top_y(int y)
227 string const BufferView::getClipboard() const
229 return pimpl_->workarea().getClipboard();
233 void BufferView::stuffClipboard(string const & stuff) const
235 pimpl_->stuffClipboard(stuff);
239 bool BufferView::dispatch(FuncRequest const & ev)
241 return pimpl_->dispatch(ev);
245 void BufferView::scroll(int lines)
247 pimpl_->scroll(lines);
251 // Inserts a file into current document
252 bool BufferView::insertLyXFile(string const & filen)
254 // Copyright CHT Software Service GmbH
257 // Insert a LyXformat - file into current buffer
259 // Moved from lyx_cb.C (Lgb)
261 Assert(!filen.empty());
263 string const fname = MakeAbsPath(filen);
267 text->breakParagraph(buffer()->paragraphs);
269 bool res = buffer()->readFile(fname, text->cursor.par());
276 void BufferView::showErrorList(string const & action) const
278 if (getErrorList().size()) {
279 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
280 owner()->getDialogs().show("errorlist", title);
281 pimpl_->errorlist_.clear();
287 BufferView::getErrorList() const
289 return pimpl_->errorlist_;
293 void BufferView::setCursorFromRow(int row)
298 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
300 ParagraphList::iterator texrowpar;
303 texrowpar = text->ownerParagraphs().begin();
306 texrowpar = buffer()->getParFromID(tmpid).pit();
308 text->setCursor(texrowpar, tmppos);
312 bool BufferView::insertInset(InsetOld * inset, string const & lout)
314 return pimpl_->insertInset(inset, lout);
318 void BufferView::gotoLabel(string const & label)
320 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
321 it != buffer()->inset_iterator_end(); ++it) {
322 vector<string> labels;
323 it->getLabelList(labels);
324 if (find(labels.begin(),labels.end(),label) != labels.end()) {
326 text->setCursor(it.getPar(), it.getPos());
327 text->selection.cursor = text->cursor;
335 void BufferView::undo()
340 owner()->message(_("Undo"));
343 owner()->message(_("No further undo information"));
349 void BufferView::redo()
354 owner()->message(_("Redo"));
357 owner()->message(_("No further redo information"));
363 // these functions are for the spellchecker
364 WordLangTuple const BufferView::nextWord(float & value)
368 return WordLangTuple();
371 return text->selectNextWordToSpellcheck(value);
375 void BufferView::selectLastWord()
380 LyXCursor cur = text->selection.cursor;
382 text->selection.cursor = cur;
383 text->selectSelectedWord();
388 void BufferView::endOfSpellCheck()
390 if (!available()) return;
393 text->selectSelectedWord();
394 text->clearSelection();
399 void BufferView::replaceWord(string const & replacestring)
404 LyXText * tt = getLyXText();
406 tt->replaceSelectionWithString(replacestring);
407 tt->setSelectionRange(replacestring.length());
409 // Go back so that replacement string is also spellchecked
410 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
411 tt->cursorLeft(this);
413 // FIXME: should be done through LFUN
414 buffer()->markDirty();
419 bool BufferView::lockInset(UpdatableInset * inset)
423 // don't relock if we're already locked
424 if (theLockingInset() == inset)
426 if (!theLockingInset()) {
427 // first check if it's the inset under the cursor we want lock
428 // should be most of the time
429 if (text->cursor.pos() < text->cursor.par()->size()
430 && text->cursor.par()->getChar(text->cursor.pos()) ==
431 Paragraph::META_INSET) {
432 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
434 theLockingInset(inset);
438 // Then do a deep look of the inset and lock the right one
439 int const id = inset->id();
440 ParagraphList::iterator pit = buffer()->paragraphs.begin();
441 ParagraphList::iterator pend = buffer()->paragraphs.end();
442 for (; pit != pend; ++pit) {
443 InsetList::iterator it = pit->insetlist.begin();
444 InsetList::iterator end = pit->insetlist.end();
445 for (; it != end; ++it) {
446 if (it->inset == inset) {
447 text->setCursorIntern(pit, it->pos);
448 theLockingInset(inset);
451 if (it->inset->getInsetFromID(id)) {
452 text->setCursorIntern(pit, it->pos);
453 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
454 it->inset->localDispatch(cmd);
455 return theLockingInset()->lockInsetInInset(this, inset);
461 return theLockingInset()->lockInsetInInset(this, inset);
465 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
467 if (theLockingInset() && available()) {
468 y += text->cursor.y() + theLockingInset()->insetInInsetY();
469 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
478 void BufferView::hideCursor()
480 screen().hideCursor();
484 int BufferView::unlockInset(UpdatableInset * inset)
488 if (inset && theLockingInset() == inset) {
489 inset->insetUnlock(this);
491 // make sure we update the combo !
492 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
493 // Tell the paragraph dialog that we changed paragraph
494 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
497 } else if (inset && theLockingInset() &&
498 theLockingInset()->unlockInsetInInset(this, inset)) {
499 // Tell the paragraph dialog that we changed paragraph
500 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
501 // owner inset has updated the layout combo
509 void BufferView::updateInset(InsetOld const * inset)
511 pimpl_->updateInset(inset);
515 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
517 // Check if the label 'from' appears more than once
518 vector<string> labels;
519 buffer()->getLabelList(labels);
521 if (lyx::count(labels.begin(), labels.end(), from) > 1)
524 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
528 UpdatableInset * BufferView::theLockingInset() const
530 // If NULL is not allowed we should put an Assert here. (Lgb)
532 return text->the_locking_inset;
537 void BufferView::theLockingInset(UpdatableInset * inset)
539 text->the_locking_inset = inset;
543 LyXText * BufferView::getLyXText() const
545 if (theLockingInset()) {
546 LyXText * txt = theLockingInset()->getLyXText(this, true);
554 Language const * BufferView::getParentLanguage(InsetOld * inset) const
556 Paragraph const & par = ownerPar(*buffer(), inset);
557 return par.getFontSettings(buffer()->params,
558 par.getPositionOfInset(inset)).language();
562 Encoding const * BufferView::getEncoding() const
564 LyXText * t = getLyXText();
568 LyXCursor const & c = t->cursor;
569 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
570 outerFont(c.par(), t->ownerParagraphs()));
571 return font.language()->encoding();
575 void BufferView::haveSelection(bool sel)
577 pimpl_->workarea().haveSelection(sel);
581 int BufferView::workHeight() const
583 return pimpl_->workarea().workHeight();