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::showErrorList(string const & action) const
341 if (getErrorList().size()) {
342 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
343 owner()->getDialogs().show("errorlist", title);
349 BufferView::getErrorList() const
351 return pimpl_->errorlist_;
352 pimpl_->errorlist_.clear();
356 void BufferView::setCursorFromRow(int row)
361 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
363 ParagraphList::iterator texrowpar;
366 texrowpar = text->ownerParagraphs().begin();
369 texrowpar = buffer()->getParFromID(tmpid).pit();
371 text->setCursor(texrowpar, tmppos);
375 bool BufferView::insertInset(Inset * inset, string const & lout)
377 return pimpl_->insertInset(inset, lout);
381 void BufferView::gotoLabel(string const & label)
383 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
384 it != buffer()->inset_iterator_end(); ++it) {
385 vector<string> labels;
386 it->getLabelList(labels);
387 if (find(labels.begin(),labels.end(),label)
390 text->setCursor(it.getPar(), it.getPos());
391 text->selection.cursor = text->cursor;
392 update(text, BufferView::SELECT);
399 void BufferView::undo()
404 owner()->message(_("Undo"));
406 update(text, BufferView::SELECT);
408 owner()->message(_("No further undo information"));
410 update(text, BufferView::SELECT);
415 void BufferView::redo()
420 owner()->message(_("Redo"));
422 update(text, BufferView::SELECT);
424 owner()->message(_("No further redo information"));
426 update(text, BufferView::SELECT);
431 // these functions are for the spellchecker
432 WordLangTuple const BufferView::nextWord(float & value)
436 return WordLangTuple();
439 return text->selectNextWordToSpellcheck(value);
443 void BufferView::selectLastWord()
448 LyXCursor cur = text->selection.cursor;
450 text->selection.cursor = cur;
451 text->selectSelectedWord();
452 toggleSelection(false);
453 update(text, BufferView::SELECT);
457 void BufferView::endOfSpellCheck()
459 if (!available()) return;
462 text->selectSelectedWord();
463 text->clearSelection();
464 update(text, BufferView::SELECT);
468 void BufferView::replaceWord(string const & replacestring)
473 LyXText * tt = getLyXText();
474 update(tt, BufferView::SELECT);
476 // clear the selection (if there is any)
477 toggleSelection(false);
478 update(tt, BufferView::SELECT);
480 // clear the selection (if there is any)
481 toggleSelection(false);
482 tt->replaceSelectionWithString(replacestring);
484 tt->setSelectionRange(replacestring.length());
486 // Go back so that replacement string is also spellchecked
487 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
488 tt->cursorLeft(this);
490 update(tt, BufferView::SELECT);
492 // FIXME: should be done through LFUN
493 buffer()->markDirty();
496 // End of spellchecker stuff
499 bool BufferView::lockInset(UpdatableInset * inset)
503 // don't relock if we're already locked
504 if (theLockingInset() == inset)
506 if (!theLockingInset()) {
507 // first check if it's the inset under the cursor we want lock
508 // should be most of the time
509 if (text->cursor.pos() < text->cursor.par()->size()
510 && text->cursor.par()->getChar(text->cursor.pos()) ==
511 Paragraph::META_INSET) {
512 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
514 theLockingInset(inset);
518 // Then do a deep look of the inset and lock the right one
519 int const id = inset->id();
520 ParagraphList::iterator pit = buffer()->paragraphs.begin();
521 ParagraphList::iterator pend = buffer()->paragraphs.end();
522 for (; pit != pend; ++pit) {
523 InsetList::iterator it = pit->insetlist.begin();
524 InsetList::iterator end = pit->insetlist.end();
525 for (; it != end; ++it) {
526 if (it->inset == inset) {
527 text->setCursorIntern(pit, it->pos);
528 theLockingInset(inset);
531 if (it->inset->getInsetFromID(id)) {
532 text->setCursorIntern(pit, it->pos);
533 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
534 it->inset->localDispatch(cmd);
535 return theLockingInset()->lockInsetInInset(this, inset);
541 return theLockingInset()->lockInsetInInset(this, inset);
545 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
547 if (theLockingInset() && available()) {
548 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
549 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
558 void BufferView::hideCursor()
560 screen().hideCursor();
564 int BufferView::unlockInset(UpdatableInset * inset)
568 if (inset && theLockingInset() == inset) {
569 inset->insetUnlock(this);
571 // make sure we update the combo !
572 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
573 // Tell the paragraph dialog that we changed paragraph
574 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
577 } else if (inset && theLockingInset() &&
578 theLockingInset()->unlockInsetInInset(this, inset)) {
579 // Tell the paragraph dialog that we changed paragraph
580 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
581 // owner inset has updated the layout combo
589 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
591 if (!theLockingInset())
592 return; // shouldn't happen
593 if (kind == Undo::EDIT) // in this case insets would not be stored!
595 setUndo(this, kind, text->cursor.par());
599 void BufferView::updateInset(Inset * inset)
601 pimpl_->updateInset(inset);
605 bool BufferView::ChangeInsets(Inset::Code code,
606 string const & from, string const & to)
608 bool need_update = false;
609 LyXCursor cursor = text->cursor;
610 LyXCursor tmpcursor = cursor;
611 cursor.par(tmpcursor.par());
612 cursor.pos(tmpcursor.pos());
614 ParIterator end = buffer()->par_iterator_end();
615 for (ParIterator it = buffer()->par_iterator_begin();
617 bool changed_inset = false;
618 for (InsetList::iterator it2 = it->insetlist.begin();
619 it2 != it->insetlist.end(); ++it2) {
620 if (it2->inset->lyxCode() == code) {
621 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
622 if (inset->getContents() == from) {
623 inset->setContents(to);
624 changed_inset = true;
633 // The test it.size()==1 was needed to prevent crashes.
634 // How to set the cursor corretly when it.size()>1 ??
635 if (it.size() == 1) {
636 text->setCursorIntern(it.pit(), 0);
637 text->redoParagraphs(text->cursor,
638 boost::next(text->cursor.par()));
643 text->setCursorIntern(cursor.par(), cursor.pos());
648 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
650 // Check if the label 'from' appears more than once
651 vector<string> labels;
652 buffer()->getLabelList(labels);
654 if (lyx::count(labels.begin(), labels.end(), from) > 1)
657 return ChangeInsets(Inset::REF_CODE, from, to);
661 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
663 typedef pair<string, string> StringPair;
665 vector<StringPair> keys;
666 buffer()->fillWithBibKeys(keys);
667 if (count_if(keys.begin(), keys.end(),
668 lyx::equal_1st_in_pair<StringPair>(from))
672 return ChangeInsets(Inset::CITE_CODE, from, to);
676 UpdatableInset * BufferView::theLockingInset() const
678 // If NULL is not allowed we should put an Assert here. (Lgb)
680 return text->the_locking_inset;
685 void BufferView::theLockingInset(UpdatableInset * inset)
687 text->the_locking_inset = inset;
691 LyXText * BufferView::getLyXText() const
693 if (theLockingInset()) {
694 LyXText * txt = theLockingInset()->getLyXText(this, true);
702 LyXText * BufferView::getParentText(Inset * inset) const
704 if (inset->owner()) {
705 LyXText * txt = inset->getLyXText(this);
706 inset = inset->owner();
707 while (inset && inset->getLyXText(this) == txt)
708 inset = inset->owner();
710 return inset->getLyXText(this);
716 Language const * BufferView::getParentLanguage(Inset * inset) const
718 LyXText * text = getParentText(inset);
719 return text->cursor.par()->getFontSettings(buffer()->params,
720 text->cursor.pos()).language();
724 Encoding const * BufferView::getEncoding() const
726 LyXText * t = getLyXText();
730 LyXCursor const & c = t->cursor;
731 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
732 outerFont(c.par(), t->ownerParagraphs()));
733 return font.language()->encoding();
737 void BufferView::haveSelection(bool sel)
739 pimpl_->workarea().haveSelection(sel);
743 int BufferView::workHeight() const
745 return pimpl_->workarea().workHeight();