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()
130 pimpl_->resizeCurrentBuffer();
134 void BufferView::repaint()
140 bool BufferView::fitCursor()
142 return pimpl_->fitCursor();
146 void BufferView::update()
152 void BufferView::updateScrollbar()
154 pimpl_->updateScrollbar();
158 void BufferView::scrollDocView(int value)
160 pimpl_->scrollDocView(value);
164 void BufferView::redoCurrentBuffer()
166 pimpl_->redoCurrentBuffer();
170 bool BufferView::available() const
172 return pimpl_->available();
176 Change const BufferView::getCurrentChange()
178 return pimpl_->getCurrentChange();
182 void BufferView::beforeChange(LyXText * text)
184 pimpl_->beforeChange(text);
188 void BufferView::savePosition(unsigned int i)
190 pimpl_->savePosition(i);
194 void BufferView::restorePosition(unsigned int i)
196 pimpl_->restorePosition(i);
200 bool BufferView::isSavedPosition(unsigned int i)
202 return pimpl_->isSavedPosition(i);
206 void BufferView::update(LyXText * text, UpdateCodes f)
208 pimpl_->update(text, f);
212 void BufferView::update(UpdateCodes f)
218 void BufferView::switchKeyMap()
220 pimpl_->switchKeyMap();
224 void BufferView::insetUnlock()
226 pimpl_->insetUnlock();
230 int BufferView::workWidth() const
232 return pimpl_->workarea().workWidth();
236 void BufferView::toggleSelection(bool b)
238 pimpl_->toggleSelection(b);
242 void BufferView::toggleToggle()
244 pimpl_->toggleToggle();
248 void BufferView::center()
254 string const BufferView::getClipboard() const
256 return pimpl_->workarea().getClipboard();
260 void BufferView::stuffClipboard(string const & stuff) const
262 pimpl_->stuffClipboard(stuff);
266 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
267 BufferView::UpdateCodes uc2)
269 return static_cast<BufferView::UpdateCodes>
270 (static_cast<int>(uc1) | static_cast<int>(uc2));
274 bool BufferView::dispatch(FuncRequest const & ev)
276 return pimpl_->dispatch(ev);
280 void BufferView::scroll(int lines)
282 pimpl_->scroll(lines);
286 // Inserts a file into current document
287 bool BufferView::insertLyXFile(string const & filen)
289 // Copyright CHT Software Service GmbH
292 // Insert a LyXformat - file into current buffer
294 // Moved from lyx_cb.C (Lgb)
299 string const fname = MakeAbsPath(filen);
301 // check if file exist
302 FileInfo const fi(fname);
304 if (!fi.readable()) {
305 string const file = MakeDisplayPath(fname, 50);
307 bformat(_("The specified document\n%1$s\ncould not be read."), file);
308 Alert::error(_("Could not read document"), text);
314 ifstream ifs(fname.c_str());
316 string const file = MakeDisplayPath(fname, 50);
318 bformat(_("Could not open the specified document %1$s\n"), file);
319 Alert::error(_("Could not open file"), text);
323 int const c = ifs.peek();
330 text->breakParagraph(buffer()->paragraphs);
333 // FIXME: huh ? No we won't !
334 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
335 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
337 lyxerr[Debug::INFO] << "Will insert file without header"
339 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
347 void BufferView::showErrorList(string const & action) const
349 if (getErrorList().size()) {
350 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
351 owner()->getDialogs().show("errorlist", title);
352 pimpl_->errorlist_.clear();
358 BufferView::getErrorList() const
360 return pimpl_->errorlist_;
364 void BufferView::setCursorFromRow(int row)
369 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
371 ParagraphList::iterator texrowpar;
374 texrowpar = text->ownerParagraphs().begin();
377 texrowpar = buffer()->getParFromID(tmpid).pit();
379 text->setCursor(texrowpar, tmppos);
383 bool BufferView::insertInset(InsetOld * inset, string const & lout)
385 return pimpl_->insertInset(inset, lout);
389 void BufferView::gotoLabel(string const & label)
391 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
392 it != buffer()->inset_iterator_end(); ++it) {
393 vector<string> labels;
394 it->getLabelList(labels);
395 if (find(labels.begin(),labels.end(),label)
398 text->setCursor(it.getPar(), it.getPos());
399 text->selection.cursor = text->cursor;
400 update(text, BufferView::SELECT);
407 void BufferView::undo()
412 owner()->message(_("Undo"));
414 update(text, BufferView::SELECT);
416 owner()->message(_("No further undo information"));
418 update(text, BufferView::SELECT);
423 void BufferView::redo()
428 owner()->message(_("Redo"));
430 update(text, BufferView::SELECT);
432 owner()->message(_("No further redo information"));
434 update(text, BufferView::SELECT);
439 // these functions are for the spellchecker
440 WordLangTuple const BufferView::nextWord(float & value)
444 return WordLangTuple();
447 return text->selectNextWordToSpellcheck(value);
451 void BufferView::selectLastWord()
456 LyXCursor cur = text->selection.cursor;
458 text->selection.cursor = cur;
459 text->selectSelectedWord();
460 toggleSelection(false);
461 update(text, BufferView::SELECT);
465 void BufferView::endOfSpellCheck()
467 if (!available()) return;
470 text->selectSelectedWord();
471 text->clearSelection();
472 update(text, BufferView::SELECT);
476 void BufferView::replaceWord(string const & replacestring)
481 LyXText * tt = getLyXText();
482 update(tt, BufferView::SELECT);
484 // clear the selection (if there is any)
485 toggleSelection(false);
486 update(tt, BufferView::SELECT);
488 // clear the selection (if there is any)
489 toggleSelection(false);
490 tt->replaceSelectionWithString(replacestring);
492 tt->setSelectionRange(replacestring.length());
494 // Go back so that replacement string is also spellchecked
495 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
496 tt->cursorLeft(this);
498 update(tt, BufferView::SELECT);
500 // FIXME: should be done through LFUN
501 buffer()->markDirty();
504 // End of spellchecker stuff
507 bool BufferView::lockInset(UpdatableInset * inset)
511 // don't relock if we're already locked
512 if (theLockingInset() == inset)
514 if (!theLockingInset()) {
515 // first check if it's the inset under the cursor we want lock
516 // should be most of the time
517 if (text->cursor.pos() < text->cursor.par()->size()
518 && text->cursor.par()->getChar(text->cursor.pos()) ==
519 Paragraph::META_INSET) {
520 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
522 theLockingInset(inset);
526 // Then do a deep look of the inset and lock the right one
527 int const id = inset->id();
528 ParagraphList::iterator pit = buffer()->paragraphs.begin();
529 ParagraphList::iterator pend = buffer()->paragraphs.end();
530 for (; pit != pend; ++pit) {
531 InsetList::iterator it = pit->insetlist.begin();
532 InsetList::iterator end = pit->insetlist.end();
533 for (; it != end; ++it) {
534 if (it->inset == inset) {
535 text->setCursorIntern(pit, it->pos);
536 theLockingInset(inset);
539 if (it->inset->getInsetFromID(id)) {
540 text->setCursorIntern(pit, it->pos);
541 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
542 it->inset->localDispatch(cmd);
543 return theLockingInset()->lockInsetInInset(this, inset);
549 return theLockingInset()->lockInsetInInset(this, inset);
553 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
555 if (theLockingInset() && available()) {
556 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
557 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
566 void BufferView::hideCursor()
568 screen().hideCursor();
572 int BufferView::unlockInset(UpdatableInset * inset)
576 if (inset && theLockingInset() == inset) {
577 inset->insetUnlock(this);
579 // make sure we update the combo !
580 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
581 // Tell the paragraph dialog that we changed paragraph
582 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
585 } else if (inset && theLockingInset() &&
586 theLockingInset()->unlockInsetInInset(this, inset)) {
587 // Tell the paragraph dialog that we changed paragraph
588 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
589 // owner inset has updated the layout combo
597 void BufferView::updateInset(InsetOld * inset)
599 pimpl_->updateInset(inset);
603 bool BufferView::ChangeInsets(InsetOld::Code code,
604 string const & from, string const & to)
606 bool need_update = false;
607 LyXCursor cursor = text->cursor;
608 LyXCursor tmpcursor = cursor;
609 cursor.par(tmpcursor.par());
610 cursor.pos(tmpcursor.pos());
612 ParIterator end = buffer()->par_iterator_end();
613 for (ParIterator it = buffer()->par_iterator_begin();
615 bool changed_inset = false;
616 for (InsetList::iterator it2 = it->insetlist.begin();
617 it2 != it->insetlist.end(); ++it2) {
618 if (it2->inset->lyxCode() == code) {
619 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
620 if (inset->getContents() == from) {
621 inset->setContents(to);
622 changed_inset = true;
631 // The test it.size()==1 was needed to prevent crashes.
632 // How to set the cursor corretly when it.size()>1 ??
633 if (it.size() == 1) {
634 text->setCursorIntern(it.pit(), 0);
635 text->redoParagraphs(text->cursor,
636 boost::next(text->cursor.par()));
637 text->partialRebreak();
641 text->setCursorIntern(cursor.par(), cursor.pos());
646 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
648 // Check if the label 'from' appears more than once
649 vector<string> labels;
650 buffer()->getLabelList(labels);
652 if (lyx::count(labels.begin(), labels.end(), from) > 1)
655 return ChangeInsets(InsetOld::REF_CODE, from, to);
659 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
661 typedef pair<string, string> StringPair;
663 vector<StringPair> keys;
664 buffer()->fillWithBibKeys(keys);
665 if (count_if(keys.begin(), keys.end(),
666 lyx::equal_1st_in_pair<StringPair>(from))
670 return ChangeInsets(InsetOld::CITE_CODE, from, to);
674 UpdatableInset * BufferView::theLockingInset() const
676 // If NULL is not allowed we should put an Assert here. (Lgb)
678 return text->the_locking_inset;
683 void BufferView::theLockingInset(UpdatableInset * inset)
685 text->the_locking_inset = inset;
689 LyXText * BufferView::getLyXText() const
691 if (theLockingInset()) {
692 LyXText * txt = theLockingInset()->getLyXText(this, true);
700 LyXText * BufferView::getParentText(InsetOld * inset) const
702 if (inset->owner()) {
703 LyXText * txt = inset->getLyXText(this);
704 inset = inset->owner();
705 while (inset && inset->getLyXText(this) == txt)
706 inset = inset->owner();
708 return inset->getLyXText(this);
714 Language const * BufferView::getParentLanguage(InsetOld * inset) const
716 LyXText * text = getParentText(inset);
717 return text->cursor.par()->getFontSettings(buffer()->params,
718 text->cursor.pos()).language();
722 Encoding const * BufferView::getEncoding() const
724 LyXText * t = getLyXText();
728 LyXCursor const & c = t->cursor;
729 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
730 outerFont(c.par(), t->ownerParagraphs()));
731 return font.language()->encoding();
735 void BufferView::haveSelection(bool sel)
737 pimpl_->workarea().haveSelection(sel);
741 int BufferView::workHeight() const
743 return pimpl_->workarea().workHeight();