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 void BufferView::replaceWord(string const & replacestring)
370 LyXText * text = getLyXText();
372 text->replaceSelectionWithString(replacestring);
373 text->setSelectionRange(replacestring.length());
375 // Go back so that replacement string is also spellchecked
376 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
377 text->cursorLeft(this);
379 // FIXME: should be done through LFUN
380 buffer()->markDirty();
385 bool BufferView::lockInset(UpdatableInset * inset)
390 // don't relock if we're already locked
391 if (theLockingInset() == inset)
394 if (theLockingInset())
395 return theLockingInset()->lockInsetInInset(this, inset);
397 // first check if it's the inset under the cursor we want lock
398 // should be most of the time
399 if (text->cursor.pos() < text->cursorPar()->size()
400 && text->cursorPar()->getChar(text->cursor.pos()) ==
401 Paragraph::META_INSET) {
402 if (inset == text->cursorPar()->getInset(text->cursor.pos())) {
403 theLockingInset(inset);
408 // then do a deep look at the inset and lock the right one
409 ParagraphList::iterator pit = buffer()->paragraphs().begin();
410 ParagraphList::iterator pend = buffer()->paragraphs().end();
411 for (int par = 0; pit != pend; ++pit, ++par) {
412 InsetList::iterator it = pit->insetlist.begin();
413 InsetList::iterator end = pit->insetlist.end();
414 for (; it != end; ++it) {
415 if (it->inset == inset) {
416 text->setCursorIntern(par, it->pos);
417 theLockingInset(inset);
426 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
428 if (theLockingInset() && available()) {
429 y += text->cursor.y() + theLockingInset()->insetInInsetY();
430 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
439 void BufferView::hideCursor()
441 screen().hideCursor();
445 int BufferView::unlockInset(UpdatableInset * inset)
449 if (inset && theLockingInset() == inset) {
450 inset->insetUnlock(this);
452 // make sure we update the combo !
453 owner()->setLayout(getLyXText()->cursorPar()->layout()->name());
454 // Tell the paragraph dialog that we changed paragraph
455 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
459 if (inset && theLockingInset() &&
460 theLockingInset()->unlockInsetInInset(this, inset)) {
461 // Tell the paragraph dialog that we changed paragraph
462 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
463 // owner inset has updated the layout combo
471 void BufferView::updateInset(InsetOld const * inset)
473 pimpl_->updateInset(inset);
477 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
479 // Check if the label 'from' appears more than once
480 vector<string> labels;
481 buffer()->getLabelList(labels);
483 if (lyx::count(labels.begin(), labels.end(), from) > 1)
486 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
490 UpdatableInset * BufferView::theLockingInset() const
492 // If NULL is not allowed we should put an Assert here. (Lgb)
494 return text->the_locking_inset;
499 void BufferView::theLockingInset(UpdatableInset * inset)
501 text->the_locking_inset = inset;
505 LyXText * BufferView::getLyXText() const
507 if (theLockingInset()) {
508 LyXText * txt = theLockingInset()->getLyXText(this, true);
516 Language const * BufferView::getParentLanguage(InsetOld * inset) const
518 Paragraph const & par = ownerPar(*buffer(), inset);
519 return par.getFontSettings(buffer()->params(),
520 par.getPositionOfInset(inset)).language();
524 Encoding const * BufferView::getEncoding() const
526 LyXText * text = getLyXText();
530 return text->cursorPar()->getFont(
533 outerFont(text->cursorPar(), text->ownerParagraphs())
534 ).language()->encoding();
538 void BufferView::haveSelection(bool sel)
540 pimpl_->workarea().haveSelection(sel);
544 int BufferView::workHeight() const
546 return pimpl_->workarea().workHeight();
550 void BufferView::x_target(int x)
556 int BufferView::x_target() const