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::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();
324 text->breakParagraph(buffer()->paragraphs);
327 // FIXME: huh ? No we won't !
328 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
329 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
331 lyxerr[Debug::INFO] << "Will insert file without header"
333 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
341 void BufferView::showErrorList(string const & action) const
343 if (getErrorList().size()) {
344 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
345 owner()->getDialogs().show("errorlist", title);
346 pimpl_->errorlist_.clear();
352 BufferView::getErrorList() const
354 return pimpl_->errorlist_;
358 void BufferView::setCursorFromRow(int row)
363 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
365 ParagraphList::iterator texrowpar;
368 texrowpar = text->ownerParagraphs().begin();
371 texrowpar = buffer()->getParFromID(tmpid).pit();
373 text->setCursor(texrowpar, tmppos);
377 bool BufferView::insertInset(InsetOld * inset, string const & lout)
379 return pimpl_->insertInset(inset, lout);
383 void BufferView::gotoLabel(string const & label)
385 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
386 it != buffer()->inset_iterator_end(); ++it) {
387 vector<string> labels;
388 it->getLabelList(labels);
389 if (find(labels.begin(),labels.end(),label)
392 text->setCursor(it.getPar(), it.getPos());
393 text->selection.cursor = text->cursor;
394 update(text, BufferView::SELECT);
401 void BufferView::undo()
406 owner()->message(_("Undo"));
408 update(text, BufferView::SELECT);
410 owner()->message(_("No further undo information"));
412 update(text, BufferView::SELECT);
417 void BufferView::redo()
422 owner()->message(_("Redo"));
424 update(text, BufferView::SELECT);
426 owner()->message(_("No further redo information"));
428 update(text, BufferView::SELECT);
433 // these functions are for the spellchecker
434 WordLangTuple const BufferView::nextWord(float & value)
438 return WordLangTuple();
441 return text->selectNextWordToSpellcheck(value);
445 void BufferView::selectLastWord()
450 LyXCursor cur = text->selection.cursor;
452 text->selection.cursor = cur;
453 text->selectSelectedWord();
454 toggleSelection(false);
455 update(text, BufferView::SELECT);
459 void BufferView::endOfSpellCheck()
461 if (!available()) return;
464 text->selectSelectedWord();
465 text->clearSelection();
466 update(text, BufferView::SELECT);
470 void BufferView::replaceWord(string const & replacestring)
475 LyXText * tt = getLyXText();
476 update(tt, BufferView::SELECT);
478 // clear the selection (if there is any)
479 toggleSelection(false);
480 update(tt, BufferView::SELECT);
482 // clear the selection (if there is any)
483 toggleSelection(false);
484 tt->replaceSelectionWithString(replacestring);
486 tt->setSelectionRange(replacestring.length());
488 // Go back so that replacement string is also spellchecked
489 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
490 tt->cursorLeft(this);
492 update(tt, BufferView::SELECT);
494 // FIXME: should be done through LFUN
495 buffer()->markDirty();
498 // End of spellchecker stuff
501 bool BufferView::lockInset(UpdatableInset * inset)
505 // don't relock if we're already locked
506 if (theLockingInset() == inset)
508 if (!theLockingInset()) {
509 // first check if it's the inset under the cursor we want lock
510 // should be most of the time
511 if (text->cursor.pos() < text->cursor.par()->size()
512 && text->cursor.par()->getChar(text->cursor.pos()) ==
513 Paragraph::META_INSET) {
514 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
516 theLockingInset(inset);
520 // Then do a deep look of the inset and lock the right one
521 int const id = inset->id();
522 ParagraphList::iterator pit = buffer()->paragraphs.begin();
523 ParagraphList::iterator pend = buffer()->paragraphs.end();
524 for (; pit != pend; ++pit) {
525 InsetList::iterator it = pit->insetlist.begin();
526 InsetList::iterator end = pit->insetlist.end();
527 for (; it != end; ++it) {
528 if (it->inset == inset) {
529 text->setCursorIntern(pit, it->pos);
530 theLockingInset(inset);
533 if (it->inset->getInsetFromID(id)) {
534 text->setCursorIntern(pit, it->pos);
535 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
536 it->inset->localDispatch(cmd);
537 return theLockingInset()->lockInsetInInset(this, inset);
543 return theLockingInset()->lockInsetInInset(this, inset);
547 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
549 if (theLockingInset() && available()) {
550 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
551 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
560 void BufferView::hideCursor()
562 screen().hideCursor();
566 int BufferView::unlockInset(UpdatableInset * inset)
570 if (inset && theLockingInset() == inset) {
571 inset->insetUnlock(this);
573 // make sure we update the combo !
574 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
575 // Tell the paragraph dialog that we changed paragraph
576 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
579 } else if (inset && theLockingInset() &&
580 theLockingInset()->unlockInsetInInset(this, inset)) {
581 // Tell the paragraph dialog that we changed paragraph
582 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
583 // owner inset has updated the layout combo
591 void BufferView::updateInset(InsetOld * inset)
593 pimpl_->updateInset(inset);
597 bool BufferView::ChangeInsets(InsetOld::Code code,
598 string const & from, string const & to)
600 bool need_update = false;
601 LyXCursor cursor = text->cursor;
602 LyXCursor tmpcursor = cursor;
603 cursor.par(tmpcursor.par());
604 cursor.pos(tmpcursor.pos());
606 ParIterator end = buffer()->par_iterator_end();
607 for (ParIterator it = buffer()->par_iterator_begin();
609 bool changed_inset = false;
610 for (InsetList::iterator it2 = it->insetlist.begin();
611 it2 != it->insetlist.end(); ++it2) {
612 if (it2->inset->lyxCode() == code) {
613 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
614 if (inset->getContents() == from) {
615 inset->setContents(to);
616 changed_inset = true;
625 // The test it.size()==1 was needed to prevent crashes.
626 // How to set the cursor corretly when it.size()>1 ??
627 if (it.size() == 1) {
628 text->setCursorIntern(it.pit(), 0);
629 text->redoParagraphs(text->cursor,
630 boost::next(text->cursor.par()));
631 text->partialRebreak();
635 text->setCursorIntern(cursor.par(), cursor.pos());
640 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
642 // Check if the label 'from' appears more than once
643 vector<string> labels;
644 buffer()->getLabelList(labels);
646 if (lyx::count(labels.begin(), labels.end(), from) > 1)
649 return ChangeInsets(InsetOld::REF_CODE, from, to);
653 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
655 typedef pair<string, string> StringPair;
657 vector<StringPair> keys;
658 buffer()->fillWithBibKeys(keys);
659 if (count_if(keys.begin(), keys.end(),
660 lyx::equal_1st_in_pair<StringPair>(from))
664 return ChangeInsets(InsetOld::CITE_CODE, from, to);
668 UpdatableInset * BufferView::theLockingInset() const
670 // If NULL is not allowed we should put an Assert here. (Lgb)
672 return text->the_locking_inset;
677 void BufferView::theLockingInset(UpdatableInset * inset)
679 text->the_locking_inset = inset;
683 LyXText * BufferView::getLyXText() const
685 if (theLockingInset()) {
686 LyXText * txt = theLockingInset()->getLyXText(this, true);
694 LyXText * BufferView::getParentText(InsetOld * inset) const
696 if (inset->owner()) {
697 LyXText * txt = inset->getLyXText(this);
698 inset = inset->owner();
699 while (inset && inset->getLyXText(this) == txt)
700 inset = inset->owner();
702 return inset->getLyXText(this);
708 Language const * BufferView::getParentLanguage(InsetOld * inset) const
710 LyXText * text = getParentText(inset);
711 return text->cursor.par()->getFontSettings(buffer()->params,
712 text->cursor.pos()).language();
716 Encoding const * BufferView::getEncoding() const
718 LyXText * t = getLyXText();
722 LyXCursor const & c = t->cursor;
723 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
724 outerFont(c.par(), t->ownerParagraphs()));
725 return font.language()->encoding();
729 void BufferView::haveSelection(bool sel)
731 pimpl_->workarea().haveSelection(sel);
735 int BufferView::workHeight() const
737 return pimpl_->workarea().workHeight();