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->cursor.par());
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);
304 ParagraphList::iterator texrowpar;
307 texrowpar = text->ownerParagraphs().begin();
310 texrowpar = buffer()->getParFromID(tmpid).pit();
312 text->setCursor(texrowpar, tmppos);
316 bool BufferView::insertInset(InsetOld * inset, string const & lout)
318 return pimpl_->insertInset(inset, lout);
322 void BufferView::gotoLabel(string const & label)
324 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
325 it != buffer()->inset_iterator_end(); ++it) {
326 vector<string> labels;
327 it->getLabelList(*buffer(), labels);
328 if (find(labels.begin(),labels.end(),label) != labels.end()) {
330 text->setCursor(it.getPar(), it.getPos());
331 text->selection.cursor = text->cursor;
339 void BufferView::undo()
344 owner()->message(_("Undo"));
347 owner()->message(_("No further undo information"));
353 void BufferView::redo()
358 owner()->message(_("Redo"));
361 owner()->message(_("No further redo information"));
367 // these functions are for the spellchecker
368 WordLangTuple const BufferView::nextWord(float & value)
372 return WordLangTuple();
375 return text->selectNextWordToSpellcheck(value);
379 void BufferView::selectLastWord()
384 LyXCursor cur = text->selection.cursor;
386 text->selection.cursor = cur;
387 text->selectSelectedWord();
392 void BufferView::endOfSpellCheck()
394 if (!available()) return;
397 text->selectSelectedWord();
398 text->clearSelection();
403 void BufferView::replaceWord(string const & replacestring)
408 LyXText * tt = getLyXText();
410 tt->replaceSelectionWithString(replacestring);
411 tt->setSelectionRange(replacestring.length());
413 // Go back so that replacement string is also spellchecked
414 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
415 tt->cursorLeft(this);
417 // FIXME: should be done through LFUN
418 buffer()->markDirty();
423 bool BufferView::lockInset(UpdatableInset * inset)
427 // don't relock if we're already locked
428 if (theLockingInset() == inset)
430 if (!theLockingInset()) {
431 // first check if it's the inset under the cursor we want lock
432 // should be most of the time
433 if (text->cursor.pos() < text->cursor.par()->size()
434 && text->cursor.par()->getChar(text->cursor.pos()) ==
435 Paragraph::META_INSET) {
436 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
438 theLockingInset(inset);
442 // Then do a deep look of the inset and lock the right one
443 int const id = inset->id();
444 ParagraphList::iterator pit = buffer()->paragraphs().begin();
445 ParagraphList::iterator pend = buffer()->paragraphs().end();
446 for (; pit != pend; ++pit) {
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(pit, it->pos);
452 theLockingInset(inset);
459 return theLockingInset()->lockInsetInInset(this, inset);
463 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
465 if (theLockingInset() && available()) {
466 y += text->cursor.y() + theLockingInset()->insetInInsetY();
467 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
476 void BufferView::hideCursor()
478 screen().hideCursor();
482 int BufferView::unlockInset(UpdatableInset * inset)
486 if (inset && theLockingInset() == inset) {
487 inset->insetUnlock(this);
489 // make sure we update the combo !
490 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
491 // Tell the paragraph dialog that we changed paragraph
492 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
496 if (inset && theLockingInset() &&
497 theLockingInset()->unlockInsetInInset(this, inset)) {
498 // Tell the paragraph dialog that we changed paragraph
499 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
500 // owner inset has updated the layout combo
508 void BufferView::updateInset(InsetOld const * inset)
510 pimpl_->updateInset(inset);
514 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
516 // Check if the label 'from' appears more than once
517 vector<string> labels;
518 buffer()->getLabelList(labels);
520 if (lyx::count(labels.begin(), labels.end(), from) > 1)
523 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
527 UpdatableInset * BufferView::theLockingInset() const
529 // If NULL is not allowed we should put an Assert here. (Lgb)
531 return text->the_locking_inset;
536 void BufferView::theLockingInset(UpdatableInset * inset)
538 text->the_locking_inset = inset;
542 LyXText * BufferView::getLyXText() const
544 if (theLockingInset()) {
545 LyXText * txt = theLockingInset()->getLyXText(this, true);
553 Language const * BufferView::getParentLanguage(InsetOld * inset) const
555 Paragraph const & par = ownerPar(*buffer(), inset);
556 return par.getFontSettings(buffer()->params(),
557 par.getPositionOfInset(inset)).language();
561 Encoding const * BufferView::getEncoding() const
563 LyXText * t = getLyXText();
567 LyXCursor const & c = t->cursor;
568 LyXFont const font = c.par()->getFont(buffer()->params(), c.pos(),
569 outerFont(c.par(), t->ownerParagraphs()));
570 return font.language()->encoding();
574 void BufferView::haveSelection(bool sel)
576 pimpl_->workarea().haveSelection(sel);
580 int BufferView::workHeight() const
582 return pimpl_->workarea().workHeight();