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::update(LyXText * text, UpdateCodes f)
201 pimpl_->update(text, f);
205 void BufferView::update(UpdateCodes f)
211 void BufferView::switchKeyMap()
213 pimpl_->switchKeyMap();
217 void BufferView::insetUnlock()
219 pimpl_->insetUnlock();
223 int BufferView::workWidth() const
225 return pimpl_->workarea().workWidth();
229 void BufferView::toggleSelection(bool b)
231 pimpl_->toggleSelection(b);
235 void BufferView::center()
241 string const BufferView::getClipboard() const
243 return pimpl_->workarea().getClipboard();
247 void BufferView::stuffClipboard(string const & stuff) const
249 pimpl_->stuffClipboard(stuff);
253 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
254 BufferView::UpdateCodes uc2)
256 return static_cast<BufferView::UpdateCodes>
257 (static_cast<int>(uc1) | static_cast<int>(uc2));
261 bool BufferView::dispatch(FuncRequest const & ev)
263 return pimpl_->dispatch(ev);
267 void BufferView::scroll(int lines)
269 pimpl_->scroll(lines);
273 // Inserts a file into current document
274 bool BufferView::insertLyXFile(string const & filen)
276 // Copyright CHT Software Service GmbH
279 // Insert a LyXformat - file into current buffer
281 // Moved from lyx_cb.C (Lgb)
286 string const fname = MakeAbsPath(filen);
288 // check if file exist
289 FileInfo const fi(fname);
291 if (!fi.readable()) {
292 string const file = MakeDisplayPath(fname, 50);
294 bformat(_("The specified document\n%1$s\ncould not be read."), file);
295 Alert::error(_("Could not read document"), text);
301 ifstream ifs(fname.c_str());
303 string const file = MakeDisplayPath(fname, 50);
305 bformat(_("Could not open the specified document %1$s\n"), file);
306 Alert::error(_("Could not open file"), text);
310 int const c = ifs.peek();
317 text->breakParagraph(buffer()->paragraphs);
320 // FIXME: huh ? No we won't !
321 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
322 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
324 lyxerr[Debug::INFO] << "Will insert file without header"
326 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
334 void BufferView::showErrorList(string const & action) const
336 if (getErrorList().size()) {
337 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
338 owner()->getDialogs().show("errorlist", title);
339 pimpl_->errorlist_.clear();
345 BufferView::getErrorList() const
347 return pimpl_->errorlist_;
351 void BufferView::setCursorFromRow(int row)
356 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
358 ParagraphList::iterator texrowpar;
361 texrowpar = text->ownerParagraphs().begin();
364 texrowpar = buffer()->getParFromID(tmpid).pit();
366 text->setCursor(texrowpar, tmppos);
370 bool BufferView::insertInset(InsetOld * inset, string const & lout)
372 return pimpl_->insertInset(inset, lout);
376 void BufferView::gotoLabel(string const & label)
378 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
379 it != buffer()->inset_iterator_end(); ++it) {
380 vector<string> labels;
381 it->getLabelList(labels);
382 if (find(labels.begin(),labels.end(),label)
385 text->setCursor(it.getPar(), it.getPos());
386 text->selection.cursor = text->cursor;
387 update(text, BufferView::SELECT);
394 void BufferView::undo()
399 owner()->message(_("Undo"));
401 update(text, BufferView::SELECT);
403 owner()->message(_("No further undo information"));
405 update(text, BufferView::SELECT);
410 void BufferView::redo()
415 owner()->message(_("Redo"));
417 update(text, BufferView::SELECT);
419 owner()->message(_("No further redo information"));
421 update(text, BufferView::SELECT);
426 // these functions are for the spellchecker
427 WordLangTuple const BufferView::nextWord(float & value)
431 return WordLangTuple();
434 return text->selectNextWordToSpellcheck(value);
438 void BufferView::selectLastWord()
443 LyXCursor cur = text->selection.cursor;
445 text->selection.cursor = cur;
446 text->selectSelectedWord();
447 toggleSelection(false);
448 update(text, BufferView::SELECT);
452 void BufferView::endOfSpellCheck()
454 if (!available()) return;
457 text->selectSelectedWord();
458 text->clearSelection();
459 update(text, BufferView::SELECT);
463 void BufferView::replaceWord(string const & replacestring)
468 LyXText * tt = getLyXText();
469 update(tt, BufferView::SELECT);
471 // clear the selection (if there is any)
472 toggleSelection(false);
473 update(tt, BufferView::SELECT);
475 // clear the selection (if there is any)
476 toggleSelection(false);
477 tt->replaceSelectionWithString(replacestring);
479 tt->setSelectionRange(replacestring.length());
481 // Go back so that replacement string is also spellchecked
482 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
483 tt->cursorLeft(this);
485 update(tt, BufferView::SELECT);
487 // FIXME: should be done through LFUN
488 buffer()->markDirty();
491 // End of spellchecker stuff
494 bool BufferView::lockInset(UpdatableInset * inset)
498 // don't relock if we're already locked
499 if (theLockingInset() == inset)
501 if (!theLockingInset()) {
502 // first check if it's the inset under the cursor we want lock
503 // should be most of the time
504 if (text->cursor.pos() < text->cursor.par()->size()
505 && text->cursor.par()->getChar(text->cursor.pos()) ==
506 Paragraph::META_INSET) {
507 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
509 theLockingInset(inset);
513 // Then do a deep look of the inset and lock the right one
514 int const id = inset->id();
515 ParagraphList::iterator pit = buffer()->paragraphs.begin();
516 ParagraphList::iterator pend = buffer()->paragraphs.end();
517 for (; pit != pend; ++pit) {
518 InsetList::iterator it = pit->insetlist.begin();
519 InsetList::iterator end = pit->insetlist.end();
520 for (; it != end; ++it) {
521 if (it->inset == inset) {
522 text->setCursorIntern(pit, it->pos);
523 theLockingInset(inset);
526 if (it->inset->getInsetFromID(id)) {
527 text->setCursorIntern(pit, it->pos);
528 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
529 it->inset->localDispatch(cmd);
530 return theLockingInset()->lockInsetInInset(this, inset);
536 return theLockingInset()->lockInsetInInset(this, inset);
540 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
542 if (theLockingInset() && available()) {
543 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
544 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
553 void BufferView::hideCursor()
555 screen().hideCursor();
559 int BufferView::unlockInset(UpdatableInset * inset)
563 if (inset && theLockingInset() == inset) {
564 inset->insetUnlock(this);
566 // make sure we update the combo !
567 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
568 // Tell the paragraph dialog that we changed paragraph
569 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
572 } else if (inset && theLockingInset() &&
573 theLockingInset()->unlockInsetInInset(this, inset)) {
574 // Tell the paragraph dialog that we changed paragraph
575 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
576 // owner inset has updated the layout combo
584 void BufferView::updateInset(InsetOld * inset)
586 pimpl_->updateInset(inset);
590 bool BufferView::ChangeInsets(InsetOld::Code code,
591 string const & from, string const & to)
593 bool need_update = false;
594 LyXCursor cursor = text->cursor;
595 LyXCursor tmpcursor = cursor;
596 cursor.par(tmpcursor.par());
597 cursor.pos(tmpcursor.pos());
599 ParIterator end = buffer()->par_iterator_end();
600 for (ParIterator it = buffer()->par_iterator_begin();
602 bool changed_inset = false;
603 for (InsetList::iterator it2 = it->insetlist.begin();
604 it2 != it->insetlist.end(); ++it2) {
605 if (it2->inset->lyxCode() == code) {
606 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
607 if (inset->getContents() == from) {
608 inset->setContents(to);
609 changed_inset = true;
618 // The test it.size()==1 was needed to prevent crashes.
619 // How to set the cursor corretly when it.size()>1 ??
620 if (it.size() == 1) {
621 text->setCursorIntern(it.pit(), 0);
622 text->redoParagraph(text->cursor.par());
623 text->partialRebreak();
627 text->setCursorIntern(cursor.par(), cursor.pos());
632 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
634 // Check if the label 'from' appears more than once
635 vector<string> labels;
636 buffer()->getLabelList(labels);
638 if (lyx::count(labels.begin(), labels.end(), from) > 1)
641 return ChangeInsets(InsetOld::REF_CODE, from, to);
645 UpdatableInset * BufferView::theLockingInset() const
647 // If NULL is not allowed we should put an Assert here. (Lgb)
649 return text->the_locking_inset;
654 void BufferView::theLockingInset(UpdatableInset * inset)
656 text->the_locking_inset = inset;
660 LyXText * BufferView::getLyXText() const
662 if (theLockingInset()) {
663 LyXText * txt = theLockingInset()->getLyXText(this, true);
671 LyXText * BufferView::getParentText(InsetOld * inset) const
673 if (inset->owner()) {
674 LyXText * txt = inset->getLyXText(this);
675 inset = inset->owner();
676 while (inset && inset->getLyXText(this) == txt)
677 inset = inset->owner();
679 return inset->getLyXText(this);
685 Language const * BufferView::getParentLanguage(InsetOld * inset) const
687 LyXText * text = getParentText(inset);
688 return text->cursor.par()->getFontSettings(buffer()->params,
689 text->cursor.pos()).language();
693 Encoding const * BufferView::getEncoding() const
695 LyXText * t = getLyXText();
699 LyXCursor const & c = t->cursor;
700 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
701 outerFont(c.par(), t->ownerParagraphs()));
702 return font.language()->encoding();
706 void BufferView::haveSelection(bool sel)
708 pimpl_->workarea().haveSelection(sel);
712 int BufferView::workHeight() const
714 return pimpl_->workarea().workHeight();