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(0, 0);
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();
317 distance(text()->paragraphs().begin(), it.getPar()),
319 cursor().resetAnchor();
327 void BufferView::hideCursor()
329 screen().hideCursor();
333 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
335 // Check if the label 'from' appears more than once
336 vector<string> labels;
337 buffer()->getLabelList(labels);
339 if (lyx::count(labels.begin(), labels.end(), from) > 1)
342 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
346 LyXText * BufferView::getLyXText() const
348 return cursor().innerText();
352 Language const * BufferView::getParentLanguage(InsetOld * inset) const
354 Paragraph const & par = ownerPar(*buffer(), inset);
355 return par.getFontSettings(buffer()->params(),
356 par.getPositionOfInset(inset)).language();
360 Encoding const * BufferView::getEncoding() const
362 LyXText * t = getLyXText();
365 CursorSlice const & cur = cursor().innerTextSlice();
366 return t->getPar(cur.par())->getFont(
367 buffer()->params(), cur.pos(),
368 outerFont(t->getPar(cur.par()), t->paragraphs())
369 ).language()->encoding();
373 void BufferView::haveSelection(bool sel)
375 pimpl_->workarea().haveSelection(sel);
379 int BufferView::workHeight() const
381 return pimpl_->workarea().workHeight();
385 LyXText * BufferView::text() const
387 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
391 void BufferView::setCursor(ParIterator const & par,
394 LCursor & cur = cursor();
396 ParIterator::PosHolder const & positions = par.positions();
397 int const last = par.size() - 1;
398 for (int i = 0; i < last; ++i)
399 (*positions[i].it)->inset->edit(cur, true);
401 LyXText * lt = par.text(*buffer());
402 lt->setCursor(lt->parOffset(par.pit()), pos);
407 if the fitCursor call refers to some point in never-explored-land, then we
408 don't have y information in insets there, then we cannot even do an update
409 to get it (because we need the y infomation for setting top_y first). So
410 this is solved in putSelectionAt with:
412 - setting top_y to the y of the outerPar (that has good info)
414 - calling cursor().updatePos()
415 - then call fitCursor()
420 void BufferView::putSelectionAt(PosIterator const & cur,
421 int length, bool backwards)
423 ParIterator par(cur);
425 cursor().clearSelection();
427 LyXText * text = par.text(*buffer());
428 setCursor(par, cur.pos());
430 // hack for the chicken and egg problem
432 top_y(par.outerPar()->y);
434 text->setCursor(text->parOffset(cur.pit()), cur.pos());
435 cursor().updatePos();
438 text->setSelectionRange(cursor(), length);
439 cursor().setSelection();
441 swap(cursor().cursor_, cursor().anchor_);
449 LCursor & BufferView::cursor()
451 return pimpl_->cursor_;
455 LCursor const & BufferView::cursor() const
457 return pimpl_->cursor_;