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/LAssert.h"
44 #include "support/lyxalgo.h" // lyx_count
47 extern BufferList bufferlist;
49 using namespace lyx::support;
55 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
56 int width, int height)
57 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
63 BufferView::~BufferView()
70 Buffer * BufferView::buffer() const
72 return pimpl_->buffer_;
76 LyXScreen & BufferView::screen() const
78 return pimpl_->screen();
82 LyXView * BufferView::owner() const
84 return pimpl_->owner_;
88 Painter & BufferView::painter() const
90 return pimpl_->painter();
94 void BufferView::buffer(Buffer * b)
100 bool BufferView::newFile(string const & fn, string const & tn, bool named)
102 return pimpl_->newFile(fn, tn, named);
106 bool BufferView::loadLyXFile(string const & fn, bool tl)
108 return pimpl_->loadLyXFile(fn, tl);
112 void BufferView::reload()
114 string const fn = buffer()->fileName();
115 if (bufferlist.close(buffer(), false))
120 void BufferView::resize()
123 pimpl_->resizeCurrentBuffer();
127 bool BufferView::fitCursor()
129 return pimpl_->fitCursor();
133 void BufferView::update()
139 void BufferView::updateScrollbar()
141 pimpl_->updateScrollbar();
145 void BufferView::scrollDocView(int value)
147 pimpl_->scrollDocView(value);
151 void BufferView::redoCurrentBuffer()
153 pimpl_->redoCurrentBuffer();
157 bool BufferView::available() const
159 return pimpl_->available();
163 Change const BufferView::getCurrentChange()
165 return pimpl_->getCurrentChange();
169 void BufferView::beforeChange(LyXText * text)
171 pimpl_->beforeChange(text);
175 void BufferView::savePosition(unsigned int i)
177 pimpl_->savePosition(i);
181 void BufferView::restorePosition(unsigned int i)
183 pimpl_->restorePosition(i);
187 bool BufferView::isSavedPosition(unsigned int i)
189 return pimpl_->isSavedPosition(i);
193 void BufferView::switchKeyMap()
195 pimpl_->switchKeyMap();
199 void BufferView::insetUnlock()
201 pimpl_->insetUnlock();
205 int BufferView::workWidth() const
207 return pimpl_->workarea().workWidth();
211 void BufferView::center()
217 int BufferView::top_y() const
219 return pimpl_->top_y();
223 void BufferView::top_y(int y)
229 string const BufferView::getClipboard() const
231 return pimpl_->workarea().getClipboard();
235 void BufferView::stuffClipboard(string const & stuff) const
237 pimpl_->stuffClipboard(stuff);
241 bool BufferView::dispatch(FuncRequest const & ev)
243 return pimpl_->dispatch(ev);
247 void BufferView::scroll(int lines)
249 pimpl_->scroll(lines);
253 // Inserts a file into current document
254 bool BufferView::insertLyXFile(string const & filen)
256 // Copyright CHT Software Service GmbH
259 // Insert a LyXformat - file into current buffer
261 // Moved from lyx_cb.C (Lgb)
263 Assert(!filen.empty());
265 string const fname = MakeAbsPath(filen);
269 text->breakParagraph(buffer()->paragraphs());
271 bool res = buffer()->readFile(fname, text->cursor.par());
278 void BufferView::showErrorList(string const & action) const
280 if (getErrorList().size()) {
281 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
282 owner()->getDialogs().show("errorlist", title);
283 pimpl_->errorlist_.clear();
289 BufferView::getErrorList() const
291 return pimpl_->errorlist_;
295 void BufferView::setCursorFromRow(int row)
300 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
302 ParagraphList::iterator texrowpar;
305 texrowpar = text->ownerParagraphs().begin();
308 texrowpar = buffer()->getParFromID(tmpid).pit();
310 text->setCursor(texrowpar, tmppos);
314 bool BufferView::insertInset(InsetOld * inset, string const & lout)
316 return pimpl_->insertInset(inset, lout);
320 void BufferView::gotoLabel(string const & label)
322 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
323 it != buffer()->inset_iterator_end(); ++it) {
324 vector<string> labels;
325 it->getLabelList(labels);
326 if (find(labels.begin(),labels.end(),label) != labels.end()) {
328 text->setCursor(it.getPar(), it.getPos());
329 text->selection.cursor = text->cursor;
337 void BufferView::undo()
342 owner()->message(_("Undo"));
345 owner()->message(_("No further undo information"));
351 void BufferView::redo()
356 owner()->message(_("Redo"));
359 owner()->message(_("No further redo information"));
365 // these functions are for the spellchecker
366 WordLangTuple const BufferView::nextWord(float & value)
370 return WordLangTuple();
373 return text->selectNextWordToSpellcheck(value);
377 void BufferView::selectLastWord()
382 LyXCursor cur = text->selection.cursor;
384 text->selection.cursor = cur;
385 text->selectSelectedWord();
390 void BufferView::endOfSpellCheck()
392 if (!available()) return;
395 text->selectSelectedWord();
396 text->clearSelection();
401 void BufferView::replaceWord(string const & replacestring)
406 LyXText * tt = getLyXText();
408 tt->replaceSelectionWithString(replacestring);
409 tt->setSelectionRange(replacestring.length());
411 // Go back so that replacement string is also spellchecked
412 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
413 tt->cursorLeft(this);
415 // FIXME: should be done through LFUN
416 buffer()->markDirty();
421 bool BufferView::lockInset(UpdatableInset * inset)
425 // don't relock if we're already locked
426 if (theLockingInset() == inset)
428 if (!theLockingInset()) {
429 // first check if it's the inset under the cursor we want lock
430 // should be most of the time
431 if (text->cursor.pos() < text->cursor.par()->size()
432 && text->cursor.par()->getChar(text->cursor.pos()) ==
433 Paragraph::META_INSET) {
434 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
436 theLockingInset(inset);
440 // Then do a deep look of the inset and lock the right one
441 int const id = inset->id();
442 ParagraphList::iterator pit = buffer()->paragraphs().begin();
443 ParagraphList::iterator pend = buffer()->paragraphs().end();
444 for (; pit != pend; ++pit) {
445 InsetList::iterator it = pit->insetlist.begin();
446 InsetList::iterator end = pit->insetlist.end();
447 for (; it != end; ++it) {
448 if (it->inset == inset) {
449 text->setCursorIntern(pit, it->pos);
450 theLockingInset(inset);
453 if (it->inset->getInsetFromID(id)) {
454 text->setCursorIntern(pit, it->pos);
455 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
456 it->inset->localDispatch(cmd);
457 return theLockingInset()->lockInsetInInset(this, inset);
463 return theLockingInset()->lockInsetInInset(this, inset);
467 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
469 if (theLockingInset() && available()) {
470 y += text->cursor.y() + theLockingInset()->insetInInsetY();
471 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
480 void BufferView::hideCursor()
482 screen().hideCursor();
486 int BufferView::unlockInset(UpdatableInset * inset)
490 if (inset && theLockingInset() == inset) {
491 inset->insetUnlock(this);
493 // make sure we update the combo !
494 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
495 // Tell the paragraph dialog that we changed paragraph
496 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
499 } else if (inset && theLockingInset() &&
500 theLockingInset()->unlockInsetInInset(this, inset)) {
501 // Tell the paragraph dialog that we changed paragraph
502 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
503 // owner inset has updated the layout combo
511 void BufferView::updateInset(InsetOld const * inset)
513 pimpl_->updateInset(inset);
517 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
519 // Check if the label 'from' appears more than once
520 vector<string> labels;
521 buffer()->getLabelList(labels);
523 if (lyx::count(labels.begin(), labels.end(), from) > 1)
526 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
530 UpdatableInset * BufferView::theLockingInset() const
532 // If NULL is not allowed we should put an Assert here. (Lgb)
534 return text->the_locking_inset;
539 void BufferView::theLockingInset(UpdatableInset * inset)
541 text->the_locking_inset = inset;
545 LyXText * BufferView::getLyXText() const
547 if (theLockingInset()) {
548 LyXText * txt = theLockingInset()->getLyXText(this, true);
556 Language const * BufferView::getParentLanguage(InsetOld * inset) const
558 Paragraph const & par = ownerPar(*buffer(), inset);
559 return par.getFontSettings(buffer()->params(),
560 par.getPositionOfInset(inset)).language();
564 Encoding const * BufferView::getEncoding() const
566 LyXText * t = getLyXText();
570 LyXCursor const & c = t->cursor;
571 LyXFont const font = c.par()->getFont(buffer()->params(), c.pos(),
572 outerFont(c.par(), t->ownerParagraphs()));
573 return font.language()->encoding();
577 void BufferView::haveSelection(bool sel)
579 pimpl_->workarea().haveSelection(sel);
583 int BufferView::workHeight() const
585 return pimpl_->workarea().workHeight();