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_funcs.h"
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/WorkArea.h"
35 #include "frontends/screen.h"
37 #include "insets/insetcommand.h" // ChangeRefs
38 #include "insets/updatableinset.h"
40 #include "support/filetools.h"
41 #include "support/lyxalgo.h" // lyx_count
42 #include "support/LAssert.h"
45 extern BufferList bufferlist;
48 using namespace lyx::support;
58 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
59 int width, int height)
60 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
66 BufferView::~BufferView()
73 Buffer * BufferView::buffer() const
75 return pimpl_->buffer_;
79 LyXScreen & BufferView::screen() const
81 return pimpl_->screen();
85 LyXView * BufferView::owner() const
87 return pimpl_->owner_;
91 Painter & BufferView::painter() const
93 return pimpl_->painter();
97 void BufferView::buffer(Buffer * b)
103 bool BufferView::newFile(string const & fn, string const & tn, bool named)
105 return pimpl_->newFile(fn, tn, named);
109 bool BufferView::loadLyXFile(string const & fn, bool tl)
111 return pimpl_->loadLyXFile(fn, tl);
115 void BufferView::reload()
117 string const fn = buffer()->fileName();
118 if (bufferlist.close(buffer(), false))
123 void BufferView::resize()
126 pimpl_->resizeCurrentBuffer();
130 bool BufferView::fitCursor()
132 return pimpl_->fitCursor();
136 void BufferView::update()
142 void BufferView::updateScrollbar()
144 pimpl_->updateScrollbar();
148 void BufferView::scrollDocView(int value)
150 pimpl_->scrollDocView(value);
154 void BufferView::redoCurrentBuffer()
156 pimpl_->redoCurrentBuffer();
160 bool BufferView::available() const
162 return pimpl_->available();
166 Change const BufferView::getCurrentChange()
168 return pimpl_->getCurrentChange();
172 void BufferView::beforeChange(LyXText * text)
174 pimpl_->beforeChange(text);
178 void BufferView::savePosition(unsigned int i)
180 pimpl_->savePosition(i);
184 void BufferView::restorePosition(unsigned int i)
186 pimpl_->restorePosition(i);
190 bool BufferView::isSavedPosition(unsigned int i)
192 return pimpl_->isSavedPosition(i);
196 void BufferView::switchKeyMap()
198 pimpl_->switchKeyMap();
202 void BufferView::insetUnlock()
204 pimpl_->insetUnlock();
208 int BufferView::workWidth() const
210 return pimpl_->workarea().workWidth();
214 void BufferView::center()
220 int BufferView::top_y() const
222 return pimpl_->top_y();
226 void BufferView::top_y(int y)
232 string const BufferView::getClipboard() const
234 return pimpl_->workarea().getClipboard();
238 void BufferView::stuffClipboard(string const & stuff) const
240 pimpl_->stuffClipboard(stuff);
244 bool BufferView::dispatch(FuncRequest const & ev)
246 return pimpl_->dispatch(ev);
250 void BufferView::scroll(int lines)
252 pimpl_->scroll(lines);
256 // Inserts a file into current document
257 bool BufferView::insertLyXFile(string const & filen)
259 // Copyright CHT Software Service GmbH
262 // Insert a LyXformat - file into current buffer
264 // Moved from lyx_cb.C (Lgb)
266 Assert(!filen.empty());
268 string const fname = MakeAbsPath(filen);
272 text->breakParagraph(buffer()->paragraphs);
274 bool res = buffer()->readFile(fname, text->cursor.par());
281 void BufferView::showErrorList(string const & action) const
283 if (getErrorList().size()) {
284 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
285 owner()->getDialogs().show("errorlist", title);
286 pimpl_->errorlist_.clear();
292 BufferView::getErrorList() const
294 return pimpl_->errorlist_;
298 void BufferView::setCursorFromRow(int row)
303 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
305 ParagraphList::iterator texrowpar;
308 texrowpar = text->ownerParagraphs().begin();
311 texrowpar = buffer()->getParFromID(tmpid).pit();
313 text->setCursor(texrowpar, tmppos);
317 bool BufferView::insertInset(InsetOld * inset, string const & lout)
319 return pimpl_->insertInset(inset, lout);
323 void BufferView::gotoLabel(string const & label)
325 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
326 it != buffer()->inset_iterator_end(); ++it) {
327 vector<string> labels;
328 it->getLabelList(labels);
329 if (find(labels.begin(),labels.end(),label) != labels.end()) {
331 text->setCursor(it.getPar(), it.getPos());
332 text->selection.cursor = text->cursor;
340 void BufferView::undo()
345 owner()->message(_("Undo"));
348 owner()->message(_("No further undo information"));
354 void BufferView::redo()
359 owner()->message(_("Redo"));
362 owner()->message(_("No further redo information"));
368 // these functions are for the spellchecker
369 WordLangTuple const BufferView::nextWord(float & value)
373 return WordLangTuple();
376 return text->selectNextWordToSpellcheck(value);
380 void BufferView::selectLastWord()
385 LyXCursor cur = text->selection.cursor;
387 text->selection.cursor = cur;
388 text->selectSelectedWord();
393 void BufferView::endOfSpellCheck()
395 if (!available()) return;
398 text->selectSelectedWord();
399 text->clearSelection();
404 void BufferView::replaceWord(string const & replacestring)
409 LyXText * tt = getLyXText();
411 tt->replaceSelectionWithString(replacestring);
412 tt->setSelectionRange(replacestring.length());
414 // Go back so that replacement string is also spellchecked
415 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
416 tt->cursorLeft(this);
418 // FIXME: should be done through LFUN
419 buffer()->markDirty();
424 bool BufferView::lockInset(UpdatableInset * inset)
428 // don't relock if we're already locked
429 if (theLockingInset() == inset)
431 if (!theLockingInset()) {
432 // first check if it's the inset under the cursor we want lock
433 // should be most of the time
434 if (text->cursor.pos() < text->cursor.par()->size()
435 && text->cursor.par()->getChar(text->cursor.pos()) ==
436 Paragraph::META_INSET) {
437 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
439 theLockingInset(inset);
443 // Then do a deep look of the inset and lock the right one
444 int const id = inset->id();
445 ParagraphList::iterator pit = buffer()->paragraphs.begin();
446 ParagraphList::iterator pend = buffer()->paragraphs.end();
447 for (; pit != pend; ++pit) {
448 InsetList::iterator it = pit->insetlist.begin();
449 InsetList::iterator end = pit->insetlist.end();
450 for (; it != end; ++it) {
451 if (it->inset == inset) {
452 text->setCursorIntern(pit, it->pos);
453 theLockingInset(inset);
456 if (it->inset->getInsetFromID(id)) {
457 text->setCursorIntern(pit, it->pos);
458 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
459 it->inset->localDispatch(cmd);
460 return theLockingInset()->lockInsetInInset(this, inset);
466 return theLockingInset()->lockInsetInInset(this, inset);
470 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
472 if (theLockingInset() && available()) {
473 y += text->cursor.y() + theLockingInset()->insetInInsetY();
474 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
483 void BufferView::hideCursor()
485 screen().hideCursor();
489 int BufferView::unlockInset(UpdatableInset * inset)
493 if (inset && theLockingInset() == inset) {
494 inset->insetUnlock(this);
496 // make sure we update the combo !
497 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
498 // Tell the paragraph dialog that we changed paragraph
499 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
502 } else if (inset && theLockingInset() &&
503 theLockingInset()->unlockInsetInInset(this, inset)) {
504 // Tell the paragraph dialog that we changed paragraph
505 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
506 // owner inset has updated the layout combo
514 void BufferView::updateInset(InsetOld const * inset)
516 pimpl_->updateInset(inset);
520 bool BufferView::ChangeInsets(InsetOld::Code code,
521 string const & from, string const & to)
523 bool need_update = false;
524 LyXCursor cursor = text->cursor;
525 LyXCursor tmpcursor = cursor;
526 cursor.par(tmpcursor.par());
527 cursor.pos(tmpcursor.pos());
529 ParIterator end = buffer()->par_iterator_end();
530 for (ParIterator it = buffer()->par_iterator_begin();
532 bool changed_inset = false;
533 for (InsetList::iterator it2 = it->insetlist.begin();
534 it2 != it->insetlist.end(); ++it2) {
535 if (it2->inset->lyxCode() == code) {
536 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
537 if (inset->getContents() == from) {
538 inset->setContents(to);
539 changed_inset = true;
548 // The test it.size()==1 was needed to prevent crashes.
549 // How to set the cursor corretly when it.size()>1 ??
550 if (it.size() == 1) {
551 text->setCursorIntern(it.pit(), 0);
552 text->redoParagraph(text->cursor.par());
556 text->setCursorIntern(cursor.par(), cursor.pos());
561 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
563 // Check if the label 'from' appears more than once
564 vector<string> labels;
565 buffer()->getLabelList(labels);
567 if (lyx::count(labels.begin(), labels.end(), from) > 1)
570 return ChangeInsets(InsetOld::REF_CODE, from, to);
574 UpdatableInset * BufferView::theLockingInset() const
576 // If NULL is not allowed we should put an Assert here. (Lgb)
578 return text->the_locking_inset;
583 void BufferView::theLockingInset(UpdatableInset * inset)
585 text->the_locking_inset = inset;
589 LyXText * BufferView::getLyXText() const
591 if (theLockingInset()) {
592 LyXText * txt = theLockingInset()->getLyXText(this, true);
600 Language const * BufferView::getParentLanguage(InsetOld * inset) const
602 Paragraph const & par = ownerPar(*buffer(), inset);
603 return par.getFontSettings(buffer()->params,
604 par.getPositionOfInset(inset)).language();
608 Encoding const * BufferView::getEncoding() const
610 LyXText * t = getLyXText();
614 LyXCursor const & c = t->cursor;
615 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
616 outerFont(c.par(), t->ownerParagraphs()));
617 return font.language()->encoding();
621 void BufferView::haveSelection(bool sel)
623 pimpl_->workarea().haveSelection(sel);
627 int BufferView::workHeight() const
629 return pimpl_->workarea().workHeight();