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"
21 #include "ParagraphParameters.h"
22 #include "WordLangTuple.h"
24 #include "bufferlist.h"
27 #include "errorlist.h"
28 #include "iterators.h"
30 #include "lyxcursor.h"
33 #include "undo_funcs.h"
35 #include "paragraph_funcs.h"
37 #include "frontends/Alert.h"
38 #include "frontends/Dialogs.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/WorkArea.h"
41 #include "frontends/screen.h"
43 #include "insets/insetcommand.h" // ChangeRefs
44 #include "insets/updatableinset.h"
46 #include "support/FileInfo.h"
47 #include "support/filetools.h"
48 #include "support/types.h"
49 #include "support/lyxalgo.h" // lyx_count
50 #include "support/LAssert.h"
54 extern BufferList bufferlist;
57 using namespace lyx::support;
67 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
68 int width, int height)
69 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
75 BufferView::~BufferView()
82 Buffer * BufferView::buffer() const
84 return pimpl_->buffer_;
88 LyXScreen & BufferView::screen() const
90 return pimpl_->screen();
94 LyXView * BufferView::owner() const
96 return pimpl_->owner_;
100 Painter & BufferView::painter() const
102 return pimpl_->painter();
106 void BufferView::buffer(Buffer * b)
112 bool BufferView::newFile(string const & fn, string const & tn, bool named)
114 return pimpl_->newFile(fn, tn, named);
118 bool BufferView::loadLyXFile(string const & fn, bool tl)
120 return pimpl_->loadLyXFile(fn, tl);
124 void BufferView::reload()
126 string const fn = buffer()->fileName();
127 if (bufferlist.close(buffer(), false))
132 void BufferView::resize()
135 pimpl_->resizeCurrentBuffer();
139 bool BufferView::fitCursor()
141 return pimpl_->fitCursor();
145 void BufferView::update()
151 void BufferView::updateScrollbar()
153 pimpl_->updateScrollbar();
157 void BufferView::scrollDocView(int value)
159 pimpl_->scrollDocView(value);
163 void BufferView::redoCurrentBuffer()
165 pimpl_->redoCurrentBuffer();
169 bool BufferView::available() const
171 return pimpl_->available();
175 Change const BufferView::getCurrentChange()
177 return pimpl_->getCurrentChange();
181 void BufferView::beforeChange(LyXText * text)
183 pimpl_->beforeChange(text);
187 void BufferView::savePosition(unsigned int i)
189 pimpl_->savePosition(i);
193 void BufferView::restorePosition(unsigned int i)
195 pimpl_->restorePosition(i);
199 bool BufferView::isSavedPosition(unsigned int i)
201 return pimpl_->isSavedPosition(i);
205 void BufferView::switchKeyMap()
207 pimpl_->switchKeyMap();
211 void BufferView::insetUnlock()
213 pimpl_->insetUnlock();
217 int BufferView::workWidth() const
219 return pimpl_->workarea().workWidth();
223 void BufferView::center()
229 int BufferView::top_y() const
231 return pimpl_->top_y();
235 void BufferView::top_y(int y)
241 string const BufferView::getClipboard() const
243 return pimpl_->workarea().getClipboard();
247 void BufferView::stuffClipboard(string const & stuff) const
249 pimpl_->stuffClipboard(stuff);
253 bool BufferView::dispatch(FuncRequest const & ev)
255 return pimpl_->dispatch(ev);
259 void BufferView::scroll(int lines)
261 pimpl_->scroll(lines);
265 // Inserts a file into current document
266 bool BufferView::insertLyXFile(string const & filen)
268 // Copyright CHT Software Service GmbH
271 // Insert a LyXformat - file into current buffer
273 // Moved from lyx_cb.C (Lgb)
275 Assert(!filen.empty());
277 string const fname = MakeAbsPath(filen);
281 text->breakParagraph(buffer()->paragraphs);
283 bool res = buffer()->readFile(fname, text->cursor.par());
290 void BufferView::showErrorList(string const & action) const
292 if (getErrorList().size()) {
293 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
294 owner()->getDialogs().show("errorlist", title);
295 pimpl_->errorlist_.clear();
301 BufferView::getErrorList() const
303 return pimpl_->errorlist_;
307 void BufferView::setCursorFromRow(int row)
312 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
314 ParagraphList::iterator texrowpar;
317 texrowpar = text->ownerParagraphs().begin();
320 texrowpar = buffer()->getParFromID(tmpid).pit();
322 text->setCursor(texrowpar, tmppos);
326 bool BufferView::insertInset(InsetOld * inset, string const & lout)
328 return pimpl_->insertInset(inset, lout);
332 void BufferView::gotoLabel(string const & label)
334 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
335 it != buffer()->inset_iterator_end(); ++it) {
336 vector<string> labels;
337 it->getLabelList(labels);
338 if (find(labels.begin(),labels.end(),label) != labels.end()) {
340 text->setCursor(it.getPar(), it.getPos());
341 text->selection.cursor = text->cursor;
349 void BufferView::undo()
354 owner()->message(_("Undo"));
357 owner()->message(_("No further undo information"));
363 void BufferView::redo()
368 owner()->message(_("Redo"));
371 owner()->message(_("No further redo information"));
377 // these functions are for the spellchecker
378 WordLangTuple const BufferView::nextWord(float & value)
382 return WordLangTuple();
385 return text->selectNextWordToSpellcheck(value);
389 void BufferView::selectLastWord()
394 LyXCursor cur = text->selection.cursor;
396 text->selection.cursor = cur;
397 text->selectSelectedWord();
402 void BufferView::endOfSpellCheck()
404 if (!available()) return;
407 text->selectSelectedWord();
408 text->clearSelection();
413 void BufferView::replaceWord(string const & replacestring)
418 LyXText * tt = getLyXText();
420 tt->replaceSelectionWithString(replacestring);
421 tt->setSelectionRange(replacestring.length());
423 // Go back so that replacement string is also spellchecked
424 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
425 tt->cursorLeft(this);
427 // FIXME: should be done through LFUN
428 buffer()->markDirty();
433 bool BufferView::lockInset(UpdatableInset * inset)
437 // don't relock if we're already locked
438 if (theLockingInset() == inset)
440 if (!theLockingInset()) {
441 // first check if it's the inset under the cursor we want lock
442 // should be most of the time
443 if (text->cursor.pos() < text->cursor.par()->size()
444 && text->cursor.par()->getChar(text->cursor.pos()) ==
445 Paragraph::META_INSET) {
446 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
448 theLockingInset(inset);
452 // Then do a deep look of the inset and lock the right one
453 int const id = inset->id();
454 ParagraphList::iterator pit = buffer()->paragraphs.begin();
455 ParagraphList::iterator pend = buffer()->paragraphs.end();
456 for (; pit != pend; ++pit) {
457 InsetList::iterator it = pit->insetlist.begin();
458 InsetList::iterator end = pit->insetlist.end();
459 for (; it != end; ++it) {
460 if (it->inset == inset) {
461 text->setCursorIntern(pit, it->pos);
462 theLockingInset(inset);
465 if (it->inset->getInsetFromID(id)) {
466 text->setCursorIntern(pit, it->pos);
467 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
468 it->inset->localDispatch(cmd);
469 return theLockingInset()->lockInsetInInset(this, inset);
475 return theLockingInset()->lockInsetInInset(this, inset);
479 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
481 if (theLockingInset() && available()) {
482 y += text->cursor.y() + theLockingInset()->insetInInsetY();
483 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
492 void BufferView::hideCursor()
494 screen().hideCursor();
498 int BufferView::unlockInset(UpdatableInset * inset)
502 if (inset && theLockingInset() == inset) {
503 inset->insetUnlock(this);
505 // make sure we update the combo !
506 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
507 // Tell the paragraph dialog that we changed paragraph
508 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
511 } else if (inset && theLockingInset() &&
512 theLockingInset()->unlockInsetInInset(this, inset)) {
513 // Tell the paragraph dialog that we changed paragraph
514 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
515 // owner inset has updated the layout combo
523 void BufferView::updateInset(InsetOld const * inset)
525 pimpl_->updateInset(inset);
529 bool BufferView::ChangeInsets(InsetOld::Code code,
530 string const & from, string const & to)
532 bool need_update = false;
533 LyXCursor cursor = text->cursor;
534 LyXCursor tmpcursor = cursor;
535 cursor.par(tmpcursor.par());
536 cursor.pos(tmpcursor.pos());
538 ParIterator end = buffer()->par_iterator_end();
539 for (ParIterator it = buffer()->par_iterator_begin();
541 bool changed_inset = false;
542 for (InsetList::iterator it2 = it->insetlist.begin();
543 it2 != it->insetlist.end(); ++it2) {
544 if (it2->inset->lyxCode() == code) {
545 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
546 if (inset->getContents() == from) {
547 inset->setContents(to);
548 changed_inset = true;
557 // The test it.size()==1 was needed to prevent crashes.
558 // How to set the cursor corretly when it.size()>1 ??
559 if (it.size() == 1) {
560 text->setCursorIntern(it.pit(), 0);
561 text->redoParagraph(text->cursor.par());
565 text->setCursorIntern(cursor.par(), cursor.pos());
570 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
572 // Check if the label 'from' appears more than once
573 vector<string> labels;
574 buffer()->getLabelList(labels);
576 if (lyx::count(labels.begin(), labels.end(), from) > 1)
579 return ChangeInsets(InsetOld::REF_CODE, from, to);
583 UpdatableInset * BufferView::theLockingInset() const
585 // If NULL is not allowed we should put an Assert here. (Lgb)
587 return text->the_locking_inset;
592 void BufferView::theLockingInset(UpdatableInset * inset)
594 text->the_locking_inset = inset;
598 LyXText * BufferView::getLyXText() const
600 if (theLockingInset()) {
601 LyXText * txt = theLockingInset()->getLyXText(this, true);
609 Language const * BufferView::getParentLanguage(InsetOld * inset) const
611 Paragraph const & par = ownerPar(*buffer(), inset);
612 return par.getFontSettings(buffer()->params,
613 par.getPositionOfInset(inset)).language();
617 Encoding const * BufferView::getEncoding() const
619 LyXText * t = getLyXText();
623 LyXCursor const & c = t->cursor;
624 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
625 outerFont(c.par(), t->ownerParagraphs()));
626 return font.language()->encoding();
630 void BufferView::haveSelection(bool sel)
632 pimpl_->workarea().haveSelection(sel);
636 int BufferView::workHeight() const
638 return pimpl_->workarea().workHeight();