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
53 extern BufferList bufferlist;
56 using namespace lyx::support;
66 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
67 int width, int height)
68 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
74 BufferView::~BufferView()
81 Buffer * BufferView::buffer() const
83 return pimpl_->buffer_;
87 LyXScreen & BufferView::screen() const
89 return pimpl_->screen();
93 LyXView * BufferView::owner() const
95 return pimpl_->owner_;
99 Painter & BufferView::painter() const
101 return pimpl_->painter();
105 void BufferView::buffer(Buffer * b)
111 bool BufferView::newFile(string const & fn, string const & tn, bool named)
113 return pimpl_->newFile(fn, tn, named);
117 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 return pimpl_->loadLyXFile(fn, tl);
123 void BufferView::reload()
125 string const fn = buffer()->fileName();
126 if (bufferlist.close(buffer(), false))
131 void BufferView::resize()
134 pimpl_->resizeCurrentBuffer();
138 bool BufferView::fitCursor()
140 return pimpl_->fitCursor();
144 void BufferView::update()
150 void BufferView::updateScrollbar()
152 pimpl_->updateScrollbar();
156 void BufferView::scrollDocView(int value)
158 pimpl_->scrollDocView(value);
162 void BufferView::redoCurrentBuffer()
164 pimpl_->redoCurrentBuffer();
168 bool BufferView::available() const
170 return pimpl_->available();
174 Change const BufferView::getCurrentChange()
176 return pimpl_->getCurrentChange();
180 void BufferView::beforeChange(LyXText * text)
182 pimpl_->beforeChange(text);
186 void BufferView::savePosition(unsigned int i)
188 pimpl_->savePosition(i);
192 void BufferView::restorePosition(unsigned int i)
194 pimpl_->restorePosition(i);
198 bool BufferView::isSavedPosition(unsigned int i)
200 return pimpl_->isSavedPosition(i);
204 void BufferView::switchKeyMap()
206 pimpl_->switchKeyMap();
210 void BufferView::insetUnlock()
212 pimpl_->insetUnlock();
216 int BufferView::workWidth() const
218 return pimpl_->workarea().workWidth();
222 void BufferView::center()
228 int BufferView::top_y() const
230 return pimpl_->top_y();
234 void BufferView::top_y(int y)
240 string const BufferView::getClipboard() const
242 return pimpl_->workarea().getClipboard();
246 void BufferView::stuffClipboard(string const & stuff) const
248 pimpl_->stuffClipboard(stuff);
252 bool BufferView::dispatch(FuncRequest const & ev)
254 return pimpl_->dispatch(ev);
258 void BufferView::scroll(int lines)
260 pimpl_->scroll(lines);
264 // Inserts a file into current document
265 bool BufferView::insertLyXFile(string const & filen)
267 // Copyright CHT Software Service GmbH
270 // Insert a LyXformat - file into current buffer
272 // Moved from lyx_cb.C (Lgb)
277 string const fname = MakeAbsPath(filen);
279 // check if file exist
280 FileInfo const fi(fname);
282 if (!fi.readable()) {
283 string const file = MakeDisplayPath(fname, 50);
285 bformat(_("The specified document\n%1$s\ncould not be read."), file);
286 Alert::error(_("Could not read document"), text);
292 ifstream ifs(fname.c_str());
294 string const file = MakeDisplayPath(fname, 50);
296 bformat(_("Could not open the specified document %1$s\n"), file);
297 Alert::error(_("Could not open file"), text);
301 int const c = ifs.peek();
308 text->breakParagraph(buffer()->paragraphs);
311 // FIXME: huh ? No we won't !
312 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
313 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
315 lyxerr[Debug::INFO] << "Will insert file without header"
317 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
325 void BufferView::showErrorList(string const & action) const
327 if (getErrorList().size()) {
328 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
329 owner()->getDialogs().show("errorlist", title);
330 pimpl_->errorlist_.clear();
336 BufferView::getErrorList() const
338 return pimpl_->errorlist_;
342 void BufferView::setCursorFromRow(int row)
347 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
349 ParagraphList::iterator texrowpar;
352 texrowpar = text->ownerParagraphs().begin();
355 texrowpar = buffer()->getParFromID(tmpid).pit();
357 text->setCursor(texrowpar, tmppos);
361 bool BufferView::insertInset(InsetOld * inset, string const & lout)
363 return pimpl_->insertInset(inset, lout);
367 void BufferView::gotoLabel(string const & label)
369 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
370 it != buffer()->inset_iterator_end(); ++it) {
371 vector<string> labels;
372 it->getLabelList(labels);
373 if (find(labels.begin(),labels.end(),label) != labels.end()) {
375 text->setCursor(it.getPar(), it.getPos());
376 text->selection.cursor = text->cursor;
384 void BufferView::undo()
389 owner()->message(_("Undo"));
392 owner()->message(_("No further undo information"));
398 void BufferView::redo()
403 owner()->message(_("Redo"));
406 owner()->message(_("No further redo information"));
412 // these functions are for the spellchecker
413 WordLangTuple const BufferView::nextWord(float & value)
417 return WordLangTuple();
420 return text->selectNextWordToSpellcheck(value);
424 void BufferView::selectLastWord()
429 LyXCursor cur = text->selection.cursor;
431 text->selection.cursor = cur;
432 text->selectSelectedWord();
437 void BufferView::endOfSpellCheck()
439 if (!available()) return;
442 text->selectSelectedWord();
443 text->clearSelection();
448 void BufferView::replaceWord(string const & replacestring)
453 LyXText * tt = getLyXText();
455 tt->replaceSelectionWithString(replacestring);
456 tt->setSelectionRange(replacestring.length());
458 // Go back so that replacement string is also spellchecked
459 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
460 tt->cursorLeft(this);
462 // FIXME: should be done through LFUN
463 buffer()->markDirty();
468 bool BufferView::lockInset(UpdatableInset * inset)
472 // don't relock if we're already locked
473 if (theLockingInset() == inset)
475 if (!theLockingInset()) {
476 // first check if it's the inset under the cursor we want lock
477 // should be most of the time
478 if (text->cursor.pos() < text->cursor.par()->size()
479 && text->cursor.par()->getChar(text->cursor.pos()) ==
480 Paragraph::META_INSET) {
481 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
483 theLockingInset(inset);
487 // Then do a deep look of the inset and lock the right one
488 int const id = inset->id();
489 ParagraphList::iterator pit = buffer()->paragraphs.begin();
490 ParagraphList::iterator pend = buffer()->paragraphs.end();
491 for (; pit != pend; ++pit) {
492 InsetList::iterator it = pit->insetlist.begin();
493 InsetList::iterator end = pit->insetlist.end();
494 for (; it != end; ++it) {
495 if (it->inset == inset) {
496 text->setCursorIntern(pit, it->pos);
497 theLockingInset(inset);
500 if (it->inset->getInsetFromID(id)) {
501 text->setCursorIntern(pit, it->pos);
502 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
503 it->inset->localDispatch(cmd);
504 return theLockingInset()->lockInsetInInset(this, inset);
510 return theLockingInset()->lockInsetInInset(this, inset);
514 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
516 if (theLockingInset() && available()) {
517 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
518 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
527 void BufferView::hideCursor()
529 screen().hideCursor();
533 int BufferView::unlockInset(UpdatableInset * inset)
537 if (inset && theLockingInset() == inset) {
538 inset->insetUnlock(this);
540 // make sure we update the combo !
541 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
542 // Tell the paragraph dialog that we changed paragraph
543 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
546 } else if (inset && theLockingInset() &&
547 theLockingInset()->unlockInsetInInset(this, inset)) {
548 // Tell the paragraph dialog that we changed paragraph
549 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
550 // owner inset has updated the layout combo
558 void BufferView::updateInset(InsetOld const * inset)
560 pimpl_->updateInset(inset);
564 bool BufferView::ChangeInsets(InsetOld::Code code,
565 string const & from, string const & to)
567 bool need_update = false;
568 LyXCursor cursor = text->cursor;
569 LyXCursor tmpcursor = cursor;
570 cursor.par(tmpcursor.par());
571 cursor.pos(tmpcursor.pos());
573 ParIterator end = buffer()->par_iterator_end();
574 for (ParIterator it = buffer()->par_iterator_begin();
576 bool changed_inset = false;
577 for (InsetList::iterator it2 = it->insetlist.begin();
578 it2 != it->insetlist.end(); ++it2) {
579 if (it2->inset->lyxCode() == code) {
580 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
581 if (inset->getContents() == from) {
582 inset->setContents(to);
583 changed_inset = true;
592 // The test it.size()==1 was needed to prevent crashes.
593 // How to set the cursor corretly when it.size()>1 ??
594 if (it.size() == 1) {
595 text->setCursorIntern(it.pit(), 0);
596 text->redoParagraph(text->cursor.par());
600 text->setCursorIntern(cursor.par(), cursor.pos());
605 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
607 // Check if the label 'from' appears more than once
608 vector<string> labels;
609 buffer()->getLabelList(labels);
611 if (lyx::count(labels.begin(), labels.end(), from) > 1)
614 return ChangeInsets(InsetOld::REF_CODE, from, to);
618 UpdatableInset * BufferView::theLockingInset() const
620 // If NULL is not allowed we should put an Assert here. (Lgb)
622 return text->the_locking_inset;
627 void BufferView::theLockingInset(UpdatableInset * inset)
629 text->the_locking_inset = inset;
633 LyXText * BufferView::getLyXText() const
635 if (theLockingInset()) {
636 LyXText * txt = theLockingInset()->getLyXText(this, true);
644 Language const * BufferView::getParentLanguage(InsetOld * inset) const
646 Paragraph const & par = ownerPar(*buffer(), inset);
647 return par.getFontSettings(buffer()->params,
648 par.getPositionOfInset(inset)).language();
652 Encoding const * BufferView::getEncoding() const
654 LyXText * t = getLyXText();
658 LyXCursor const & c = t->cursor;
659 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
660 outerFont(c.par(), t->ownerParagraphs()));
661 return font.language()->encoding();
665 void BufferView::haveSelection(bool sel)
667 pimpl_->workarea().haveSelection(sel);
671 int BufferView::workHeight() const
673 return pimpl_->workarea().workHeight();