3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
7 * \author John Levon <moz@compsoc.man.ac.uk>
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
16 #include "ParagraphParameters.h"
17 #include "WordLangTuple.h"
19 #include "bufferlist.h"
22 #include "errorlist.h"
23 #include "iterators.h"
25 #include "lyxcursor.h"
28 #include "undo_funcs.h"
30 #include "paragraph_funcs.h"
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/updatableinset.h"
41 #include "support/FileInfo.h"
42 #include "support/filetools.h"
43 #include "support/types.h"
44 #include "support/lyxalgo.h" // lyx_count
48 extern BufferList bufferlist;
51 using namespace lyx::support;
61 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
62 int width, int height)
63 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
69 BufferView::~BufferView()
76 Buffer * BufferView::buffer() const
78 return pimpl_->buffer_;
82 LyXScreen & BufferView::screen() const
84 return pimpl_->screen();
88 LyXView * BufferView::owner() const
90 return pimpl_->owner_;
94 Painter & BufferView::painter() const
96 return pimpl_->painter();
100 void BufferView::buffer(Buffer * b)
106 bool BufferView::newFile(string const & fn, string const & tn, bool named)
108 return pimpl_->newFile(fn, tn, named);
112 bool BufferView::loadLyXFile(string const & fn, bool tl)
114 return pimpl_->loadLyXFile(fn, tl);
118 void BufferView::reload()
120 string const fn = buffer()->fileName();
121 if (bufferlist.close(buffer(), false))
126 void BufferView::resize()
129 pimpl_->resizeCurrentBuffer();
133 bool BufferView::fitCursor()
135 return pimpl_->fitCursor();
139 void BufferView::update()
145 void BufferView::updateScrollbar()
147 pimpl_->updateScrollbar();
151 void BufferView::scrollDocView(int value)
153 pimpl_->scrollDocView(value);
157 void BufferView::redoCurrentBuffer()
159 pimpl_->redoCurrentBuffer();
163 bool BufferView::available() const
165 return pimpl_->available();
169 Change const BufferView::getCurrentChange()
171 return pimpl_->getCurrentChange();
175 void BufferView::beforeChange(LyXText * text)
177 pimpl_->beforeChange(text);
181 void BufferView::savePosition(unsigned int i)
183 pimpl_->savePosition(i);
187 void BufferView::restorePosition(unsigned int i)
189 pimpl_->restorePosition(i);
193 bool BufferView::isSavedPosition(unsigned int i)
195 return pimpl_->isSavedPosition(i);
199 void BufferView::switchKeyMap()
201 pimpl_->switchKeyMap();
205 void BufferView::insetUnlock()
207 pimpl_->insetUnlock();
211 int BufferView::workWidth() const
213 return pimpl_->workarea().workWidth();
217 void BufferView::center()
223 string const BufferView::getClipboard() const
225 return pimpl_->workarea().getClipboard();
229 void BufferView::stuffClipboard(string const & stuff) const
231 pimpl_->stuffClipboard(stuff);
235 bool BufferView::dispatch(FuncRequest const & ev)
237 return pimpl_->dispatch(ev);
241 void BufferView::scroll(int lines)
243 pimpl_->scroll(lines);
247 // Inserts a file into current document
248 bool BufferView::insertLyXFile(string const & filen)
250 // Copyright CHT Software Service GmbH
253 // Insert a LyXformat - file into current buffer
255 // Moved from lyx_cb.C (Lgb)
260 string const fname = MakeAbsPath(filen);
262 // check if file exist
263 FileInfo const fi(fname);
265 if (!fi.readable()) {
266 string const file = MakeDisplayPath(fname, 50);
268 bformat(_("The specified document\n%1$s\ncould not be read."), file);
269 Alert::error(_("Could not read document"), text);
275 ifstream ifs(fname.c_str());
277 string const file = MakeDisplayPath(fname, 50);
279 bformat(_("Could not open the specified document %1$s\n"), file);
280 Alert::error(_("Could not open file"), text);
284 int const c = ifs.peek();
291 text->breakParagraph(buffer()->paragraphs);
294 // FIXME: huh ? No we won't !
295 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
296 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
298 lyxerr[Debug::INFO] << "Will insert file without header"
300 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
308 void BufferView::showErrorList(string const & action) const
310 if (getErrorList().size()) {
311 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
312 owner()->getDialogs().show("errorlist", title);
313 pimpl_->errorlist_.clear();
319 BufferView::getErrorList() const
321 return pimpl_->errorlist_;
325 void BufferView::setCursorFromRow(int row)
330 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
332 ParagraphList::iterator texrowpar;
335 texrowpar = text->ownerParagraphs().begin();
338 texrowpar = buffer()->getParFromID(tmpid).pit();
340 text->setCursor(texrowpar, tmppos);
344 bool BufferView::insertInset(InsetOld * inset, string const & lout)
346 return pimpl_->insertInset(inset, lout);
350 void BufferView::gotoLabel(string const & label)
352 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
353 it != buffer()->inset_iterator_end(); ++it) {
354 vector<string> labels;
355 it->getLabelList(labels);
356 if (find(labels.begin(),labels.end(),label) != labels.end()) {
358 text->setCursor(it.getPar(), it.getPos());
359 text->selection.cursor = text->cursor;
367 void BufferView::undo()
372 owner()->message(_("Undo"));
375 owner()->message(_("No further undo information"));
381 void BufferView::redo()
386 owner()->message(_("Redo"));
389 owner()->message(_("No further redo information"));
395 // these functions are for the spellchecker
396 WordLangTuple const BufferView::nextWord(float & value)
400 return WordLangTuple();
403 return text->selectNextWordToSpellcheck(value);
407 void BufferView::selectLastWord()
412 LyXCursor cur = text->selection.cursor;
414 text->selection.cursor = cur;
415 text->selectSelectedWord();
420 void BufferView::endOfSpellCheck()
422 if (!available()) return;
425 text->selectSelectedWord();
426 text->clearSelection();
431 void BufferView::replaceWord(string const & replacestring)
436 LyXText * tt = getLyXText();
438 tt->replaceSelectionWithString(replacestring);
439 tt->setSelectionRange(replacestring.length());
441 // Go back so that replacement string is also spellchecked
442 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
443 tt->cursorLeft(this);
445 // FIXME: should be done through LFUN
446 buffer()->markDirty();
451 bool BufferView::lockInset(UpdatableInset * inset)
455 // don't relock if we're already locked
456 if (theLockingInset() == inset)
458 if (!theLockingInset()) {
459 // first check if it's the inset under the cursor we want lock
460 // should be most of the time
461 if (text->cursor.pos() < text->cursor.par()->size()
462 && text->cursor.par()->getChar(text->cursor.pos()) ==
463 Paragraph::META_INSET) {
464 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
466 theLockingInset(inset);
470 // Then do a deep look of the inset and lock the right one
471 int const id = inset->id();
472 ParagraphList::iterator pit = buffer()->paragraphs.begin();
473 ParagraphList::iterator pend = buffer()->paragraphs.end();
474 for (; pit != pend; ++pit) {
475 InsetList::iterator it = pit->insetlist.begin();
476 InsetList::iterator end = pit->insetlist.end();
477 for (; it != end; ++it) {
478 if (it->inset == inset) {
479 text->setCursorIntern(pit, it->pos);
480 theLockingInset(inset);
483 if (it->inset->getInsetFromID(id)) {
484 text->setCursorIntern(pit, it->pos);
485 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
486 it->inset->localDispatch(cmd);
487 return theLockingInset()->lockInsetInInset(this, inset);
493 return theLockingInset()->lockInsetInInset(this, inset);
497 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
499 if (theLockingInset() && available()) {
500 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
501 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
510 void BufferView::hideCursor()
512 screen().hideCursor();
516 int BufferView::unlockInset(UpdatableInset * inset)
520 if (inset && theLockingInset() == inset) {
521 inset->insetUnlock(this);
523 // make sure we update the combo !
524 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
525 // Tell the paragraph dialog that we changed paragraph
526 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
529 } else if (inset && theLockingInset() &&
530 theLockingInset()->unlockInsetInInset(this, inset)) {
531 // Tell the paragraph dialog that we changed paragraph
532 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
533 // owner inset has updated the layout combo
541 void BufferView::updateInset()
543 pimpl_->updateInset();
547 bool BufferView::ChangeInsets(InsetOld::Code code,
548 string const & from, string const & to)
550 bool need_update = false;
551 LyXCursor cursor = text->cursor;
552 LyXCursor tmpcursor = cursor;
553 cursor.par(tmpcursor.par());
554 cursor.pos(tmpcursor.pos());
556 ParIterator end = buffer()->par_iterator_end();
557 for (ParIterator it = buffer()->par_iterator_begin();
559 bool changed_inset = false;
560 for (InsetList::iterator it2 = it->insetlist.begin();
561 it2 != it->insetlist.end(); ++it2) {
562 if (it2->inset->lyxCode() == code) {
563 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
564 if (inset->getContents() == from) {
565 inset->setContents(to);
566 changed_inset = true;
575 // The test it.size()==1 was needed to prevent crashes.
576 // How to set the cursor corretly when it.size()>1 ??
577 if (it.size() == 1) {
578 text->setCursorIntern(it.pit(), 0);
579 text->redoParagraph(text->cursor.par());
583 text->setCursorIntern(cursor.par(), cursor.pos());
588 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
590 // Check if the label 'from' appears more than once
591 vector<string> labels;
592 buffer()->getLabelList(labels);
594 if (lyx::count(labels.begin(), labels.end(), from) > 1)
597 return ChangeInsets(InsetOld::REF_CODE, from, to);
601 UpdatableInset * BufferView::theLockingInset() const
603 // If NULL is not allowed we should put an Assert here. (Lgb)
605 return text->the_locking_inset;
610 void BufferView::theLockingInset(UpdatableInset * inset)
612 text->the_locking_inset = inset;
616 LyXText * BufferView::getLyXText() const
618 if (theLockingInset()) {
619 LyXText * txt = theLockingInset()->getLyXText(this, true);
627 LyXText * BufferView::getParentText(InsetOld * inset) const
629 if (inset->owner()) {
630 LyXText * txt = inset->getLyXText(this);
631 inset = inset->owner();
632 while (inset && inset->getLyXText(this) == txt)
633 inset = inset->owner();
635 return inset->getLyXText(this);
641 Language const * BufferView::getParentLanguage(InsetOld * inset) const
643 LyXText * text = getParentText(inset);
644 return text->cursor.par()->getFontSettings(buffer()->params,
645 text->cursor.pos()).language();
649 Encoding const * BufferView::getEncoding() const
651 LyXText * t = getLyXText();
655 LyXCursor const & c = t->cursor;
656 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
657 outerFont(c.par(), t->ownerParagraphs()));
658 return font.language()->encoding();
662 void BufferView::haveSelection(bool sel)
664 pimpl_->workarea().haveSelection(sel);
668 int BufferView::workHeight() const
670 return pimpl_->workarea().workHeight();