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 "bufferparams.h"
22 #include "BufferView_pimpl.h"
24 #include "funcrequest.h"
26 #include "iterators.h"
28 #include "lyxlayout.h"
30 #include "lyxtextclass.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "PosIterator.h"
36 #include "WordLangTuple.h"
38 #include "frontends/Alert.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/screen.h"
42 #include "frontends/WorkArea.h"
44 #include "insets/insetcommand.h" // ChangeRefs
45 #include "insets/updatableinset.h"
47 #include "support/filetools.h"
48 #include "support/lyxalgo.h" // lyx_count
50 using lyx::support::bformat;
51 using lyx::support::MakeAbsPath;
60 extern BufferList bufferlist;
63 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
64 int width, int height)
65 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
69 BufferView::~BufferView()
75 void BufferView::unsetXSel()
77 pimpl_->xsel_cache_.set = false;
81 Buffer * BufferView::buffer() const
83 return pimpl_->buffer_;
87 LyXScreen & BufferView::screen() const
89 return pimpl_->screen();
93 LyXView * BufferView::owner() const
95 return pimpl_->owner_;
99 Painter & BufferView::painter() const
101 return pimpl_->painter();
105 void BufferView::buffer(Buffer * b)
111 bool BufferView::newFile(string const & fn, string const & tn, bool named)
113 return pimpl_->newFile(fn, tn, named);
117 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 return pimpl_->loadLyXFile(fn, tl);
123 void BufferView::reload()
125 string const fn = buffer()->fileName();
126 if (bufferlist.close(buffer(), false))
131 void BufferView::resize()
134 pimpl_->resizeCurrentBuffer();
138 bool BufferView::fitCursor()
140 return pimpl_->fitCursor();
144 void BufferView::update()
150 void BufferView::updateScrollbar()
152 pimpl_->updateScrollbar();
156 void BufferView::scrollDocView(int value)
158 pimpl_->scrollDocView(value);
162 void BufferView::redoCurrentBuffer()
164 pimpl_->redoCurrentBuffer();
168 bool BufferView::available() const
170 return pimpl_->available();
174 Change const BufferView::getCurrentChange()
176 return pimpl_->getCurrentChange();
180 void BufferView::savePosition(unsigned int i)
182 pimpl_->savePosition(i);
186 void BufferView::restorePosition(unsigned int i)
188 pimpl_->restorePosition(i);
192 bool BufferView::isSavedPosition(unsigned int i)
194 return pimpl_->isSavedPosition(i);
198 void BufferView::switchKeyMap()
200 pimpl_->switchKeyMap();
204 int BufferView::workWidth() const
206 return pimpl_->workarea().workWidth();
210 void BufferView::center()
216 int BufferView::top_y() const
218 return pimpl_->top_y();
222 void BufferView::top_y(int y)
228 string const BufferView::getClipboard() const
230 return pimpl_->workarea().getClipboard();
234 void BufferView::stuffClipboard(string const & stuff) const
236 pimpl_->stuffClipboard(stuff);
240 bool BufferView::dispatch(FuncRequest const & ev)
242 return pimpl_->dispatch(ev);
246 void BufferView::scroll(int lines)
248 pimpl_->scroll(lines);
252 // Inserts a file into current document
253 bool BufferView::insertLyXFile(string const & filen)
255 // Copyright CHT Software Service GmbH
258 // Insert a LyXformat - file into current buffer
260 // Moved from lyx_cb.C (Lgb)
262 BOOST_ASSERT(!filen.empty());
264 string const fname = MakeAbsPath(filen);
266 cursor().clearSelection();
267 text()->breakParagraph(buffer()->paragraphs());
269 bool res = buffer()->readFile(fname, text()->cursorPar());
275 void BufferView::showErrorList(string const & action) const
277 if (getErrorList().size()) {
278 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
279 action, buffer()->fileName());
280 owner()->getDialogs().show("errorlist", title);
281 pimpl_->errorlist_.clear();
286 ErrorList const & BufferView::getErrorList() const
288 return pimpl_->errorlist_;
292 void BufferView::setCursorFromRow(int row)
297 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
300 text()->setCursor(0, 0);
302 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
306 void BufferView::insertInset(InsetBase * inset, string const & lout)
308 // not quite sure if we want this...
309 text()->recUndo(text()->cursor().par());
312 cursor().clearSelection();
314 text()->breakParagraph(buffer()->paragraphs());
316 if (!text()->cursorPar()->empty()) {
317 text()->cursorLeft(true);
318 text()->breakParagraph(buffer()->paragraphs());
322 LyXTextClass const & tclass = buffer()->params().getLyXTextClass();
323 bool hasLayout = tclass.hasLayout(lres);
325 text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
326 text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
328 cursor().innerText()->insertInset(inset);
333 void BufferView::gotoLabel(string const & label)
335 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
336 it != buffer()->inset_iterator_end(); ++it) {
337 vector<string> labels;
338 it->getLabelList(*buffer(), labels);
339 if (find(labels.begin(),labels.end(),label) != labels.end()) {
340 cursor().clearSelection();
342 distance(text()->paragraphs().begin(), it.getPar()),
344 cursor().resetAnchor();
352 void BufferView::undo()
357 owner()->message(_("Undo"));
358 cursor().clearSelection();
359 if (!textUndo(*this))
360 owner()->message(_("No further undo information"));
366 void BufferView::redo()
371 owner()->message(_("Redo"));
372 cursor().clearSelection();
373 if (!textRedo(*this))
374 owner()->message(_("No further redo information"));
380 void BufferView::replaceWord(string const & replacestring)
385 LyXText * t = getLyXText();
387 t->replaceSelectionWithString(replacestring);
388 t->setSelectionRange(replacestring.length());
390 // Go back so that replacement string is also spellchecked
391 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
394 // FIXME: should be done through LFUN
395 buffer()->markDirty();
400 void BufferView::hideCursor()
402 screen().hideCursor();
406 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
408 // Check if the label 'from' appears more than once
409 vector<string> labels;
410 buffer()->getLabelList(labels);
412 if (lyx::count(labels.begin(), labels.end(), from) > 1)
415 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
419 LyXText * BufferView::getLyXText() const
421 return cursor().innerText();
425 Language const * BufferView::getParentLanguage(InsetOld * inset) const
427 Paragraph const & par = ownerPar(*buffer(), inset);
428 return par.getFontSettings(buffer()->params(),
429 par.getPositionOfInset(inset)).language();
433 Encoding const * BufferView::getEncoding() const
435 LyXText * t = getLyXText();
438 CursorSlice const & cur = cursor().innerTextSlice();
439 return t->getPar(cur.par())->getFont(
440 buffer()->params(), cur.pos(),
441 outerFont(t->getPar(cur.par()), t->paragraphs())
442 ).language()->encoding();
446 void BufferView::haveSelection(bool sel)
448 pimpl_->workarea().haveSelection(sel);
452 int BufferView::workHeight() const
454 return pimpl_->workarea().workHeight();
458 void BufferView::updateParagraphDialog()
460 pimpl_->updateParagraphDialog();
464 LyXText * BufferView::text() const
466 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
470 void BufferView::setCursor(ParIterator const & par,
473 LCursor & cur = cursor();
475 ParIterator::PosHolder const & positions = par.positions();
476 int const last = par.size() - 1;
477 for (int i = 0; i < last; ++i)
478 (*positions[i].it)->inset->edit(cur, true);
480 LyXText * lt = par.text(*buffer());
481 lt->setCursor(par.pit(), pos);
486 if the fitCursor call refers to some point in never-explored-land, then we
487 don't have y information in insets there, then we cannot even do an update
488 to get it (because we need the y infomation for setting top_y first). So
489 this is solved in putSelectionAt with:
491 - setting top_y to the y of the outerPar (that has good info)
493 - calling cursor().updatePos()
494 - then call fitCursor()
499 void BufferView::putSelectionAt(PosIterator const & cur,
500 int length, bool backwards)
502 ParIterator par(cur);
504 cursor().clearSelection();
506 LyXText * text = par.text(*buffer());
507 setCursor(par, cur.pos());
509 // hack for the chicken and egg problem
511 top_y(par.outerPar()->y);
513 text->setCursor(cur.pit(), cur.pos());
514 cursor().updatePos();
517 text->setSelectionRange(length);
518 cursor().setSelection();
520 swap(cursor().cursor_, cursor().anchor_);
528 LCursor & BufferView::cursor()
530 return pimpl_->cursor_;
534 LCursor const & BufferView::cursor() const
536 return pimpl_->cursor_;