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(cursor());
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(cursor(), 0, 0);
302 text()->setCursor(cursor(),
303 text()->parOffset(buffer()->getParFromID(tmpid).pit()),
308 void BufferView::gotoLabel(string const & label)
310 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
311 it != buffer()->inset_iterator_end(); ++it) {
312 vector<string> labels;
313 it->getLabelList(*buffer(), labels);
314 if (find(labels.begin(),labels.end(),label) != labels.end()) {
315 cursor().clearSelection();
316 text()->setCursor(cursor(),
317 distance(text()->paragraphs().begin(), it.getPar()),
319 cursor().resetAnchor();
327 void BufferView::hideCursor()
329 screen().hideCursor();
333 LyXText * BufferView::getLyXText() const
335 return cursor().innerText();
339 Language const * BufferView::getParentLanguage(InsetOld * inset) const
341 Paragraph const & par = ownerPar(*buffer(), inset);
342 return par.getFontSettings(buffer()->params(),
343 par.getPositionOfInset(inset)).language();
347 Encoding const * BufferView::getEncoding() const
349 LyXText * t = getLyXText();
352 CursorSlice const & cur = cursor().innerTextSlice();
353 return t->getPar(cur.par())->getFont(
354 buffer()->params(), cur.pos(),
355 outerFont(t->getPar(cur.par()), t->paragraphs())
356 ).language()->encoding();
360 void BufferView::haveSelection(bool sel)
362 pimpl_->workarea().haveSelection(sel);
366 int BufferView::workHeight() const
368 return pimpl_->workarea().workHeight();
372 LyXText * BufferView::text() const
374 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
378 void BufferView::setCursor(ParIterator const & par,
381 LCursor & cur = cursor();
383 ParIterator::PosHolder const & positions = par.positions();
384 int const last = par.size() - 1;
385 for (int i = 0; i < last; ++i)
386 (*positions[i].it)->inset->edit(cur, true);
388 LyXText * lt = par.text(*buffer());
389 lt->setCursor(cur, lt->parOffset(par.pit()), pos);
394 if the fitCursor call refers to some point in never-explored-land, then we
395 don't have y information in insets there, then we cannot even do an update
396 to get it (because we need the y infomation for setting top_y first). So
397 this is solved in putSelectionAt with:
399 - setting top_y to the y of the outerPar (that has good info)
401 - calling cursor().updatePos()
402 - then call fitCursor()
407 void BufferView::putSelectionAt(PosIterator const & cur,
408 int length, bool backwards)
410 ParIterator par(cur);
412 cursor().clearSelection();
414 LyXText * text = par.text(*buffer());
415 setCursor(par, cur.pos());
417 // hack for the chicken and egg problem
419 top_y(par.outerPar()->y);
421 text->setCursor(cursor(), text->parOffset(cur.pit()), cur.pos());
422 cursor().updatePos();
425 text->setSelectionRange(cursor(), length);
426 cursor().setSelection();
429 swap(cursor().cursor_, cursor().anchor_);
431 DocumentIterator it = cursor();
432 cursor().setCursor(cursor().anchor_, false);
433 cursor().anchor_ = it;
443 LCursor & BufferView::cursor()
445 return pimpl_->cursor_;
449 LCursor const & BufferView::cursor() const
451 return pimpl_->cursor_;