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"
20 #include "WordLangTuple.h"
22 #include "bufferlist.h"
24 #include "funcrequest.h"
25 #include "iterators.h"
28 #include "undo_funcs.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/updatableinset.h"
41 #include "support/filetools.h"
42 #include "support/lyxalgo.h" // lyx_count
43 #include "support/LAssert.h"
46 extern BufferList bufferlist;
49 using namespace lyx::support;
59 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
60 int width, int height)
61 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
67 BufferView::~BufferView()
74 Buffer * BufferView::buffer() const
76 return pimpl_->buffer_;
80 LyXScreen & BufferView::screen() const
82 return pimpl_->screen();
86 LyXView * BufferView::owner() const
88 return pimpl_->owner_;
92 Painter & BufferView::painter() const
94 return pimpl_->painter();
98 void BufferView::buffer(Buffer * b)
104 bool BufferView::newFile(string const & fn, string const & tn, bool named)
106 return pimpl_->newFile(fn, tn, named);
110 bool BufferView::loadLyXFile(string const & fn, bool tl)
112 return pimpl_->loadLyXFile(fn, tl);
116 void BufferView::reload()
118 string const fn = buffer()->fileName();
119 if (bufferlist.close(buffer(), false))
124 void BufferView::resize()
127 pimpl_->resizeCurrentBuffer();
131 bool BufferView::fitCursor()
133 return pimpl_->fitCursor();
137 void BufferView::update()
143 void BufferView::updateScrollbar()
145 pimpl_->updateScrollbar();
149 void BufferView::scrollDocView(int value)
151 pimpl_->scrollDocView(value);
155 void BufferView::redoCurrentBuffer()
157 pimpl_->redoCurrentBuffer();
161 bool BufferView::available() const
163 return pimpl_->available();
167 Change const BufferView::getCurrentChange()
169 return pimpl_->getCurrentChange();
173 void BufferView::beforeChange(LyXText * text)
175 pimpl_->beforeChange(text);
179 void BufferView::savePosition(unsigned int i)
181 pimpl_->savePosition(i);
185 void BufferView::restorePosition(unsigned int i)
187 pimpl_->restorePosition(i);
191 bool BufferView::isSavedPosition(unsigned int i)
193 return pimpl_->isSavedPosition(i);
197 void BufferView::switchKeyMap()
199 pimpl_->switchKeyMap();
203 void BufferView::insetUnlock()
205 pimpl_->insetUnlock();
209 int BufferView::workWidth() const
211 return pimpl_->workarea().workWidth();
215 void BufferView::center()
221 int BufferView::top_y() const
223 return pimpl_->top_y();
227 void BufferView::top_y(int y)
233 string const BufferView::getClipboard() const
235 return pimpl_->workarea().getClipboard();
239 void BufferView::stuffClipboard(string const & stuff) const
241 pimpl_->stuffClipboard(stuff);
245 bool BufferView::dispatch(FuncRequest const & ev)
247 return pimpl_->dispatch(ev);
251 void BufferView::scroll(int lines)
253 pimpl_->scroll(lines);
257 // Inserts a file into current document
258 bool BufferView::insertLyXFile(string const & filen)
260 // Copyright CHT Software Service GmbH
263 // Insert a LyXformat - file into current buffer
265 // Moved from lyx_cb.C (Lgb)
267 Assert(!filen.empty());
269 string const fname = MakeAbsPath(filen);
273 text->breakParagraph(buffer()->paragraphs);
275 bool res = buffer()->readFile(fname, text->cursor.par());
282 void BufferView::showErrorList(string const & action) const
284 if (getErrorList().size()) {
285 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
286 owner()->getDialogs().show("errorlist", title);
287 pimpl_->errorlist_.clear();
293 BufferView::getErrorList() const
295 return pimpl_->errorlist_;
299 void BufferView::setCursorFromRow(int row)
304 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
306 ParagraphList::iterator texrowpar;
309 texrowpar = text->ownerParagraphs().begin();
312 texrowpar = buffer()->getParFromID(tmpid).pit();
314 text->setCursor(texrowpar, tmppos);
318 bool BufferView::insertInset(InsetOld * inset, string const & lout)
320 return pimpl_->insertInset(inset, lout);
324 void BufferView::gotoLabel(string const & label)
326 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
327 it != buffer()->inset_iterator_end(); ++it) {
328 vector<string> labels;
329 it->getLabelList(labels);
330 if (find(labels.begin(),labels.end(),label) != labels.end()) {
332 text->setCursor(it.getPar(), it.getPos());
333 text->selection.cursor = text->cursor;
341 void BufferView::undo()
346 owner()->message(_("Undo"));
349 owner()->message(_("No further undo information"));
355 void BufferView::redo()
360 owner()->message(_("Redo"));
363 owner()->message(_("No further redo information"));
369 // these functions are for the spellchecker
370 WordLangTuple const BufferView::nextWord(float & value)
374 return WordLangTuple();
377 return text->selectNextWordToSpellcheck(value);
381 void BufferView::selectLastWord()
386 LyXCursor cur = text->selection.cursor;
388 text->selection.cursor = cur;
389 text->selectSelectedWord();
394 void BufferView::endOfSpellCheck()
396 if (!available()) return;
399 text->selectSelectedWord();
400 text->clearSelection();
405 void BufferView::replaceWord(string const & replacestring)
410 LyXText * tt = getLyXText();
412 tt->replaceSelectionWithString(replacestring);
413 tt->setSelectionRange(replacestring.length());
415 // Go back so that replacement string is also spellchecked
416 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
417 tt->cursorLeft(this);
419 // FIXME: should be done through LFUN
420 buffer()->markDirty();
425 bool BufferView::lockInset(UpdatableInset * inset)
429 // don't relock if we're already locked
430 if (theLockingInset() == inset)
432 if (!theLockingInset()) {
433 // first check if it's the inset under the cursor we want lock
434 // should be most of the time
435 if (text->cursor.pos() < text->cursor.par()->size()
436 && text->cursor.par()->getChar(text->cursor.pos()) ==
437 Paragraph::META_INSET) {
438 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
440 theLockingInset(inset);
444 // Then do a deep look of the inset and lock the right one
445 int const id = inset->id();
446 ParagraphList::iterator pit = buffer()->paragraphs.begin();
447 ParagraphList::iterator pend = buffer()->paragraphs.end();
448 for (; pit != pend; ++pit) {
449 InsetList::iterator it = pit->insetlist.begin();
450 InsetList::iterator end = pit->insetlist.end();
451 for (; it != end; ++it) {
452 if (it->inset == inset) {
453 text->setCursorIntern(pit, it->pos);
454 theLockingInset(inset);
457 if (it->inset->getInsetFromID(id)) {
458 text->setCursorIntern(pit, it->pos);
459 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
460 it->inset->localDispatch(cmd);
461 return theLockingInset()->lockInsetInInset(this, inset);
467 return theLockingInset()->lockInsetInInset(this, inset);
471 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
473 if (theLockingInset() && available()) {
474 y += text->cursor.y() + theLockingInset()->insetInInsetY();
475 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
484 void BufferView::hideCursor()
486 screen().hideCursor();
490 int BufferView::unlockInset(UpdatableInset * inset)
494 if (inset && theLockingInset() == inset) {
495 inset->insetUnlock(this);
497 // make sure we update the combo !
498 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
499 // Tell the paragraph dialog that we changed paragraph
500 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
503 } else if (inset && theLockingInset() &&
504 theLockingInset()->unlockInsetInInset(this, inset)) {
505 // Tell the paragraph dialog that we changed paragraph
506 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
507 // owner inset has updated the layout combo
515 void BufferView::updateInset(InsetOld const * inset)
517 pimpl_->updateInset(inset);
521 bool BufferView::ChangeInsets(InsetOld::Code code,
522 string const & from, string const & to)
524 bool need_update = false;
525 LyXCursor cursor = text->cursor;
526 LyXCursor tmpcursor = cursor;
527 cursor.par(tmpcursor.par());
528 cursor.pos(tmpcursor.pos());
530 ParIterator end = buffer()->par_iterator_end();
531 for (ParIterator it = buffer()->par_iterator_begin();
533 bool changed_inset = false;
534 for (InsetList::iterator it2 = it->insetlist.begin();
535 it2 != it->insetlist.end(); ++it2) {
536 if (it2->inset->lyxCode() == code) {
537 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
538 if (inset->getContents() == from) {
539 inset->setContents(to);
540 changed_inset = true;
549 // The test it.size()==1 was needed to prevent crashes.
550 // How to set the cursor corretly when it.size()>1 ??
551 if (it.size() == 1) {
552 text->setCursorIntern(it.pit(), 0);
553 text->redoParagraph(text->cursor.par());
557 text->setCursorIntern(cursor.par(), cursor.pos());
562 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
564 // Check if the label 'from' appears more than once
565 vector<string> labels;
566 buffer()->getLabelList(labels);
568 if (lyx::count(labels.begin(), labels.end(), from) > 1)
571 return ChangeInsets(InsetOld::REF_CODE, from, to);
575 UpdatableInset * BufferView::theLockingInset() const
577 // If NULL is not allowed we should put an Assert here. (Lgb)
579 return text->the_locking_inset;
584 void BufferView::theLockingInset(UpdatableInset * inset)
586 text->the_locking_inset = inset;
590 LyXText * BufferView::getLyXText() const
592 if (theLockingInset()) {
593 LyXText * txt = theLockingInset()->getLyXText(this, true);
601 Language const * BufferView::getParentLanguage(InsetOld * inset) const
603 Paragraph const & par = ownerPar(*buffer(), inset);
604 return par.getFontSettings(buffer()->params,
605 par.getPositionOfInset(inset)).language();
609 Encoding const * BufferView::getEncoding() const
611 LyXText * t = getLyXText();
615 LyXCursor const & c = t->cursor;
616 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
617 outerFont(c.par(), t->ownerParagraphs()));
618 return font.language()->encoding();
622 void BufferView::haveSelection(bool sel)
624 pimpl_->workarea().haveSelection(sel);
628 int BufferView::workHeight() const
630 return pimpl_->workarea().workHeight();