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"
20 #include "bufferlist.h"
21 #include "BufferView_pimpl.h"
22 #include "funcrequest.h"
24 #include "iterators.h"
26 #include "lyxlayout.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
31 #include "undo_funcs.h"
32 #include "WordLangTuple.h"
34 #include "frontends/Alert.h"
35 #include "frontends/Dialogs.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/screen.h"
38 #include "frontends/WorkArea.h"
40 #include "insets/insetcommand.h" // ChangeRefs
41 #include "insets/updatableinset.h"
43 #include "support/filetools.h"
44 #include "support/lyxalgo.h" // lyx_count
46 using lyx::support::bformat;
47 using lyx::support::MakeAbsPath;
54 extern BufferList bufferlist;
57 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
58 int width, int height)
59 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
65 BufferView::~BufferView()
72 Buffer * BufferView::buffer() const
74 return pimpl_->buffer_;
78 LyXScreen & BufferView::screen() const
80 return pimpl_->screen();
84 LyXView * BufferView::owner() const
86 return pimpl_->owner_;
90 Painter & BufferView::painter() const
92 return pimpl_->painter();
96 void BufferView::buffer(Buffer * b)
102 bool BufferView::newFile(string const & fn, string const & tn, bool named)
104 return pimpl_->newFile(fn, tn, named);
108 bool BufferView::loadLyXFile(string const & fn, bool tl)
110 return pimpl_->loadLyXFile(fn, tl);
114 void BufferView::reload()
116 string const fn = buffer()->fileName();
117 if (bufferlist.close(buffer(), false))
122 void BufferView::resize()
125 pimpl_->resizeCurrentBuffer();
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::switchKeyMap()
197 pimpl_->switchKeyMap();
201 void BufferView::insetUnlock()
203 pimpl_->insetUnlock();
207 int BufferView::workWidth() const
209 return pimpl_->workarea().workWidth();
213 void BufferView::center()
219 int BufferView::top_y() const
221 return pimpl_->top_y();
225 void BufferView::top_y(int y)
231 string const BufferView::getClipboard() const
233 return pimpl_->workarea().getClipboard();
237 void BufferView::stuffClipboard(string const & stuff) const
239 pimpl_->stuffClipboard(stuff);
243 bool BufferView::dispatch(FuncRequest const & ev)
245 return pimpl_->dispatch(ev);
249 void BufferView::scroll(int lines)
251 pimpl_->scroll(lines);
255 // Inserts a file into current document
256 bool BufferView::insertLyXFile(string const & filen)
258 // Copyright CHT Software Service GmbH
261 // Insert a LyXformat - file into current buffer
263 // Moved from lyx_cb.C (Lgb)
265 BOOST_ASSERT(!filen.empty());
267 string const fname = MakeAbsPath(filen);
271 text->breakParagraph(buffer()->paragraphs());
273 bool res = buffer()->readFile(fname, text->cursorPar());
280 void BufferView::showErrorList(string const & action) const
282 if (getErrorList().size()) {
283 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
284 owner()->getDialogs().show("errorlist", title);
285 pimpl_->errorlist_.clear();
291 BufferView::getErrorList() const
293 return pimpl_->errorlist_;
297 void BufferView::setCursorFromRow(int row)
302 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
305 text->setCursor(0, 0);
307 text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
311 bool BufferView::insertInset(InsetOld * inset, string const & lout)
313 return pimpl_->insertInset(inset, lout);
317 void BufferView::gotoLabel(string const & label)
319 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
320 it != buffer()->inset_iterator_end(); ++it) {
321 vector<string> labels;
322 it->getLabelList(*buffer(), labels);
323 if (find(labels.begin(),labels.end(),label) != labels.end()) {
326 std::distance(text->ownerParagraphs().begin(), it.getPar()),
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)
425 // don't relock if we're already locked
426 if (theLockingInset() == inset)
429 if (theLockingInset())
430 return theLockingInset()->lockInsetInInset(this, inset);
432 // first check if it's the inset under the cursor we want lock
433 // should be most of the time
434 if (text->cursor.pos() < text->cursorPar()->size()
435 && text->cursorPar()->getChar(text->cursor.pos()) ==
436 Paragraph::META_INSET) {
437 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
438 theLockingInset(inset);
443 // then do a deep look at the inset and lock the right one
444 ParagraphList::iterator pit = buffer()->paragraphs().begin();
445 ParagraphList::iterator pend = buffer()->paragraphs().end();
446 for (int par = 0; pit != pend; ++pit, ++par) {
447 InsetList::iterator it = pit->insetlist.begin();
448 InsetList::iterator end = pit->insetlist.end();
449 for (; it != end; ++it) {
450 if (it->inset == inset) {
451 text->setCursorIntern(par, it->pos);
452 theLockingInset(inset);
461 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
463 if (theLockingInset() && available()) {
464 y += text->cursor.y() + theLockingInset()->insetInInsetY();
465 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
474 void BufferView::hideCursor()
476 screen().hideCursor();
480 int BufferView::unlockInset(UpdatableInset * inset)
484 if (inset && theLockingInset() == inset) {
485 inset->insetUnlock(this);
487 // make sure we update the combo !
488 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
489 // Tell the paragraph dialog that we changed paragraph
490 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
494 if (inset && theLockingInset() &&
495 theLockingInset()->unlockInsetInInset(this, inset)) {
496 // Tell the paragraph dialog that we changed paragraph
497 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
498 // owner inset has updated the layout combo
506 void BufferView::updateInset(InsetOld const * inset)
508 pimpl_->updateInset(inset);
512 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
514 // Check if the label 'from' appears more than once
515 vector<string> labels;
516 buffer()->getLabelList(labels);
518 if (lyx::count(labels.begin(), labels.end(), from) > 1)
521 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
525 UpdatableInset * BufferView::theLockingInset() const
527 // If NULL is not allowed we should put an Assert here. (Lgb)
529 return text->the_locking_inset;
534 void BufferView::theLockingInset(UpdatableInset * inset)
536 text->the_locking_inset = inset;
540 LyXText * BufferView::getLyXText() const
542 if (theLockingInset()) {
543 LyXText * txt = theLockingInset()->getLyXText(this, true);
551 Language const * BufferView::getParentLanguage(InsetOld * inset) const
553 Paragraph const & par = ownerPar(*buffer(), inset);
554 return par.getFontSettings(buffer()->params(),
555 par.getPositionOfInset(inset)).language();
559 Encoding const * BufferView::getEncoding() const
561 LyXText * text = getLyXText();
565 return text->cursorPar()->getFont(
568 outerFont(text->cursorPar(), text->ownerParagraphs())
569 ).language()->encoding();
573 void BufferView::haveSelection(bool sel)
575 pimpl_->workarea().haveSelection(sel);
579 int BufferView::workHeight() const
581 return pimpl_->workarea().workHeight();