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/types.h"
44 #include "support/lyxalgo.h" // lyx_count
48 extern BufferList bufferlist;
51 using namespace lyx::support;
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::newFile(string const & fn, string const & tn, bool named)
108 return pimpl_->newFile(fn, tn, named);
112 bool BufferView::loadLyXFile(string const & fn, bool tl)
114 return pimpl_->loadLyXFile(fn, tl);
118 void BufferView::reload()
120 string const fn = buffer()->fileName();
121 if (bufferlist.close(buffer(), false))
126 void BufferView::resize()
129 pimpl_->resizeCurrentBuffer();
133 void BufferView::repaint()
139 bool BufferView::fitCursor()
141 return pimpl_->fitCursor();
145 void BufferView::update()
151 void BufferView::updateScrollbar()
153 pimpl_->updateScrollbar();
157 void BufferView::scrollDocView(int value)
159 pimpl_->scrollDocView(value);
163 void BufferView::redoCurrentBuffer()
165 pimpl_->redoCurrentBuffer();
169 bool BufferView::available() const
171 return pimpl_->available();
175 Change const BufferView::getCurrentChange()
177 return pimpl_->getCurrentChange();
181 void BufferView::beforeChange(LyXText * text)
183 pimpl_->beforeChange(text);
187 void BufferView::savePosition(unsigned int i)
189 pimpl_->savePosition(i);
193 void BufferView::restorePosition(unsigned int i)
195 pimpl_->restorePosition(i);
199 bool BufferView::isSavedPosition(unsigned int i)
201 return pimpl_->isSavedPosition(i);
205 void BufferView::update(LyXText * text, UpdateCodes f)
207 pimpl_->update(text, f);
211 void BufferView::update(UpdateCodes f)
217 void BufferView::switchKeyMap()
219 pimpl_->switchKeyMap();
223 void BufferView::insetUnlock()
225 pimpl_->insetUnlock();
229 int BufferView::workWidth() const
231 return pimpl_->workarea().workWidth();
235 void BufferView::toggleSelection(bool b)
237 pimpl_->toggleSelection(b);
241 void BufferView::center()
247 string const BufferView::getClipboard() const
249 return pimpl_->workarea().getClipboard();
253 void BufferView::stuffClipboard(string const & stuff) const
255 pimpl_->stuffClipboard(stuff);
259 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
260 BufferView::UpdateCodes uc2)
262 return static_cast<BufferView::UpdateCodes>
263 (static_cast<int>(uc1) | static_cast<int>(uc2));
267 bool BufferView::dispatch(FuncRequest const & ev)
269 return pimpl_->dispatch(ev);
273 void BufferView::scroll(int lines)
275 pimpl_->scroll(lines);
279 // Inserts a file into current document
280 bool BufferView::insertLyXFile(string const & filen)
282 // Copyright CHT Software Service GmbH
285 // Insert a LyXformat - file into current buffer
287 // Moved from lyx_cb.C (Lgb)
292 string const fname = MakeAbsPath(filen);
294 // check if file exist
295 FileInfo const fi(fname);
297 if (!fi.readable()) {
298 string const file = MakeDisplayPath(fname, 50);
300 bformat(_("The specified document\n%1$s\ncould not be read."), file);
301 Alert::error(_("Could not read document"), text);
307 ifstream ifs(fname.c_str());
309 string const file = MakeDisplayPath(fname, 50);
311 bformat(_("Could not open the specified document %1$s\n"), file);
312 Alert::error(_("Could not open file"), text);
316 int const c = ifs.peek();
323 text->breakParagraph(buffer()->paragraphs);
326 // FIXME: huh ? No we won't !
327 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
328 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
330 lyxerr[Debug::INFO] << "Will insert file without header"
332 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
340 void BufferView::showErrorList(string const & action) const
342 if (getErrorList().size()) {
343 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
344 owner()->getDialogs().show("errorlist", title);
345 pimpl_->errorlist_.clear();
351 BufferView::getErrorList() const
353 return pimpl_->errorlist_;
357 void BufferView::setCursorFromRow(int row)
362 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
364 ParagraphList::iterator texrowpar;
367 texrowpar = text->ownerParagraphs().begin();
370 texrowpar = buffer()->getParFromID(tmpid).pit();
372 text->setCursor(texrowpar, tmppos);
376 bool BufferView::insertInset(InsetOld * inset, string const & lout)
378 return pimpl_->insertInset(inset, lout);
382 void BufferView::gotoLabel(string const & label)
384 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
385 it != buffer()->inset_iterator_end(); ++it) {
386 vector<string> labels;
387 it->getLabelList(labels);
388 if (find(labels.begin(),labels.end(),label)
391 text->setCursor(it.getPar(), it.getPos());
392 text->selection.cursor = text->cursor;
393 update(text, BufferView::SELECT);
400 void BufferView::undo()
405 owner()->message(_("Undo"));
407 update(text, BufferView::SELECT);
409 owner()->message(_("No further undo information"));
411 update(text, BufferView::SELECT);
416 void BufferView::redo()
421 owner()->message(_("Redo"));
423 update(text, BufferView::SELECT);
425 owner()->message(_("No further redo information"));
427 update(text, BufferView::SELECT);
432 // these functions are for the spellchecker
433 WordLangTuple const BufferView::nextWord(float & value)
437 return WordLangTuple();
440 return text->selectNextWordToSpellcheck(value);
444 void BufferView::selectLastWord()
449 LyXCursor cur = text->selection.cursor;
451 text->selection.cursor = cur;
452 text->selectSelectedWord();
453 toggleSelection(false);
454 update(text, BufferView::SELECT);
458 void BufferView::endOfSpellCheck()
460 if (!available()) return;
463 text->selectSelectedWord();
464 text->clearSelection();
465 update(text, BufferView::SELECT);
469 void BufferView::replaceWord(string const & replacestring)
474 LyXText * tt = getLyXText();
475 update(tt, BufferView::SELECT);
477 // clear the selection (if there is any)
478 toggleSelection(false);
479 update(tt, BufferView::SELECT);
481 // clear the selection (if there is any)
482 toggleSelection(false);
483 tt->replaceSelectionWithString(replacestring);
485 tt->setSelectionRange(replacestring.length());
487 // Go back so that replacement string is also spellchecked
488 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
489 tt->cursorLeft(this);
491 update(tt, BufferView::SELECT);
493 // FIXME: should be done through LFUN
494 buffer()->markDirty();
497 // End of spellchecker stuff
500 bool BufferView::lockInset(UpdatableInset * inset)
504 // don't relock if we're already locked
505 if (theLockingInset() == inset)
507 if (!theLockingInset()) {
508 // first check if it's the inset under the cursor we want lock
509 // should be most of the time
510 if (text->cursor.pos() < text->cursor.par()->size()
511 && text->cursor.par()->getChar(text->cursor.pos()) ==
512 Paragraph::META_INSET) {
513 InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
515 theLockingInset(inset);
519 // Then do a deep look of the inset and lock the right one
520 int const id = inset->id();
521 ParagraphList::iterator pit = buffer()->paragraphs.begin();
522 ParagraphList::iterator pend = buffer()->paragraphs.end();
523 for (; pit != pend; ++pit) {
524 InsetList::iterator it = pit->insetlist.begin();
525 InsetList::iterator end = pit->insetlist.end();
526 for (; it != end; ++it) {
527 if (it->inset == inset) {
528 text->setCursorIntern(pit, it->pos);
529 theLockingInset(inset);
532 if (it->inset->getInsetFromID(id)) {
533 text->setCursorIntern(pit, it->pos);
534 FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
535 it->inset->localDispatch(cmd);
536 return theLockingInset()->lockInsetInInset(this, inset);
542 return theLockingInset()->lockInsetInInset(this, inset);
546 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
548 if (theLockingInset() && available()) {
549 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
550 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
559 void BufferView::hideCursor()
561 screen().hideCursor();
565 int BufferView::unlockInset(UpdatableInset * inset)
569 if (inset && theLockingInset() == inset) {
570 inset->insetUnlock(this);
572 // make sure we update the combo !
573 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
574 // Tell the paragraph dialog that we changed paragraph
575 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
578 } else if (inset && theLockingInset() &&
579 theLockingInset()->unlockInsetInInset(this, inset)) {
580 // Tell the paragraph dialog that we changed paragraph
581 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
582 // owner inset has updated the layout combo
590 void BufferView::updateInset(InsetOld * inset)
592 pimpl_->updateInset(inset);
596 bool BufferView::ChangeInsets(InsetOld::Code code,
597 string const & from, string const & to)
599 bool need_update = false;
600 LyXCursor cursor = text->cursor;
601 LyXCursor tmpcursor = cursor;
602 cursor.par(tmpcursor.par());
603 cursor.pos(tmpcursor.pos());
605 ParIterator end = buffer()->par_iterator_end();
606 for (ParIterator it = buffer()->par_iterator_begin();
608 bool changed_inset = false;
609 for (InsetList::iterator it2 = it->insetlist.begin();
610 it2 != it->insetlist.end(); ++it2) {
611 if (it2->inset->lyxCode() == code) {
612 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
613 if (inset->getContents() == from) {
614 inset->setContents(to);
615 changed_inset = true;
624 // The test it.size()==1 was needed to prevent crashes.
625 // How to set the cursor corretly when it.size()>1 ??
626 if (it.size() == 1) {
627 text->setCursorIntern(it.pit(), 0);
628 text->redoParagraphs(text->cursor,
629 boost::next(text->cursor.par()));
630 text->partialRebreak();
634 text->setCursorIntern(cursor.par(), cursor.pos());
639 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
641 // Check if the label 'from' appears more than once
642 vector<string> labels;
643 buffer()->getLabelList(labels);
645 if (lyx::count(labels.begin(), labels.end(), from) > 1)
648 return ChangeInsets(InsetOld::REF_CODE, from, to);
652 UpdatableInset * BufferView::theLockingInset() const
654 // If NULL is not allowed we should put an Assert here. (Lgb)
656 return text->the_locking_inset;
661 void BufferView::theLockingInset(UpdatableInset * inset)
663 text->the_locking_inset = inset;
667 LyXText * BufferView::getLyXText() const
669 if (theLockingInset()) {
670 LyXText * txt = theLockingInset()->getLyXText(this, true);
678 LyXText * BufferView::getParentText(InsetOld * inset) const
680 if (inset->owner()) {
681 LyXText * txt = inset->getLyXText(this);
682 inset = inset->owner();
683 while (inset && inset->getLyXText(this) == txt)
684 inset = inset->owner();
686 return inset->getLyXText(this);
692 Language const * BufferView::getParentLanguage(InsetOld * inset) const
694 LyXText * text = getParentText(inset);
695 return text->cursor.par()->getFontSettings(buffer()->params,
696 text->cursor.pos()).language();
700 Encoding const * BufferView::getEncoding() const
702 LyXText * t = getLyXText();
706 LyXCursor const & c = t->cursor;
707 LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
708 outerFont(c.par(), t->ownerParagraphs()));
709 return font.language()->encoding();
713 void BufferView::haveSelection(bool sel)
715 pimpl_->workarea().haveSelection(sel);
719 int BufferView::workHeight() const
721 return pimpl_->workarea().workHeight();