3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
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/lyxfunctional.h" // equal_1st_in_pair
44 #include "support/types.h"
45 #include "support/lyxalgo.h" // lyx_count
49 extern BufferList bufferlist;
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::loadLyXFile(string const & fn, bool tl)
108 return pimpl_->loadLyXFile(fn, tl);
112 void BufferView::reload()
114 string const fn = buffer()->fileName();
115 if (bufferlist.close(buffer(), false))
120 void BufferView::resize()
122 if (pimpl_->buffer_) {
123 pimpl_->resizeCurrentBuffer();
128 void BufferView::repaint()
134 bool BufferView::fitCursor()
136 return pimpl_->fitCursor();
140 void BufferView::update()
146 void BufferView::updateScrollbar()
148 pimpl_->updateScrollbar();
152 void BufferView::scrollDocView(int value)
154 pimpl_->scrollDocView(value);
158 void BufferView::redoCurrentBuffer()
160 pimpl_->redoCurrentBuffer();
164 bool BufferView::available() const
166 return pimpl_->available();
170 Change const BufferView::getCurrentChange()
172 return pimpl_->getCurrentChange();
176 void BufferView::beforeChange(LyXText * text)
178 pimpl_->beforeChange(text);
182 void BufferView::savePosition(unsigned int i)
184 pimpl_->savePosition(i);
188 void BufferView::restorePosition(unsigned int i)
190 pimpl_->restorePosition(i);
194 bool BufferView::isSavedPosition(unsigned int i)
196 return pimpl_->isSavedPosition(i);
200 void BufferView::update(LyXText * text, UpdateCodes f)
202 pimpl_->update(text, f);
206 void BufferView::update(UpdateCodes f)
212 void BufferView::switchKeyMap()
214 pimpl_->switchKeyMap();
218 void BufferView::insetUnlock()
220 pimpl_->insetUnlock();
224 int BufferView::workWidth() const
226 return pimpl_->workarea().workWidth();
230 void BufferView::toggleSelection(bool b)
232 pimpl_->toggleSelection(b);
236 void BufferView::toggleToggle()
238 pimpl_->toggleToggle();
242 void BufferView::center()
248 string const BufferView::getClipboard() const
250 return pimpl_->workarea().getClipboard();
254 void BufferView::stuffClipboard(string const & stuff) const
256 pimpl_->stuffClipboard(stuff);
260 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
261 BufferView::UpdateCodes uc2)
263 return static_cast<BufferView::UpdateCodes>
264 (static_cast<int>(uc1) | static_cast<int>(uc2));
268 bool BufferView::dispatch(FuncRequest const & ev)
270 return pimpl_->dispatch(ev);
274 void BufferView::scroll(int lines)
276 pimpl_->scroll(lines);
280 // Inserts a file into current document
281 bool BufferView::insertLyXFile(string const & filen)
283 // Copyright CHT Software Service GmbH
286 // Insert a LyXformat - file into current buffer
288 // Moved from lyx_cb.C (Lgb)
293 string const fname = MakeAbsPath(filen);
295 // check if file exist
296 FileInfo const fi(fname);
298 if (!fi.readable()) {
299 string const file = MakeDisplayPath(fname, 50);
301 bformat(_("The specified document\n%1$s\ncould not be read."), file);
302 Alert::error(_("Could not read document"), text);
308 ifstream ifs(fname.c_str());
310 string const file = MakeDisplayPath(fname, 50);
312 bformat(_("Could not open the specified document %1$s\n"), file);
313 Alert::error(_("Could not open file"), text);
317 int const c = ifs.peek();
325 // FIXME: huh ? No we won't !
326 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
327 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
329 lyxerr[Debug::INFO] << "Will insert file without header"
331 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
339 void BufferView::resetErrorList()
341 pimpl_->errorlist_.clear();
345 void BufferView::setErrorList(ErrorList const & el)
347 pimpl_->errorlist_ = el;
351 void BufferView::showErrorList(string const & action) const
353 if (getErrorList().size()) {
354 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
355 owner()->getDialogs().show("errorlist", title);
361 BufferView::getErrorList() const
363 return pimpl_->errorlist_;
367 void BufferView::setCursorFromRow(int row)
372 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
374 ParagraphList::iterator texrowpar;
377 texrowpar = text->ownerParagraphs().begin();
380 texrowpar = buffer()->getParFromID(tmpid).pit();
382 text->setCursor(texrowpar, tmppos);
386 bool BufferView::insertInset(Inset * inset, string const & lout)
388 return pimpl_->insertInset(inset, lout);
392 void BufferView::gotoLabel(string const & label)
394 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
395 it != buffer()->inset_iterator_end(); ++it) {
396 vector<string> labels;
397 it->getLabelList(labels);
398 if (find(labels.begin(),labels.end(),label)
401 text->setCursor(it.getPar(), it.getPos());
402 text->selection.cursor = text->cursor;
403 update(text, BufferView::SELECT);
410 void BufferView::undo()
415 owner()->message(_("Undo"));
417 update(text, BufferView::SELECT);
419 owner()->message(_("No further undo information"));
421 update(text, BufferView::SELECT);
426 void BufferView::redo()
431 owner()->message(_("Redo"));
433 update(text, BufferView::SELECT);
435 owner()->message(_("No further redo information"));
437 update(text, BufferView::SELECT);
442 // these functions are for the spellchecker
443 WordLangTuple const BufferView::nextWord(float & value)
447 return WordLangTuple();
450 return text->selectNextWordToSpellcheck(value);
454 void BufferView::selectLastWord()
459 LyXCursor cur = text->selection.cursor;
461 text->selection.cursor = cur;
462 text->selectSelectedWord();
463 toggleSelection(false);
464 update(text, BufferView::SELECT);
468 void BufferView::endOfSpellCheck()
470 if (!available()) return;
473 text->selectSelectedWord();
474 text->clearSelection();
475 update(text, BufferView::SELECT);
479 void BufferView::replaceWord(string const & replacestring)
484 LyXText * tt = getLyXText();
485 update(tt, BufferView::SELECT);
487 // clear the selection (if there is any)
488 toggleSelection(false);
489 update(tt, BufferView::SELECT);
491 // clear the selection (if there is any)
492 toggleSelection(false);
493 tt->replaceSelectionWithString(replacestring);
495 tt->setSelectionRange(replacestring.length());
497 // Go back so that replacement string is also spellchecked
498 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
499 tt->cursorLeft(this);
501 update(tt, BufferView::SELECT);
503 // FIXME: should be done through LFUN
504 buffer()->markDirty();
507 // End of spellchecker stuff
510 bool BufferView::lockInset(UpdatableInset * inset)
514 // don't relock if we're already locked
515 if (theLockingInset() == inset)
517 if (!theLockingInset()) {
518 // first check if it's the inset under the cursor we want lock
519 // should be most of the time
520 if (text->cursor.pos() < text->cursor.par()->size()
521 && text->cursor.par()->getChar(text->cursor.pos()) ==
522 Paragraph::META_INSET) {
523 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
525 theLockingInset(inset);
529 // Then do a deep look of the inset and lock the right one
530 int const id = inset->id();
531 ParagraphList::iterator pit = buffer()->paragraphs.begin();
532 ParagraphList::iterator pend = buffer()->paragraphs.end();
533 for (; pit != pend; ++pit) {
534 InsetList::iterator it = pit->insetlist.begin();
535 InsetList::iterator end = pit->insetlist.end();
536 for (; it != end; ++it) {
537 if (it->inset == inset) {
538 text->setCursorIntern(pit, it->pos);
539 theLockingInset(inset);
542 if (it->inset->getInsetFromID(id)) {
543 text->setCursorIntern(pit, it->pos);
544 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
545 it->inset->localDispatch(cmd);
546 return theLockingInset()->lockInsetInInset(this, inset);
552 return theLockingInset()->lockInsetInInset(this, inset);
556 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
558 if (theLockingInset() && available()) {
559 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
560 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
569 void BufferView::hideCursor()
571 screen().hideCursor();
575 int BufferView::unlockInset(UpdatableInset * inset)
579 if (inset && theLockingInset() == inset) {
580 inset->insetUnlock(this);
582 // make sure we update the combo !
583 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
584 // Tell the paragraph dialog that we changed paragraph
585 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
588 } else if (inset && theLockingInset() &&
589 theLockingInset()->unlockInsetInInset(this, inset)) {
590 // Tell the paragraph dialog that we changed paragraph
591 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
592 // owner inset has updated the layout combo
600 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
602 if (!theLockingInset())
603 return; // shouldn't happen
604 if (kind == Undo::EDIT) // in this case insets would not be stored!
606 setUndo(this, kind, text->cursor.par());
610 void BufferView::updateInset(Inset * inset)
612 pimpl_->updateInset(inset);
616 bool BufferView::ChangeInsets(Inset::Code code,
617 string const & from, string const & to)
619 bool need_update = false;
620 LyXCursor cursor = text->cursor;
621 LyXCursor tmpcursor = cursor;
622 cursor.par(tmpcursor.par());
623 cursor.pos(tmpcursor.pos());
625 ParIterator end = buffer()->par_iterator_end();
626 for (ParIterator it = buffer()->par_iterator_begin();
628 bool changed_inset = false;
629 for (InsetList::iterator it2 = it->insetlist.begin();
630 it2 != it->insetlist.end(); ++it2) {
631 if (it2->inset->lyxCode() == code) {
632 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
633 if (inset->getContents() == from) {
634 inset->setContents(to);
635 changed_inset = true;
644 // The test it.size()==1 was needed to prevent crashes.
645 // How to set the cursor corretly when it.size()>1 ??
646 if (it.size() == 1) {
647 text->setCursorIntern(it.pit(), 0);
648 text->redoParagraphs(text->cursor,
649 boost::next(text->cursor.par()));
654 text->setCursorIntern(cursor.par(), cursor.pos());
659 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
661 // Check if the label 'from' appears more than once
662 vector<string> labels;
663 buffer()->getLabelList(labels);
665 if (lyx::count(labels.begin(), labels.end(), from) > 1)
668 return ChangeInsets(Inset::REF_CODE, from, to);
672 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
674 typedef pair<string, string> StringPair;
676 vector<StringPair> keys;
677 buffer()->fillWithBibKeys(keys);
678 if (count_if(keys.begin(), keys.end(),
679 lyx::equal_1st_in_pair<StringPair>(from))
683 return ChangeInsets(Inset::CITE_CODE, from, to);
687 UpdatableInset * BufferView::theLockingInset() const
689 // If NULL is not allowed we should put an Assert here. (Lgb)
691 return text->the_locking_inset;
696 void BufferView::theLockingInset(UpdatableInset * inset)
698 text->the_locking_inset = inset;
702 LyXText * BufferView::getLyXText() const
704 if (theLockingInset()) {
705 LyXText * txt = theLockingInset()->getLyXText(this, true);
713 LyXText * BufferView::getParentText(Inset * inset) const
715 if (inset->owner()) {
716 LyXText * txt = inset->getLyXText(this);
717 inset = inset->owner();
718 while (inset && inset->getLyXText(this) == txt)
719 inset = inset->owner();
721 return inset->getLyXText(this);
727 Language const * BufferView::getParentLanguage(Inset * inset) const
729 LyXText * text = getParentText(inset);
730 return text->cursor.par()->getFontSettings(buffer()->params,
731 text->cursor.pos()).language();
735 Encoding const * BufferView::getEncoding() const
737 LyXText * t = getLyXText();
741 LyXCursor const & c = t->cursor;
742 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
743 outerFont(c.par(), t->ownerParagraphs()));
744 return font.language()->encoding();
748 void BufferView::haveSelection(bool sel)
750 pimpl_->workarea().haveSelection(sel);
754 int BufferView::workHeight() const
756 return pimpl_->workarea().workHeight();