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 bool BufferView::dispatch(FuncRequest const & ev)
243 return pimpl_->dispatch(ev);
247 void BufferView::scroll(int lines)
249 pimpl_->scroll(lines);
253 // Inserts a file into current document
254 bool BufferView::insertLyXFile(string const & filen)
256 // Copyright CHT Software Service GmbH
259 // Insert a LyXformat - file into current buffer
261 // Moved from lyx_cb.C (Lgb)
266 string const fname = MakeAbsPath(filen);
268 // check if file exist
269 FileInfo const fi(fname);
271 if (!fi.readable()) {
272 string const file = MakeDisplayPath(fname, 50);
274 bformat(_("The specified document\n%1$s\ncould not be read."), file);
275 Alert::error(_("Could not read document"), text);
281 ifstream ifs(fname.c_str());
283 string const file = MakeDisplayPath(fname, 50);
285 bformat(_("Could not open the specified document %1$s\n"), file);
286 Alert::error(_("Could not open file"), text);
290 int const c = ifs.peek();
297 text->breakParagraph(buffer()->paragraphs);
300 // FIXME: huh ? No we won't !
301 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
302 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
304 lyxerr[Debug::INFO] << "Will insert file without header"
306 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
314 void BufferView::showErrorList(string const & action) const
316 if (getErrorList().size()) {
317 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
318 owner()->getDialogs().show("errorlist", title);
319 pimpl_->errorlist_.clear();
325 BufferView::getErrorList() const
327 return pimpl_->errorlist_;
331 void BufferView::setCursorFromRow(int row)
336 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
338 ParagraphList::iterator texrowpar;
341 texrowpar = text->ownerParagraphs().begin();
344 texrowpar = buffer()->getParFromID(tmpid).pit();
346 text->setCursor(texrowpar, tmppos);
350 bool BufferView::insertInset(InsetOld * inset, string const & lout)
352 return pimpl_->insertInset(inset, lout);
356 void BufferView::gotoLabel(string const & label)
358 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
359 it != buffer()->inset_iterator_end(); ++it) {
360 vector<string> labels;
361 it->getLabelList(labels);
362 if (find(labels.begin(),labels.end(),label) != labels.end()) {
364 text->setCursor(it.getPar(), it.getPos());
365 text->selection.cursor = text->cursor;
373 void BufferView::undo()
378 owner()->message(_("Undo"));
381 owner()->message(_("No further undo information"));
387 void BufferView::redo()
392 owner()->message(_("Redo"));
395 owner()->message(_("No further redo information"));
401 // these functions are for the spellchecker
402 WordLangTuple const BufferView::nextWord(float & value)
406 return WordLangTuple();
409 return text->selectNextWordToSpellcheck(value);
413 void BufferView::selectLastWord()
418 LyXCursor cur = text->selection.cursor;
420 text->selection.cursor = cur;
421 text->selectSelectedWord();
422 toggleSelection(false);
427 void BufferView::endOfSpellCheck()
429 if (!available()) return;
432 text->selectSelectedWord();
433 text->clearSelection();
438 void BufferView::replaceWord(string const & replacestring)
443 LyXText * tt = getLyXText();
445 // clear the selection (if there is any)
446 toggleSelection(false);
448 // clear the selection (if there is any)
449 toggleSelection(false);
450 tt->replaceSelectionWithString(replacestring);
452 tt->setSelectionRange(replacestring.length());
454 // Go back so that replacement string is also spellchecked
455 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
456 tt->cursorLeft(this);
458 // FIXME: should be done through LFUN
459 buffer()->markDirty();
465 bool BufferView::lockInset(UpdatableInset * inset)
469 // don't relock if we're already locked
470 if (theLockingInset() == inset)
472 if (!theLockingInset()) {
473 // first check if it's the inset under the cursor we want lock
474 // should be most of the time
475 if (text->cursor.pos() < text->cursor.par()->size()
476 && text->cursor.par()->getChar(text->cursor.pos()) ==
477 Paragraph::META_INSET) {
478 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
480 theLockingInset(inset);
484 // Then do a deep look of the inset and lock the right one
485 int const id = inset->id();
486 ParagraphList::iterator pit = buffer()->paragraphs.begin();
487 ParagraphList::iterator pend = buffer()->paragraphs.end();
488 for (; pit != pend; ++pit) {
489 InsetList::iterator it = pit->insetlist.begin();
490 InsetList::iterator end = pit->insetlist.end();
491 for (; it != end; ++it) {
492 if (it->inset == inset) {
493 text->setCursorIntern(pit, it->pos);
494 theLockingInset(inset);
497 if (it->inset->getInsetFromID(id)) {
498 text->setCursorIntern(pit, it->pos);
499 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
500 it->inset->localDispatch(cmd);
501 return theLockingInset()->lockInsetInInset(this, inset);
507 return theLockingInset()->lockInsetInInset(this, inset);
511 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
513 if (theLockingInset() && available()) {
514 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
515 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
524 void BufferView::hideCursor()
526 screen().hideCursor();
530 int BufferView::unlockInset(UpdatableInset * inset)
534 if (inset && theLockingInset() == inset) {
535 inset->insetUnlock(this);
537 // make sure we update the combo !
538 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
539 // Tell the paragraph dialog that we changed paragraph
540 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
543 } else if (inset && theLockingInset() &&
544 theLockingInset()->unlockInsetInInset(this, inset)) {
545 // Tell the paragraph dialog that we changed paragraph
546 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
547 // owner inset has updated the layout combo
555 void BufferView::updateInset()
557 pimpl_->updateInset();
561 bool BufferView::ChangeInsets(InsetOld::Code code,
562 string const & from, string const & to)
564 bool need_update = false;
565 LyXCursor cursor = text->cursor;
566 LyXCursor tmpcursor = cursor;
567 cursor.par(tmpcursor.par());
568 cursor.pos(tmpcursor.pos());
570 ParIterator end = buffer()->par_iterator_end();
571 for (ParIterator it = buffer()->par_iterator_begin();
573 bool changed_inset = false;
574 for (InsetList::iterator it2 = it->insetlist.begin();
575 it2 != it->insetlist.end(); ++it2) {
576 if (it2->inset->lyxCode() == code) {
577 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
578 if (inset->getContents() == from) {
579 inset->setContents(to);
580 changed_inset = true;
589 // The test it.size()==1 was needed to prevent crashes.
590 // How to set the cursor corretly when it.size()>1 ??
591 if (it.size() == 1) {
592 text->setCursorIntern(it.pit(), 0);
593 text->redoParagraph(text->cursor.par());
597 text->setCursorIntern(cursor.par(), cursor.pos());
602 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
604 // Check if the label 'from' appears more than once
605 vector<string> labels;
606 buffer()->getLabelList(labels);
608 if (lyx::count(labels.begin(), labels.end(), from) > 1)
611 return ChangeInsets(InsetOld::REF_CODE, from, to);
615 UpdatableInset * BufferView::theLockingInset() const
617 // If NULL is not allowed we should put an Assert here. (Lgb)
619 return text->the_locking_inset;
624 void BufferView::theLockingInset(UpdatableInset * inset)
626 text->the_locking_inset = inset;
630 LyXText * BufferView::getLyXText() const
632 if (theLockingInset()) {
633 LyXText * txt = theLockingInset()->getLyXText(this, true);
641 LyXText * BufferView::getParentText(InsetOld * inset) const
643 if (inset->owner()) {
644 LyXText * txt = inset->getLyXText(this);
645 inset = inset->owner();
646 while (inset && inset->getLyXText(this) == txt)
647 inset = inset->owner();
649 return inset->getLyXText(this);
655 Language const * BufferView::getParentLanguage(InsetOld * inset) const
657 LyXText * text = getParentText(inset);
658 return text->cursor.par()->getFontSettings(buffer()->params,
659 text->cursor.pos()).language();
663 Encoding const * BufferView::getEncoding() const
665 LyXText * t = getLyXText();
669 LyXCursor const & c = t->cursor;
670 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
671 outerFont(c.par(), t->ownerParagraphs()));
672 return font.language()->encoding();
676 void BufferView::haveSelection(bool sel)
678 pimpl_->workarea().haveSelection(sel);
682 int BufferView::workHeight() const
684 return pimpl_->workarea().workHeight();