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::toggleSelection(bool b)
219 pimpl_->toggleSelection(b);
223 void BufferView::center()
229 string const BufferView::getClipboard() const
231 return pimpl_->workarea().getClipboard();
235 void BufferView::stuffClipboard(string const & stuff) const
237 pimpl_->stuffClipboard(stuff);
241 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
242 BufferView::UpdateCodes uc2)
244 return static_cast<BufferView::UpdateCodes>
245 (static_cast<int>(uc1) | static_cast<int>(uc2));
249 bool BufferView::dispatch(FuncRequest const & ev)
251 return pimpl_->dispatch(ev);
255 void BufferView::scroll(int lines)
257 pimpl_->scroll(lines);
261 // Inserts a file into current document
262 bool BufferView::insertLyXFile(string const & filen)
264 // Copyright CHT Software Service GmbH
267 // Insert a LyXformat - file into current buffer
269 // Moved from lyx_cb.C (Lgb)
274 string const fname = MakeAbsPath(filen);
276 // check if file exist
277 FileInfo const fi(fname);
279 if (!fi.readable()) {
280 string const file = MakeDisplayPath(fname, 50);
282 bformat(_("The specified document\n%1$s\ncould not be read."), file);
283 Alert::error(_("Could not read document"), text);
289 ifstream ifs(fname.c_str());
291 string const file = MakeDisplayPath(fname, 50);
293 bformat(_("Could not open the specified document %1$s\n"), file);
294 Alert::error(_("Could not open file"), text);
298 int const c = ifs.peek();
305 text->breakParagraph(buffer()->paragraphs);
308 // FIXME: huh ? No we won't !
309 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
310 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
312 lyxerr[Debug::INFO] << "Will insert file without header"
314 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
322 void BufferView::showErrorList(string const & action) const
324 if (getErrorList().size()) {
325 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
326 owner()->getDialogs().show("errorlist", title);
327 pimpl_->errorlist_.clear();
333 BufferView::getErrorList() const
335 return pimpl_->errorlist_;
339 void BufferView::setCursorFromRow(int row)
344 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
346 ParagraphList::iterator texrowpar;
349 texrowpar = text->ownerParagraphs().begin();
352 texrowpar = buffer()->getParFromID(tmpid).pit();
354 text->setCursor(texrowpar, tmppos);
358 bool BufferView::insertInset(InsetOld * inset, string const & lout)
360 return pimpl_->insertInset(inset, lout);
364 void BufferView::gotoLabel(string const & label)
366 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
367 it != buffer()->inset_iterator_end(); ++it) {
368 vector<string> labels;
369 it->getLabelList(labels);
370 if (find(labels.begin(),labels.end(),label) != labels.end()) {
372 text->setCursor(it.getPar(), it.getPos());
373 text->selection.cursor = text->cursor;
381 void BufferView::undo()
386 owner()->message(_("Undo"));
389 owner()->message(_("No further undo information"));
395 void BufferView::redo()
400 owner()->message(_("Redo"));
403 owner()->message(_("No further redo information"));
409 // these functions are for the spellchecker
410 WordLangTuple const BufferView::nextWord(float & value)
414 return WordLangTuple();
417 return text->selectNextWordToSpellcheck(value);
421 void BufferView::selectLastWord()
426 LyXCursor cur = text->selection.cursor;
428 text->selection.cursor = cur;
429 text->selectSelectedWord();
430 toggleSelection(false);
435 void BufferView::endOfSpellCheck()
437 if (!available()) return;
440 text->selectSelectedWord();
441 text->clearSelection();
446 void BufferView::replaceWord(string const & replacestring)
451 LyXText * tt = getLyXText();
453 // clear the selection (if there is any)
454 toggleSelection(false);
456 // clear the selection (if there is any)
457 toggleSelection(false);
458 tt->replaceSelectionWithString(replacestring);
460 tt->setSelectionRange(replacestring.length());
462 // Go back so that replacement string is also spellchecked
463 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
464 tt->cursorLeft(this);
466 // FIXME: should be done through LFUN
467 buffer()->markDirty();
473 bool BufferView::lockInset(UpdatableInset * inset)
477 // don't relock if we're already locked
478 if (theLockingInset() == inset)
480 if (!theLockingInset()) {
481 // first check if it's the inset under the cursor we want lock
482 // should be most of the time
483 if (text->cursor.pos() < text->cursor.par()->size()
484 && text->cursor.par()->getChar(text->cursor.pos()) ==
485 Paragraph::META_INSET) {
486 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
488 theLockingInset(inset);
492 // Then do a deep look of the inset and lock the right one
493 int const id = inset->id();
494 ParagraphList::iterator pit = buffer()->paragraphs.begin();
495 ParagraphList::iterator pend = buffer()->paragraphs.end();
496 for (; pit != pend; ++pit) {
497 InsetList::iterator it = pit->insetlist.begin();
498 InsetList::iterator end = pit->insetlist.end();
499 for (; it != end; ++it) {
500 if (it->inset == inset) {
501 text->setCursorIntern(pit, it->pos);
502 theLockingInset(inset);
505 if (it->inset->getInsetFromID(id)) {
506 text->setCursorIntern(pit, it->pos);
507 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
508 it->inset->localDispatch(cmd);
509 return theLockingInset()->lockInsetInInset(this, inset);
515 return theLockingInset()->lockInsetInInset(this, inset);
519 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
521 if (theLockingInset() && available()) {
522 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
523 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
532 void BufferView::hideCursor()
534 screen().hideCursor();
538 int BufferView::unlockInset(UpdatableInset * inset)
542 if (inset && theLockingInset() == inset) {
543 inset->insetUnlock(this);
545 // make sure we update the combo !
546 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
547 // Tell the paragraph dialog that we changed paragraph
548 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
551 } else if (inset && theLockingInset() &&
552 theLockingInset()->unlockInsetInInset(this, inset)) {
553 // Tell the paragraph dialog that we changed paragraph
554 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
555 // owner inset has updated the layout combo
563 void BufferView::updateInset(InsetOld * inset)
565 pimpl_->updateInset(inset);
569 bool BufferView::ChangeInsets(InsetOld::Code code,
570 string const & from, string const & to)
572 bool need_update = false;
573 LyXCursor cursor = text->cursor;
574 LyXCursor tmpcursor = cursor;
575 cursor.par(tmpcursor.par());
576 cursor.pos(tmpcursor.pos());
578 ParIterator end = buffer()->par_iterator_end();
579 for (ParIterator it = buffer()->par_iterator_begin();
581 bool changed_inset = false;
582 for (InsetList::iterator it2 = it->insetlist.begin();
583 it2 != it->insetlist.end(); ++it2) {
584 if (it2->inset->lyxCode() == code) {
585 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
586 if (inset->getContents() == from) {
587 inset->setContents(to);
588 changed_inset = true;
597 // The test it.size()==1 was needed to prevent crashes.
598 // How to set the cursor corretly when it.size()>1 ??
599 if (it.size() == 1) {
600 text->setCursorIntern(it.pit(), 0);
601 text->redoParagraph(text->cursor.par());
605 text->setCursorIntern(cursor.par(), cursor.pos());
610 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
612 // Check if the label 'from' appears more than once
613 vector<string> labels;
614 buffer()->getLabelList(labels);
616 if (lyx::count(labels.begin(), labels.end(), from) > 1)
619 return ChangeInsets(InsetOld::REF_CODE, from, to);
623 UpdatableInset * BufferView::theLockingInset() const
625 // If NULL is not allowed we should put an Assert here. (Lgb)
627 return text->the_locking_inset;
632 void BufferView::theLockingInset(UpdatableInset * inset)
634 text->the_locking_inset = inset;
638 LyXText * BufferView::getLyXText() const
640 if (theLockingInset()) {
641 LyXText * txt = theLockingInset()->getLyXText(this, true);
649 LyXText * BufferView::getParentText(InsetOld * inset) const
651 if (inset->owner()) {
652 LyXText * txt = inset->getLyXText(this);
653 inset = inset->owner();
654 while (inset && inset->getLyXText(this) == txt)
655 inset = inset->owner();
657 return inset->getLyXText(this);
663 Language const * BufferView::getParentLanguage(InsetOld * inset) const
665 LyXText * text = getParentText(inset);
666 return text->cursor.par()->getFontSettings(buffer()->params,
667 text->cursor.pos()).language();
671 Encoding const * BufferView::getEncoding() const
673 LyXText * t = getLyXText();
677 LyXCursor const & c = t->cursor;
678 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
679 outerFont(c.par(), t->ownerParagraphs()));
680 return font.language()->encoding();
684 void BufferView::haveSelection(bool sel)
686 pimpl_->workarea().haveSelection(sel);
690 int BufferView::workHeight() const
692 return pimpl_->workarea().workHeight();