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/inseterror.h"
40 #include "insets/updatableinset.h"
42 #include "support/FileInfo.h"
43 #include "support/filetools.h"
44 #include "support/lyxfunctional.h" // equal_1st_in_pair
45 #include "support/types.h"
46 #include "support/lyxalgo.h" // lyx_count
50 extern BufferList bufferlist;
62 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
63 int width, int height)
64 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
70 BufferView::~BufferView()
77 Buffer * BufferView::buffer() const
79 return pimpl_->buffer_;
83 LyXScreen & BufferView::screen() const
85 return pimpl_->screen();
89 LyXView * BufferView::owner() const
91 return pimpl_->owner_;
95 Painter & BufferView::painter() const
97 return pimpl_->painter();
101 void BufferView::buffer(Buffer * b)
107 void BufferView::reload()
109 string const fn = buffer()->fileName();
110 if (bufferlist.close(buffer(), false))
111 buffer(bufferlist.loadLyXFile(fn));
115 void BufferView::resize()
117 if (pimpl_->buffer_) {
118 pimpl_->resizeCurrentBuffer();
123 void BufferView::repaint()
129 bool BufferView::fitCursor()
131 return pimpl_->fitCursor();
135 void BufferView::update()
141 void BufferView::updateScrollbar()
143 pimpl_->updateScrollbar();
147 void BufferView::scrollDocView(int value)
149 pimpl_->scrollDocView(value);
153 void BufferView::redoCurrentBuffer()
155 pimpl_->redoCurrentBuffer();
159 bool BufferView::available() const
161 return pimpl_->available();
165 Change const BufferView::getCurrentChange()
167 return pimpl_->getCurrentChange();
171 void BufferView::beforeChange(LyXText * text)
173 pimpl_->beforeChange(text);
177 void BufferView::savePosition(unsigned int i)
179 pimpl_->savePosition(i);
183 void BufferView::restorePosition(unsigned int i)
185 pimpl_->restorePosition(i);
189 bool BufferView::isSavedPosition(unsigned int i)
191 return pimpl_->isSavedPosition(i);
195 void BufferView::update(LyXText * text, UpdateCodes f)
197 pimpl_->update(text, f);
201 void BufferView::update(UpdateCodes f)
207 void BufferView::switchKeyMap()
209 pimpl_->switchKeyMap();
213 void BufferView::insetUnlock()
215 pimpl_->insetUnlock();
219 int BufferView::workWidth() const
221 return pimpl_->workarea().workWidth();
225 void BufferView::toggleSelection(bool b)
227 pimpl_->toggleSelection(b);
231 void BufferView::toggleToggle()
233 pimpl_->toggleToggle();
237 void BufferView::center()
243 string const BufferView::getClipboard() const
245 return pimpl_->workarea().getClipboard();
249 void BufferView::stuffClipboard(string const & stuff) const
251 pimpl_->stuffClipboard(stuff);
255 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
256 BufferView::UpdateCodes uc2)
258 return static_cast<BufferView::UpdateCodes>
259 (static_cast<int>(uc1) | static_cast<int>(uc2));
263 bool BufferView::dispatch(FuncRequest const & ev)
265 return pimpl_->dispatch(ev);
269 void BufferView::scroll(int lines)
271 pimpl_->scroll(lines);
275 // Inserts a file into current document
276 bool BufferView::insertLyXFile(string const & filen)
278 // Copyright CHT Software Service GmbH
281 // Insert a LyXformat - file into current buffer
283 // Moved from lyx_cb.C (Lgb)
288 string const fname = MakeAbsPath(filen);
290 // check if file exist
291 FileInfo const fi(fname);
293 if (!fi.readable()) {
294 string const file = MakeDisplayPath(fname, 50);
296 bformat(_("The specified document\n%1$s\ncould not be read."), file);
297 Alert::error(_("Could not read document"), text);
303 ifstream ifs(fname.c_str());
305 string const file = MakeDisplayPath(fname, 50);
307 bformat(_("Could not open the specified document %1$s\n"), file);
308 Alert::error(_("Could not open file"), text);
312 int const c = ifs.peek();
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::resetErrorList()
336 pimpl_->errorlist_.clear();
340 void BufferView::setErrorList(ErrorList const & el)
342 pimpl_->errorlist_ = el;
346 void BufferView::addError(ErrorItem const & ei)
348 pimpl_->errorlist_.push_back(ei);
353 void BufferView::showErrorList(string const & action) const
355 if (getErrorList().size()) {
356 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
357 owner()->getDialogs().show("errorlist", title);
363 BufferView::getErrorList() const
365 return pimpl_->errorlist_;
369 void BufferView::setCursorFromRow(int row)
374 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
376 ParagraphList::iterator texrowpar;
379 texrowpar = text->ownerParagraphs().begin();
382 texrowpar = *buffer()->getParFromID(tmpid);
384 text->setCursor(texrowpar, tmppos);
388 bool BufferView::insertInset(Inset * inset, string const & lout)
390 return pimpl_->insertInset(inset, lout);
394 void BufferView::gotoLabel(string const & label)
396 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
397 it != buffer()->inset_iterator_end(); ++it) {
398 vector<string> labels = it->getLabelList();
399 if (find(labels.begin(),labels.end(),label)
402 text->setCursor(it.getPar(), it.getPos());
403 text->selection.cursor = text->cursor;
404 update(text, BufferView::SELECT);
411 void BufferView::undo()
416 owner()->message(_("Undo"));
418 update(text, BufferView::SELECT);
420 owner()->message(_("No further undo information"));
422 update(text, BufferView::SELECT);
427 void BufferView::redo()
432 owner()->message(_("Redo"));
434 update(text, BufferView::SELECT);
436 owner()->message(_("No further redo information"));
438 update(text, BufferView::SELECT);
443 // these functions are for the spellchecker
444 WordLangTuple const BufferView::nextWord(float & value)
448 return WordLangTuple();
451 return text->selectNextWordToSpellcheck(value);
455 void BufferView::selectLastWord()
460 LyXCursor cur = text->selection.cursor;
462 text->selection.cursor = cur;
463 text->selectSelectedWord();
464 toggleSelection(false);
465 update(text, BufferView::SELECT);
469 void BufferView::endOfSpellCheck()
471 if (!available()) return;
474 text->selectSelectedWord();
475 text->clearSelection();
476 update(text, BufferView::SELECT);
480 void BufferView::replaceWord(string const & replacestring)
485 LyXText * tt = getLyXText();
486 update(tt, BufferView::SELECT);
488 // clear the selection (if there is any)
489 toggleSelection(false);
490 update(tt, BufferView::SELECT);
492 // clear the selection (if there is any)
493 toggleSelection(false);
494 tt->replaceSelectionWithString(replacestring);
496 tt->setSelectionRange(replacestring.length());
498 // Go back so that replacement string is also spellchecked
499 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
500 tt->cursorLeft(this);
502 update(tt, BufferView::SELECT);
504 // FIXME: should be done through LFUN
505 buffer()->markDirty();
508 // End of spellchecker stuff
511 bool BufferView::lockInset(UpdatableInset * inset)
515 // don't relock if we're already locked
516 if (theLockingInset() == inset)
518 if (!theLockingInset()) {
519 // first check if it's the inset under the cursor we want lock
520 // should be most of the time
521 if (text->cursor.pos() < text->cursor.par()->size()
522 && text->cursor.par()->getChar(text->cursor.pos()) ==
523 Paragraph::META_INSET) {
524 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
526 theLockingInset(inset);
530 // Then do a deep look of the inset and lock the right one
531 int const id = inset->id();
532 ParagraphList::iterator pit = buffer()->paragraphs.begin();
533 ParagraphList::iterator pend = buffer()->paragraphs.end();
534 for (; pit != pend; ++pit) {
535 InsetList::iterator it = pit->insetlist.begin();
536 InsetList::iterator end = pit->insetlist.end();
537 for (; it != end; ++it) {
538 if (it.getInset() == inset) {
539 text->setCursorIntern(pit, it.getPos());
540 theLockingInset(inset);
543 if (it.getInset()->getInsetFromID(id)) {
544 text->setCursorIntern(pit, it.getPos());
545 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
546 it.getInset()->localDispatch(cmd);
547 return theLockingInset()->lockInsetInInset(this, inset);
553 return theLockingInset()->lockInsetInInset(this, inset);
557 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
559 if (theLockingInset() && available()) {
560 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
561 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
570 void BufferView::hideCursor()
572 screen().hideCursor();
576 int BufferView::unlockInset(UpdatableInset * inset)
580 if (inset && theLockingInset() == inset) {
581 inset->insetUnlock(this);
583 // make sure we update the combo !
584 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
585 // Tell the paragraph dialog that we changed paragraph
586 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
589 } else if (inset && theLockingInset() &&
590 theLockingInset()->unlockInsetInInset(this, inset)) {
591 // Tell the paragraph dialog that we changed paragraph
592 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
593 // owner inset has updated the layout combo
601 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
603 if (!theLockingInset())
604 return; // shouldn't happen
605 if (kind == Undo::EDIT) // in this case insets would not be stored!
607 setUndo(this, kind, text->cursor.par());
611 void BufferView::updateInset(Inset * inset)
613 pimpl_->updateInset(inset);
617 bool BufferView::ChangeInsets(Inset::Code code,
618 string const & from, string const & to)
620 bool need_update = false;
621 LyXCursor cursor = text->cursor;
622 LyXCursor tmpcursor = cursor;
623 cursor.par(tmpcursor.par());
624 cursor.pos(tmpcursor.pos());
626 ParIterator end = buffer()->par_iterator_end();
627 for (ParIterator it = buffer()->par_iterator_begin();
629 bool changed_inset = false;
630 for (InsetList::iterator it2 = it->insetlist.begin();
631 it2 != it->insetlist.end(); ++it2) {
632 if (it2.getInset()->lyxCode() == code) {
633 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
634 if (inset->getContents() == from) {
635 inset->setContents(to);
636 changed_inset = true;
645 // The test it.size()==1 was needed to prevent crashes.
646 // How to set the cursor corretly when it.size()>1 ??
647 if (it.size() == 1) {
648 text->setCursorIntern(*it, 0);
649 text->redoParagraphs(text->cursor,
650 boost::next(text->cursor.par()));
655 text->setCursorIntern(cursor.par(), cursor.pos());
660 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
662 // Check if the label 'from' appears more than once
663 vector<string> labels = buffer()->getLabelList();
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();