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 BOOST_ASSERT(cursor().inTexted());
271 LyXText * text = cursor().text();
272 bool res = buffer()->readFile(fname, text->getPar(cursor().par()));
278 void BufferView::showErrorList(string const & action) const
280 if (getErrorList().size()) {
281 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
282 action, buffer()->fileName());
283 owner()->getDialogs().show("errorlist", title);
284 pimpl_->errorlist_.clear();
289 ErrorList const & BufferView::getErrorList() const
291 return pimpl_->errorlist_;
295 void BufferView::setCursorFromRow(int row)
300 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
303 text()->setCursor(cursor(), 0, 0);
305 text()->setCursor(cursor(),
306 text()->parOffset(buffer()->getParFromID(tmpid).pit()),
311 void BufferView::gotoLabel(string const & label)
313 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
314 it != buffer()->inset_iterator_end(); ++it) {
315 vector<string> labels;
316 it->getLabelList(*buffer(), labels);
317 if (find(labels.begin(),labels.end(),label) != labels.end()) {
318 cursor().clearSelection();
319 text()->setCursor(cursor(),
320 distance(text()->paragraphs().begin(), it.getPar()),
322 cursor().resetAnchor();
330 void BufferView::hideCursor()
332 screen().hideCursor();
336 LyXText * BufferView::getLyXText() const
338 LyXText * text = cursor().innerText();
344 Language const * BufferView::getParentLanguage(InsetOld * inset) const
346 Paragraph const & par = ownerPar(*buffer(), inset);
347 return par.getFontSettings(buffer()->params(),
348 par.getPositionOfInset(inset)).language();
352 void BufferView::haveSelection(bool sel)
354 pimpl_->workarea().haveSelection(sel);
358 int BufferView::workHeight() const
360 return pimpl_->workarea().workHeight();
364 LyXText * BufferView::text() const
366 return buffer() ? &buffer()->text() : 0;
370 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
372 LCursor & cur = cursor();
373 cur.reset(buffer()->inset());
374 ParIterator::PosHolder const & positions = par.positions();
375 int const last = par.size() - 1;
376 for (int i = 0; i < last; ++i)
377 (*positions[i].it)->inset->edit(cur, true);
379 LyXText & text = *par.text(*buffer());
380 text.setCursor(cur, text.parOffset(par.pit()), pos);
385 if the fitCursor call refers to some point in never-explored-land, then we
386 don't have y information in insets there, then we cannot even do an update
387 to get it (because we need the y infomation for setting top_y first). So
388 this is solved in putSelectionAt with:
390 - setting top_y to the y of the outerPar (that has good info)
392 - calling cursor().updatePos()
393 - then call fitCursor()
398 void BufferView::putSelectionAt(PosIterator const & cur,
399 int length, bool backwards)
401 ParIterator par(cur);
403 cursor().clearSelection();
405 LyXText * text = par.text(*buffer());
406 setCursor(par, cur.pos());
408 // hack for the chicken and egg problem
410 top_y(par.outerPar()->y);
412 text->setCursor(cursor(), text->parOffset(cur.pit()), cur.pos());
413 cursor().updatePos();
416 text->setSelectionRange(cursor(), length);
417 cursor().setSelection();
420 swap(cursor().cursor_, cursor().anchor_);
422 DocumentIterator it = cursor();
423 cursor().setCursor(cursor().anchor_, false);
424 cursor().anchor_ = it;
434 LCursor & BufferView::cursor()
436 return pimpl_->cursor_;
440 LCursor const & BufferView::cursor() const
442 return pimpl_->cursor_;