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 "funcrequest.h"
29 #include "iterators.h"
31 #include "lyxcursor.h"
34 #include "undo_funcs.h"
36 #include "paragraph_funcs.h"
38 #include "frontends/Alert.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/WorkArea.h"
42 #include "frontends/screen.h"
44 #include "insets/insetcommand.h" // ChangeRefs
45 #include "insets/updatableinset.h"
47 #include "support/FileInfo.h"
48 #include "support/filetools.h"
49 #include "support/types.h"
50 #include "support/lyxalgo.h" // lyx_count
51 #include "support/LAssert.h"
55 extern BufferList bufferlist;
58 using namespace lyx::support;
68 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
69 int width, int height)
70 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
76 BufferView::~BufferView()
83 Buffer * BufferView::buffer() const
85 return pimpl_->buffer_;
89 LyXScreen & BufferView::screen() const
91 return pimpl_->screen();
95 LyXView * BufferView::owner() const
97 return pimpl_->owner_;
101 Painter & BufferView::painter() const
103 return pimpl_->painter();
107 void BufferView::buffer(Buffer * b)
113 bool BufferView::newFile(string const & fn, string const & tn, bool named)
115 return pimpl_->newFile(fn, tn, named);
119 bool BufferView::loadLyXFile(string const & fn, bool tl)
121 return pimpl_->loadLyXFile(fn, tl);
125 void BufferView::reload()
127 string const fn = buffer()->fileName();
128 if (bufferlist.close(buffer(), false))
133 void BufferView::resize()
136 pimpl_->resizeCurrentBuffer();
140 bool BufferView::fitCursor()
142 return pimpl_->fitCursor();
146 void BufferView::update()
152 void BufferView::updateScrollbar()
154 pimpl_->updateScrollbar();
158 void BufferView::scrollDocView(int value)
160 pimpl_->scrollDocView(value);
164 void BufferView::redoCurrentBuffer()
166 pimpl_->redoCurrentBuffer();
170 bool BufferView::available() const
172 return pimpl_->available();
176 Change const BufferView::getCurrentChange()
178 return pimpl_->getCurrentChange();
182 void BufferView::beforeChange(LyXText * text)
184 pimpl_->beforeChange(text);
188 void BufferView::savePosition(unsigned int i)
190 pimpl_->savePosition(i);
194 void BufferView::restorePosition(unsigned int i)
196 pimpl_->restorePosition(i);
200 bool BufferView::isSavedPosition(unsigned int i)
202 return pimpl_->isSavedPosition(i);
206 void BufferView::switchKeyMap()
208 pimpl_->switchKeyMap();
212 void BufferView::insetUnlock()
214 pimpl_->insetUnlock();
218 int BufferView::workWidth() const
220 return pimpl_->workarea().workWidth();
224 void BufferView::center()
230 int BufferView::top_y() const
232 return pimpl_->top_y();
236 void BufferView::top_y(int y)
242 string const BufferView::getClipboard() const
244 return pimpl_->workarea().getClipboard();
248 void BufferView::stuffClipboard(string const & stuff) const
250 pimpl_->stuffClipboard(stuff);
254 bool BufferView::dispatch(FuncRequest const & ev)
256 return pimpl_->dispatch(ev);
260 void BufferView::scroll(int lines)
262 pimpl_->scroll(lines);
266 // Inserts a file into current document
267 bool BufferView::insertLyXFile(string const & filen)
269 // Copyright CHT Software Service GmbH
272 // Insert a LyXformat - file into current buffer
274 // Moved from lyx_cb.C (Lgb)
276 Assert(!filen.empty());
278 string const fname = MakeAbsPath(filen);
282 text->breakParagraph(buffer()->paragraphs);
284 bool res = buffer()->readFile(fname, text->cursor.par());
291 void BufferView::showErrorList(string const & action) const
293 if (getErrorList().size()) {
294 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
295 owner()->getDialogs().show("errorlist", title);
296 pimpl_->errorlist_.clear();
302 BufferView::getErrorList() const
304 return pimpl_->errorlist_;
308 void BufferView::setCursorFromRow(int row)
313 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
315 ParagraphList::iterator texrowpar;
318 texrowpar = text->ownerParagraphs().begin();
321 texrowpar = buffer()->getParFromID(tmpid).pit();
323 text->setCursor(texrowpar, tmppos);
327 bool BufferView::insertInset(InsetOld * inset, string const & lout)
329 return pimpl_->insertInset(inset, lout);
333 void BufferView::gotoLabel(string const & label)
335 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
336 it != buffer()->inset_iterator_end(); ++it) {
337 vector<string> labels;
338 it->getLabelList(labels);
339 if (find(labels.begin(),labels.end(),label) != labels.end()) {
341 text->setCursor(it.getPar(), it.getPos());
342 text->selection.cursor = text->cursor;
350 void BufferView::undo()
355 owner()->message(_("Undo"));
358 owner()->message(_("No further undo information"));
364 void BufferView::redo()
369 owner()->message(_("Redo"));
372 owner()->message(_("No further redo information"));
378 // these functions are for the spellchecker
379 WordLangTuple const BufferView::nextWord(float & value)
383 return WordLangTuple();
386 return text->selectNextWordToSpellcheck(value);
390 void BufferView::selectLastWord()
395 LyXCursor cur = text->selection.cursor;
397 text->selection.cursor = cur;
398 text->selectSelectedWord();
403 void BufferView::endOfSpellCheck()
405 if (!available()) return;
408 text->selectSelectedWord();
409 text->clearSelection();
414 void BufferView::replaceWord(string const & replacestring)
419 LyXText * tt = getLyXText();
421 tt->replaceSelectionWithString(replacestring);
422 tt->setSelectionRange(replacestring.length());
424 // Go back so that replacement string is also spellchecked
425 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
426 tt->cursorLeft(this);
428 // FIXME: should be done through LFUN
429 buffer()->markDirty();
434 bool BufferView::lockInset(UpdatableInset * inset)
438 // don't relock if we're already locked
439 if (theLockingInset() == inset)
441 if (!theLockingInset()) {
442 // first check if it's the inset under the cursor we want lock
443 // should be most of the time
444 if (text->cursor.pos() < text->cursor.par()->size()
445 && text->cursor.par()->getChar(text->cursor.pos()) ==
446 Paragraph::META_INSET) {
447 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
449 theLockingInset(inset);
453 // Then do a deep look of the inset and lock the right one
454 int const id = inset->id();
455 ParagraphList::iterator pit = buffer()->paragraphs.begin();
456 ParagraphList::iterator pend = buffer()->paragraphs.end();
457 for (; pit != pend; ++pit) {
458 InsetList::iterator it = pit->insetlist.begin();
459 InsetList::iterator end = pit->insetlist.end();
460 for (; it != end; ++it) {
461 if (it->inset == inset) {
462 text->setCursorIntern(pit, it->pos);
463 theLockingInset(inset);
466 if (it->inset->getInsetFromID(id)) {
467 text->setCursorIntern(pit, it->pos);
468 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
469 it->inset->localDispatch(cmd);
470 return theLockingInset()->lockInsetInInset(this, inset);
476 return theLockingInset()->lockInsetInInset(this, inset);
480 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
482 if (theLockingInset() && available()) {
483 y += text->cursor.y() + theLockingInset()->insetInInsetY();
484 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
493 void BufferView::hideCursor()
495 screen().hideCursor();
499 int BufferView::unlockInset(UpdatableInset * inset)
503 if (inset && theLockingInset() == inset) {
504 inset->insetUnlock(this);
506 // make sure we update the combo !
507 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
508 // Tell the paragraph dialog that we changed paragraph
509 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
512 } else if (inset && theLockingInset() &&
513 theLockingInset()->unlockInsetInInset(this, inset)) {
514 // Tell the paragraph dialog that we changed paragraph
515 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
516 // owner inset has updated the layout combo
524 void BufferView::updateInset(InsetOld const * inset)
526 pimpl_->updateInset(inset);
530 bool BufferView::ChangeInsets(InsetOld::Code code,
531 string const & from, string const & to)
533 bool need_update = false;
534 LyXCursor cursor = text->cursor;
535 LyXCursor tmpcursor = cursor;
536 cursor.par(tmpcursor.par());
537 cursor.pos(tmpcursor.pos());
539 ParIterator end = buffer()->par_iterator_end();
540 for (ParIterator it = buffer()->par_iterator_begin();
542 bool changed_inset = false;
543 for (InsetList::iterator it2 = it->insetlist.begin();
544 it2 != it->insetlist.end(); ++it2) {
545 if (it2->inset->lyxCode() == code) {
546 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
547 if (inset->getContents() == from) {
548 inset->setContents(to);
549 changed_inset = true;
558 // The test it.size()==1 was needed to prevent crashes.
559 // How to set the cursor corretly when it.size()>1 ??
560 if (it.size() == 1) {
561 text->setCursorIntern(it.pit(), 0);
562 text->redoParagraph(text->cursor.par());
566 text->setCursorIntern(cursor.par(), cursor.pos());
571 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
573 // Check if the label 'from' appears more than once
574 vector<string> labels;
575 buffer()->getLabelList(labels);
577 if (lyx::count(labels.begin(), labels.end(), from) > 1)
580 return ChangeInsets(InsetOld::REF_CODE, from, to);
584 UpdatableInset * BufferView::theLockingInset() const
586 // If NULL is not allowed we should put an Assert here. (Lgb)
588 return text->the_locking_inset;
593 void BufferView::theLockingInset(UpdatableInset * inset)
595 text->the_locking_inset = inset;
599 LyXText * BufferView::getLyXText() const
601 if (theLockingInset()) {
602 LyXText * txt = theLockingInset()->getLyXText(this, true);
610 Language const * BufferView::getParentLanguage(InsetOld * inset) const
612 Paragraph const & par = ownerPar(*buffer(), inset);
613 return par.getFontSettings(buffer()->params,
614 par.getPositionOfInset(inset)).language();
618 Encoding const * BufferView::getEncoding() const
620 LyXText * t = getLyXText();
624 LyXCursor const & c = t->cursor;
625 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
626 outerFont(c.par(), t->ownerParagraphs()));
627 return font.language()->encoding();
631 void BufferView::haveSelection(bool sel)
633 pimpl_->workarea().haveSelection(sel);
637 int BufferView::workHeight() const
639 return pimpl_->workarea().workHeight();