3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "BufferView.h"
20 #include "bufferlist.h"
21 #include "BufferView_pimpl.h"
22 #include "funcrequest.h"
24 #include "iterators.h"
26 #include "lyxlayout.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
32 #include "WordLangTuple.h"
34 #include "frontends/Alert.h"
35 #include "frontends/Dialogs.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/screen.h"
38 #include "frontends/WorkArea.h"
40 #include "insets/insetcommand.h" // ChangeRefs
41 #include "insets/updatableinset.h"
43 #include "support/filetools.h"
44 #include "support/lyxalgo.h" // lyx_count
46 using lyx::support::bformat;
47 using lyx::support::MakeAbsPath;
54 extern BufferList bufferlist;
57 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
58 int width, int height)
59 : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height)),
66 BufferView::~BufferView()
73 Buffer * BufferView::buffer() const
75 return pimpl_->buffer_;
79 LyXScreen & BufferView::screen() const
81 return pimpl_->screen();
85 LyXView * BufferView::owner() const
87 return pimpl_->owner_;
91 Painter & BufferView::painter() const
93 return pimpl_->painter();
97 void BufferView::buffer(Buffer * b)
103 bool BufferView::newFile(string const & fn, string const & tn, bool named)
105 return pimpl_->newFile(fn, tn, named);
109 bool BufferView::loadLyXFile(string const & fn, bool tl)
111 return pimpl_->loadLyXFile(fn, tl);
115 void BufferView::reload()
117 string const fn = buffer()->fileName();
118 if (bufferlist.close(buffer(), false))
123 void BufferView::resize()
126 pimpl_->resizeCurrentBuffer();
130 bool BufferView::fitCursor()
132 return pimpl_->fitCursor();
136 void BufferView::update()
142 void BufferView::updateScrollbar()
144 pimpl_->updateScrollbar();
148 void BufferView::scrollDocView(int value)
150 pimpl_->scrollDocView(value);
154 void BufferView::redoCurrentBuffer()
156 pimpl_->redoCurrentBuffer();
160 bool BufferView::available() const
162 return pimpl_->available();
166 Change const BufferView::getCurrentChange()
168 return pimpl_->getCurrentChange();
172 void BufferView::beforeChange(LyXText * text)
174 pimpl_->beforeChange(text);
178 void BufferView::savePosition(unsigned int i)
180 pimpl_->savePosition(i);
184 void BufferView::restorePosition(unsigned int i)
186 pimpl_->restorePosition(i);
190 bool BufferView::isSavedPosition(unsigned int i)
192 return pimpl_->isSavedPosition(i);
196 void BufferView::switchKeyMap()
198 pimpl_->switchKeyMap();
202 void BufferView::insetUnlock()
204 pimpl_->insetUnlock();
208 int BufferView::workWidth() const
210 return pimpl_->workarea().workWidth();
214 void BufferView::center()
220 int BufferView::top_y() const
222 return pimpl_->top_y();
226 void BufferView::top_y(int y)
232 string const BufferView::getClipboard() const
234 return pimpl_->workarea().getClipboard();
238 void BufferView::stuffClipboard(string const & stuff) const
240 pimpl_->stuffClipboard(stuff);
244 bool BufferView::dispatch(FuncRequest const & ev)
246 return pimpl_->dispatch(ev);
250 void BufferView::scroll(int lines)
252 pimpl_->scroll(lines);
256 // Inserts a file into current document
257 bool BufferView::insertLyXFile(string const & filen)
259 // Copyright CHT Software Service GmbH
262 // Insert a LyXformat - file into current buffer
264 // Moved from lyx_cb.C (Lgb)
266 BOOST_ASSERT(!filen.empty());
268 string const fname = MakeAbsPath(filen);
272 text->breakParagraph(buffer()->paragraphs());
274 bool res = buffer()->readFile(fname, text->cursorPar());
281 void BufferView::showErrorList(string const & action) const
283 if (getErrorList().size()) {
284 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
285 action, buffer()->fileName());
286 owner()->getDialogs().show("errorlist", title);
287 pimpl_->errorlist_.clear();
292 ErrorList const & BufferView::getErrorList() const
294 return pimpl_->errorlist_;
298 void BufferView::setCursorFromRow(int row)
303 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
306 text->setCursor(0, 0);
308 text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
312 bool BufferView::insertInset(InsetOld * inset, string const & lout)
314 return pimpl_->insertInset(inset, lout);
318 void BufferView::gotoLabel(string const & label)
320 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
321 it != buffer()->inset_iterator_end(); ++it) {
322 vector<string> labels;
323 it->getLabelList(*buffer(), labels);
324 if (find(labels.begin(),labels.end(),label) != labels.end()) {
327 std::distance(text->ownerParagraphs().begin(), it.getPar()),
329 text->selection.cursor = text->cursor;
337 void BufferView::undo()
342 owner()->message(_("Undo"));
345 owner()->message(_("No further undo information"));
351 void BufferView::redo()
356 owner()->message(_("Redo"));
359 owner()->message(_("No further redo information"));
365 // these functions are for the spellchecker
366 WordLangTuple const BufferView::nextWord(float & value)
370 return WordLangTuple();
373 return text->selectNextWordToSpellcheck(value);
377 void BufferView::selectLastWord()
382 LyXCursor cur = text->selection.cursor;
384 text->selection.cursor = cur;
385 text->selectSelectedWord();
390 void BufferView::endOfSpellCheck()
392 if (!available()) return;
395 text->selectSelectedWord();
396 text->clearSelection();
401 void BufferView::replaceWord(string const & replacestring)
406 LyXText * text = getLyXText();
408 text->replaceSelectionWithString(replacestring);
409 text->setSelectionRange(replacestring.length());
411 // Go back so that replacement string is also spellchecked
412 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
413 text->cursorLeft(this);
415 // FIXME: should be done through LFUN
416 buffer()->markDirty();
421 bool BufferView::lockInset(UpdatableInset * inset)
426 // don't relock if we're already locked
427 if (theLockingInset() == inset)
430 if (theLockingInset())
431 return theLockingInset()->lockInsetInInset(this, inset);
433 // first check if it's the inset under the cursor we want lock
434 // should be most of the time
435 if (text->cursor.pos() < text->cursorPar()->size()
436 && text->cursorPar()->getChar(text->cursor.pos()) ==
437 Paragraph::META_INSET) {
438 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
439 theLockingInset(inset);
444 // then do a deep look at the inset and lock the right one
445 ParagraphList::iterator pit = buffer()->paragraphs().begin();
446 ParagraphList::iterator pend = buffer()->paragraphs().end();
447 for (int par = 0; pit != pend; ++pit, ++par) {
448 InsetList::iterator it = pit->insetlist.begin();
449 InsetList::iterator end = pit->insetlist.end();
450 for (; it != end; ++it) {
451 if (it->inset == inset) {
452 text->setCursorIntern(par, it->pos);
453 theLockingInset(inset);
462 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
464 if (theLockingInset() && available()) {
465 y += text->cursor.y() + theLockingInset()->insetInInsetY();
466 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
475 void BufferView::hideCursor()
477 screen().hideCursor();
481 int BufferView::unlockInset(UpdatableInset * inset)
485 if (inset && theLockingInset() == inset) {
486 inset->insetUnlock(this);
488 // make sure we update the combo !
489 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
490 // Tell the paragraph dialog that we changed paragraph
491 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
495 if (inset && theLockingInset() &&
496 theLockingInset()->unlockInsetInInset(this, inset)) {
497 // Tell the paragraph dialog that we changed paragraph
498 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
499 // owner inset has updated the layout combo
507 void BufferView::updateInset(InsetOld const * inset)
509 pimpl_->updateInset(inset);
513 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
515 // Check if the label 'from' appears more than once
516 vector<string> labels;
517 buffer()->getLabelList(labels);
519 if (lyx::count(labels.begin(), labels.end(), from) > 1)
522 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
526 UpdatableInset * BufferView::theLockingInset() const
528 // If NULL is not allowed we should put an Assert here. (Lgb)
530 return text->the_locking_inset;
535 void BufferView::theLockingInset(UpdatableInset * inset)
537 text->the_locking_inset = inset;
541 LyXText * BufferView::getLyXText() const
543 if (theLockingInset()) {
544 LyXText * txt = theLockingInset()->getLyXText(this, true);
552 Language const * BufferView::getParentLanguage(InsetOld * inset) const
554 Paragraph const & par = ownerPar(*buffer(), inset);
555 return par.getFontSettings(buffer()->params(),
556 par.getPositionOfInset(inset)).language();
560 Encoding const * BufferView::getEncoding() const
562 LyXText * text = getLyXText();
566 return text->cursorPar()->getFont(
569 outerFont(text->cursorPar(), text->ownerParagraphs())
570 ).language()->encoding();
574 void BufferView::haveSelection(bool sel)
576 pimpl_->workarea().haveSelection(sel);
580 int BufferView::workHeight() const
582 return pimpl_->workarea().workHeight();
586 void BufferView::x_target(int x)
592 int BufferView::x_target() const