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/lyxfunctional.h" // equal_1st_in_pair
44 #include "support/types.h"
45 #include "support/lyxalgo.h" // lyx_count
49 extern BufferList bufferlist;
52 using namespace lyx::support;
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 bool BufferView::newFile(string const & fn, string const & tn, bool named)
109 return pimpl_->newFile(fn, tn, named);
113 bool BufferView::loadLyXFile(string const & fn, bool tl)
115 return pimpl_->loadLyXFile(fn, tl);
119 void BufferView::reload()
121 string const fn = buffer()->fileName();
122 if (bufferlist.close(buffer(), false))
127 void BufferView::resize()
129 if (pimpl_->buffer_) {
130 pimpl_->resizeCurrentBuffer();
135 void BufferView::repaint()
141 bool BufferView::fitCursor()
143 return pimpl_->fitCursor();
147 void BufferView::update()
153 void BufferView::updateScrollbar()
155 pimpl_->updateScrollbar();
159 void BufferView::scrollDocView(int value)
161 pimpl_->scrollDocView(value);
165 void BufferView::redoCurrentBuffer()
167 pimpl_->redoCurrentBuffer();
171 bool BufferView::available() const
173 return pimpl_->available();
177 Change const BufferView::getCurrentChange()
179 return pimpl_->getCurrentChange();
183 void BufferView::beforeChange(LyXText * text)
185 pimpl_->beforeChange(text);
189 void BufferView::savePosition(unsigned int i)
191 pimpl_->savePosition(i);
195 void BufferView::restorePosition(unsigned int i)
197 pimpl_->restorePosition(i);
201 bool BufferView::isSavedPosition(unsigned int i)
203 return pimpl_->isSavedPosition(i);
207 void BufferView::update(LyXText * text, UpdateCodes f)
209 pimpl_->update(text, f);
213 void BufferView::update(UpdateCodes f)
219 void BufferView::switchKeyMap()
221 pimpl_->switchKeyMap();
225 void BufferView::insetUnlock()
227 pimpl_->insetUnlock();
231 int BufferView::workWidth() const
233 return pimpl_->workarea().workWidth();
237 void BufferView::toggleSelection(bool b)
239 pimpl_->toggleSelection(b);
243 void BufferView::toggleToggle()
245 pimpl_->toggleToggle();
249 void BufferView::center()
255 string const BufferView::getClipboard() const
257 return pimpl_->workarea().getClipboard();
261 void BufferView::stuffClipboard(string const & stuff) const
263 pimpl_->stuffClipboard(stuff);
267 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
268 BufferView::UpdateCodes uc2)
270 return static_cast<BufferView::UpdateCodes>
271 (static_cast<int>(uc1) | static_cast<int>(uc2));
275 bool BufferView::dispatch(FuncRequest const & ev)
277 return pimpl_->dispatch(ev);
281 void BufferView::scroll(int lines)
283 pimpl_->scroll(lines);
287 // Inserts a file into current document
288 bool BufferView::insertLyXFile(string const & filen)
290 // Copyright CHT Software Service GmbH
293 // Insert a LyXformat - file into current buffer
295 // Moved from lyx_cb.C (Lgb)
300 string const fname = MakeAbsPath(filen);
302 // check if file exist
303 FileInfo const fi(fname);
305 if (!fi.readable()) {
306 string const file = MakeDisplayPath(fname, 50);
308 bformat(_("The specified document\n%1$s\ncould not be read."), file);
309 Alert::error(_("Could not read document"), text);
315 ifstream ifs(fname.c_str());
317 string const file = MakeDisplayPath(fname, 50);
319 bformat(_("Could not open the specified document %1$s\n"), file);
320 Alert::error(_("Could not open file"), text);
324 int const c = ifs.peek();
332 // FIXME: huh ? No we won't !
333 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
334 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
336 lyxerr[Debug::INFO] << "Will insert file without header"
338 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
346 void BufferView::showErrorList(string const & action) const
348 if (getErrorList().size()) {
349 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
350 owner()->getDialogs().show("errorlist", title);
351 pimpl_->errorlist_.clear();
357 BufferView::getErrorList() const
359 return pimpl_->errorlist_;
363 void BufferView::setCursorFromRow(int row)
368 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
370 ParagraphList::iterator texrowpar;
373 texrowpar = text->ownerParagraphs().begin();
376 texrowpar = buffer()->getParFromID(tmpid).pit();
378 text->setCursor(texrowpar, tmppos);
382 bool BufferView::insertInset(Inset * inset, string const & lout)
384 return pimpl_->insertInset(inset, lout);
388 void BufferView::gotoLabel(string const & label)
390 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
391 it != buffer()->inset_iterator_end(); ++it) {
392 vector<string> labels;
393 it->getLabelList(labels);
394 if (find(labels.begin(),labels.end(),label)
397 text->setCursor(it.getPar(), it.getPos());
398 text->selection.cursor = text->cursor;
399 update(text, BufferView::SELECT);
406 void BufferView::undo()
411 owner()->message(_("Undo"));
413 update(text, BufferView::SELECT);
415 owner()->message(_("No further undo information"));
417 update(text, BufferView::SELECT);
422 void BufferView::redo()
427 owner()->message(_("Redo"));
429 update(text, BufferView::SELECT);
431 owner()->message(_("No further redo information"));
433 update(text, BufferView::SELECT);
438 // these functions are for the spellchecker
439 WordLangTuple const BufferView::nextWord(float & value)
443 return WordLangTuple();
446 return text->selectNextWordToSpellcheck(value);
450 void BufferView::selectLastWord()
455 LyXCursor cur = text->selection.cursor;
457 text->selection.cursor = cur;
458 text->selectSelectedWord();
459 toggleSelection(false);
460 update(text, BufferView::SELECT);
464 void BufferView::endOfSpellCheck()
466 if (!available()) return;
469 text->selectSelectedWord();
470 text->clearSelection();
471 update(text, BufferView::SELECT);
475 void BufferView::replaceWord(string const & replacestring)
480 LyXText * tt = getLyXText();
481 update(tt, BufferView::SELECT);
483 // clear the selection (if there is any)
484 toggleSelection(false);
485 update(tt, BufferView::SELECT);
487 // clear the selection (if there is any)
488 toggleSelection(false);
489 tt->replaceSelectionWithString(replacestring);
491 tt->setSelectionRange(replacestring.length());
493 // Go back so that replacement string is also spellchecked
494 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
495 tt->cursorLeft(this);
497 update(tt, BufferView::SELECT);
499 // FIXME: should be done through LFUN
500 buffer()->markDirty();
503 // End of spellchecker stuff
506 bool BufferView::lockInset(UpdatableInset * inset)
510 // don't relock if we're already locked
511 if (theLockingInset() == inset)
513 if (!theLockingInset()) {
514 // first check if it's the inset under the cursor we want lock
515 // should be most of the time
516 if (text->cursor.pos() < text->cursor.par()->size()
517 && text->cursor.par()->getChar(text->cursor.pos()) ==
518 Paragraph::META_INSET) {
519 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
521 theLockingInset(inset);
525 // Then do a deep look of the inset and lock the right one
526 int const id = inset->id();
527 ParagraphList::iterator pit = buffer()->paragraphs.begin();
528 ParagraphList::iterator pend = buffer()->paragraphs.end();
529 for (; pit != pend; ++pit) {
530 InsetList::iterator it = pit->insetlist.begin();
531 InsetList::iterator end = pit->insetlist.end();
532 for (; it != end; ++it) {
533 if (it->inset == inset) {
534 text->setCursorIntern(pit, it->pos);
535 theLockingInset(inset);
538 if (it->inset->getInsetFromID(id)) {
539 text->setCursorIntern(pit, it->pos);
540 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
541 it->inset->localDispatch(cmd);
542 return theLockingInset()->lockInsetInInset(this, inset);
548 return theLockingInset()->lockInsetInInset(this, inset);
552 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
554 if (theLockingInset() && available()) {
555 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
556 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
565 void BufferView::hideCursor()
567 screen().hideCursor();
571 int BufferView::unlockInset(UpdatableInset * inset)
575 if (inset && theLockingInset() == inset) {
576 inset->insetUnlock(this);
578 // make sure we update the combo !
579 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
580 // Tell the paragraph dialog that we changed paragraph
581 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
584 } else if (inset && theLockingInset() &&
585 theLockingInset()->unlockInsetInInset(this, inset)) {
586 // Tell the paragraph dialog that we changed paragraph
587 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
588 // owner inset has updated the layout combo
596 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
598 if (!theLockingInset())
599 return; // shouldn't happen
600 if (kind == Undo::EDIT) // in this case insets would not be stored!
602 setUndo(this, kind, text->cursor.par());
606 void BufferView::updateInset(Inset * inset)
608 pimpl_->updateInset(inset);
612 bool BufferView::ChangeInsets(Inset::Code code,
613 string const & from, string const & to)
615 bool need_update = false;
616 LyXCursor cursor = text->cursor;
617 LyXCursor tmpcursor = cursor;
618 cursor.par(tmpcursor.par());
619 cursor.pos(tmpcursor.pos());
621 ParIterator end = buffer()->par_iterator_end();
622 for (ParIterator it = buffer()->par_iterator_begin();
624 bool changed_inset = false;
625 for (InsetList::iterator it2 = it->insetlist.begin();
626 it2 != it->insetlist.end(); ++it2) {
627 if (it2->inset->lyxCode() == code) {
628 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
629 if (inset->getContents() == from) {
630 inset->setContents(to);
631 changed_inset = true;
640 // The test it.size()==1 was needed to prevent crashes.
641 // How to set the cursor corretly when it.size()>1 ??
642 if (it.size() == 1) {
643 text->setCursorIntern(it.pit(), 0);
644 text->redoParagraphs(text->cursor,
645 boost::next(text->cursor.par()));
646 text->partialRebreak();
650 text->setCursorIntern(cursor.par(), cursor.pos());
655 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
657 // Check if the label 'from' appears more than once
658 vector<string> labels;
659 buffer()->getLabelList(labels);
661 if (lyx::count(labels.begin(), labels.end(), from) > 1)
664 return ChangeInsets(Inset::REF_CODE, from, to);
668 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
670 typedef pair<string, string> StringPair;
672 vector<StringPair> keys;
673 buffer()->fillWithBibKeys(keys);
674 if (count_if(keys.begin(), keys.end(),
675 lyx::equal_1st_in_pair<StringPair>(from))
679 return ChangeInsets(Inset::CITE_CODE, from, to);
683 UpdatableInset * BufferView::theLockingInset() const
685 // If NULL is not allowed we should put an Assert here. (Lgb)
687 return text->the_locking_inset;
692 void BufferView::theLockingInset(UpdatableInset * inset)
694 text->the_locking_inset = inset;
698 LyXText * BufferView::getLyXText() const
700 if (theLockingInset()) {
701 LyXText * txt = theLockingInset()->getLyXText(this, true);
709 LyXText * BufferView::getParentText(Inset * inset) const
711 if (inset->owner()) {
712 LyXText * txt = inset->getLyXText(this);
713 inset = inset->owner();
714 while (inset && inset->getLyXText(this) == txt)
715 inset = inset->owner();
717 return inset->getLyXText(this);
723 Language const * BufferView::getParentLanguage(Inset * inset) const
725 LyXText * text = getParentText(inset);
726 return text->cursor.par()->getFontSettings(buffer()->params,
727 text->cursor.pos()).language();
731 Encoding const * BufferView::getEncoding() const
733 LyXText * t = getLyXText();
737 LyXCursor const & c = t->cursor;
738 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
739 outerFont(c.par(), t->ownerParagraphs()));
740 return font.language()->encoding();
744 void BufferView::haveSelection(bool sel)
746 pimpl_->workarea().haveSelection(sel);
750 int BufferView::workHeight() const
752 return pimpl_->workarea().workHeight();