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"
46 #include "insets/insettext.h"
48 #include "support/filetools.h"
49 #include "support/lyxalgo.h" // lyx_count
51 using lyx::support::bformat;
52 using lyx::support::MakeAbsPath;
61 extern BufferList bufferlist;
64 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
65 int width, int height)
66 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
70 BufferView::~BufferView()
76 void BufferView::unsetXSel()
78 pimpl_->xsel_cache_.set = false;
82 Buffer * BufferView::buffer() const
84 return pimpl_->buffer_;
88 LyXScreen & BufferView::screen() const
90 return pimpl_->screen();
94 LyXView * BufferView::owner() const
96 return pimpl_->owner_;
100 Painter & BufferView::painter() const
102 return pimpl_->painter();
106 void BufferView::setBuffer(Buffer * b)
108 pimpl_->setBuffer(b);
112 void BufferView::newFile(string const & fn, string const & tn, bool named)
114 pimpl_->newFile(fn, tn, named);
118 bool BufferView::loadLyXFile(string const & fn, bool tl)
120 return pimpl_->loadLyXFile(fn, tl);
124 void BufferView::reload()
126 string const fn = buffer()->fileName();
127 if (bufferlist.close(buffer(), false))
132 void BufferView::resize()
135 pimpl_->resizeCurrentBuffer();
139 bool BufferView::fitCursor()
141 return pimpl_->fitCursor();
145 void BufferView::update()
151 void BufferView::updateScrollbar()
153 pimpl_->updateScrollbar();
157 void BufferView::scrollDocView(int value)
159 pimpl_->scrollDocView(value);
163 void BufferView::redoCurrentBuffer()
165 pimpl_->redoCurrentBuffer();
169 bool BufferView::available() const
171 return pimpl_->available();
175 Change const BufferView::getCurrentChange()
177 return pimpl_->getCurrentChange();
181 void BufferView::savePosition(unsigned int i)
183 pimpl_->savePosition(i);
187 void BufferView::restorePosition(unsigned int i)
189 pimpl_->restorePosition(i);
193 bool BufferView::isSavedPosition(unsigned int i)
195 return pimpl_->isSavedPosition(i);
199 void BufferView::switchKeyMap()
201 pimpl_->switchKeyMap();
205 int BufferView::workWidth() const
207 return pimpl_->workarea().workWidth();
211 void BufferView::center()
217 int BufferView::top_y() const
219 return pimpl_->top_y();
223 void BufferView::top_y(int y)
229 string const BufferView::getClipboard() const
231 return pimpl_->workarea().getClipboard();
235 void BufferView::stuffClipboard(string const & stuff) const
237 pimpl_->stuffClipboard(stuff);
241 bool BufferView::dispatch(FuncRequest const & ev)
243 return pimpl_->dispatch(ev);
247 void BufferView::scroll(int lines)
249 pimpl_->scroll(lines);
253 // Inserts a file into current document
254 bool BufferView::insertLyXFile(string const & filen)
256 // Copyright CHT Software Service GmbH
259 // Insert a LyXformat - file into current buffer
261 // Moved from lyx_cb.C (Lgb)
263 BOOST_ASSERT(!filen.empty());
265 string const fname = MakeAbsPath(filen);
267 cursor().clearSelection();
268 text()->breakParagraph(cursor());
270 bool res = buffer()->readFile(fname, text()->cursorPar());
276 void BufferView::showErrorList(string const & action) const
278 if (getErrorList().size()) {
279 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
280 action, buffer()->fileName());
281 owner()->getDialogs().show("errorlist", title);
282 pimpl_->errorlist_.clear();
287 ErrorList const & BufferView::getErrorList() const
289 return pimpl_->errorlist_;
293 void BufferView::setCursorFromRow(int row)
298 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
301 text()->setCursor(cursor(), 0, 0);
303 text()->setCursor(cursor(),
304 text()->parOffset(buffer()->getParFromID(tmpid).pit()),
309 void BufferView::gotoLabel(string const & label)
311 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
312 it != buffer()->inset_iterator_end(); ++it) {
313 vector<string> labels;
314 it->getLabelList(*buffer(), labels);
315 if (find(labels.begin(),labels.end(),label) != labels.end()) {
316 cursor().clearSelection();
317 text()->setCursor(cursor(),
318 distance(text()->paragraphs().begin(), it.getPar()),
320 cursor().resetAnchor();
328 void BufferView::hideCursor()
330 screen().hideCursor();
334 LyXText * BufferView::getLyXText() const
336 LyXText * text = cursor().innerText();
342 Language const * BufferView::getParentLanguage(InsetOld * inset) const
344 Paragraph const & par = ownerPar(*buffer(), inset);
345 return par.getFontSettings(buffer()->params(),
346 par.getPositionOfInset(inset)).language();
350 void BufferView::haveSelection(bool sel)
352 pimpl_->workarea().haveSelection(sel);
356 int BufferView::workHeight() const
358 return pimpl_->workarea().workHeight();
362 LyXText * BufferView::text() const
364 return buffer() ? &buffer()->text() : 0;
368 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
370 LCursor & cur = cursor();
372 cur.push(buffer()->inset());
373 ParIterator::PosHolder const & positions = par.positions();
374 int const last = par.size() - 1;
375 for (int i = 0; i < last; ++i)
376 (*positions[i].it)->inset->edit(cur, true);
378 LyXText * text = par.text(*buffer());
379 text->setCursor(cur, text->parOffset(par.pit()), pos);
384 if the fitCursor call refers to some point in never-explored-land, then we
385 don't have y information in insets there, then we cannot even do an update
386 to get it (because we need the y infomation for setting top_y first). So
387 this is solved in putSelectionAt with:
389 - setting top_y to the y of the outerPar (that has good info)
391 - calling cursor().updatePos()
392 - then call fitCursor()
397 void BufferView::putSelectionAt(PosIterator const & cur,
398 int length, bool backwards)
400 ParIterator par(cur);
402 cursor().clearSelection();
404 LyXText * text = par.text(*buffer());
405 setCursor(par, cur.pos());
407 // hack for the chicken and egg problem
409 top_y(par.outerPar()->y);
411 text->setCursor(cursor(), text->parOffset(cur.pit()), cur.pos());
412 cursor().updatePos();
415 text->setSelectionRange(cursor(), length);
416 cursor().setSelection();
419 swap(cursor().cursor_, cursor().anchor_);
421 DocumentIterator it = cursor();
422 cursor().setCursor(cursor().anchor_, false);
423 cursor().anchor_ = it;
433 LCursor & BufferView::cursor()
435 return pimpl_->cursor_;
439 LCursor const & BufferView::cursor() const
441 return pimpl_->cursor_;