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"
23 #include "CutAndPaste.h"
25 #include "funcrequest.h"
26 #include "FuncStatus.h"
28 #include "insetiterator.h"
30 #include "lyxlayout.h"
32 #include "lyxtextclass.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "pariterator.h"
38 #include "WordLangTuple.h"
40 #include "frontends/Alert.h"
41 #include "frontends/Dialogs.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/screen.h"
44 #include "frontends/WorkArea.h"
46 #include "insets/insetcommand.h" // ChangeRefs
47 #include "insets/updatableinset.h"
48 #include "insets/insettext.h"
50 #include "support/filetools.h"
51 #include "support/lyxalgo.h" // lyx_count
53 using lyx::support::bformat;
54 using lyx::support::MakeAbsPath;
56 using lyx::cap::setSelectionRange;
65 extern BufferList bufferlist;
68 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
69 int width, int height)
70 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
74 BufferView::~BufferView()
80 void BufferView::unsetXSel()
82 pimpl_->xsel_cache_.set = false;
86 Buffer * BufferView::buffer() const
88 return pimpl_->buffer_;
92 LyXScreen & BufferView::screen() const
94 return pimpl_->screen();
98 LyXView * BufferView::owner() const
100 return pimpl_->owner_;
104 Painter & BufferView::painter() const
106 return pimpl_->painter();
110 void BufferView::setBuffer(Buffer * b)
112 pimpl_->setBuffer(b);
116 void BufferView::newFile(string const & fn, string const & tn, bool named)
118 pimpl_->newFile(fn, tn, named);
122 bool BufferView::loadLyXFile(string const & fn, bool tl)
124 return pimpl_->loadLyXFile(fn, tl);
128 void BufferView::reload()
130 string const fn = buffer()->fileName();
131 if (bufferlist.close(buffer(), false))
136 void BufferView::resize()
139 pimpl_->resizeCurrentBuffer();
143 bool BufferView::fitCursor()
145 return pimpl_->fitCursor();
149 void BufferView::update()
155 void BufferView::updateScrollbar()
157 pimpl_->updateScrollbar();
161 void BufferView::scrollDocView(int value)
163 pimpl_->scrollDocView(value);
167 void BufferView::redoCurrentBuffer()
169 pimpl_->redoCurrentBuffer();
173 bool BufferView::available() const
175 return pimpl_->available();
179 Change const BufferView::getCurrentChange()
181 return pimpl_->getCurrentChange();
185 void BufferView::savePosition(unsigned int i)
187 pimpl_->savePosition(i);
191 void BufferView::restorePosition(unsigned int i)
193 pimpl_->restorePosition(i);
197 bool BufferView::isSavedPosition(unsigned int i)
199 return pimpl_->isSavedPosition(i);
203 void BufferView::switchKeyMap()
205 pimpl_->switchKeyMap();
209 int BufferView::workWidth() const
211 return pimpl_->workarea().workWidth();
215 void BufferView::center()
221 int BufferView::top_y() const
223 return pimpl_->top_y();
227 void BufferView::top_y(int y)
233 string const BufferView::getClipboard() const
235 return pimpl_->workarea().getClipboard();
239 void BufferView::stuffClipboard(string const & stuff) const
241 pimpl_->stuffClipboard(stuff);
245 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
247 return pimpl_->getStatus(cmd);
251 bool BufferView::dispatch(FuncRequest const & ev)
253 return pimpl_->dispatch(ev);
257 void BufferView::scroll(int lines)
259 pimpl_->scroll(lines);
263 // Inserts a file into current document
264 bool BufferView::insertLyXFile(string const & filen)
266 // Copyright CHT Software Service GmbH
269 // Insert a LyXformat - file into current buffer
271 // Moved from lyx_cb.C (Lgb)
273 BOOST_ASSERT(!filen.empty());
275 string const fname = MakeAbsPath(filen);
277 cursor().clearSelection();
278 text()->breakParagraph(cursor());
280 BOOST_ASSERT(cursor().inTexted());
281 bool res = buffer()->readFile(fname, cursor().par());
287 void BufferView::showErrorList(string const & action) const
289 if (getErrorList().size()) {
290 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
291 action, buffer()->fileName());
292 owner()->getDialogs().show("errorlist", title);
293 pimpl_->errorlist_.clear();
298 ErrorList const & BufferView::getErrorList() const
300 return pimpl_->errorlist_;
304 void BufferView::setCursorFromRow(int row)
309 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
312 text()->setCursor(cursor(), 0, 0);
314 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(),
319 void BufferView::gotoLabel(string const & label)
321 for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
322 vector<string> labels;
323 it->getLabelList(*buffer(), labels);
324 if (find(labels.begin(),labels.end(),label) != labels.end()) {
325 cursor().clearSelection();
326 text()->setCursor(cursor(), it.par(), it.pos());
327 cursor().resetAnchor();
335 void BufferView::hideCursor()
337 screen().hideCursor();
341 LyXText * BufferView::getLyXText() const
343 LyXText * text = cursor().innerText();
349 Language const * BufferView::getParentLanguage(InsetOld * inset) const
351 Paragraph const & par = ownerPar(*buffer(), inset);
352 return par.getFontSettings(buffer()->params(),
353 par.getPositionOfInset(inset)).language();
357 void BufferView::haveSelection(bool sel)
359 pimpl_->workarea().haveSelection(sel);
363 int BufferView::workHeight() const
365 return pimpl_->workarea().workHeight();
369 LyXText * BufferView::text() const
371 return buffer() ? &buffer()->text() : 0;
375 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
377 int const last = par.size();
378 for (int i = 0; i < last; ++i)
379 par[i].inset().edit(cursor(), true);
381 cursor().setCursor(makeDocIterator(par, pos), false);
386 if the fitCursor call refers to some point in never-explored-land, then we
387 don't have y information in insets there, then we cannot even do an update
388 to get it (because we need the y infomation for setting top_y first). So
389 this is solved in putSelectionAt with:
391 - setting top_y to the y of the outerPar (that has good info)
393 - calling cursor().updatePos()
394 - then call fitCursor()
399 void BufferView::putSelectionAt(DocIterator const & cur,
400 int length, bool backwards)
402 ParIterator par(cur);
404 cursor().clearSelection();
406 LyXText & text = *cur[0].text();
407 setCursor(par, cur.pos());
409 // hack for the chicken and egg problem
410 top_y(text.getPar(par.outerPar()).y);
413 //text.setCursor(cursor(), cur.par(), cur.pos());
414 cursor().updatePos();
418 cursor().setSelection(cursor(), -length);
419 DocIterator const it = cursor();
420 cursor().setCursor(cursor().anchor_, true);
421 cursor().anchor_ = it;
423 cursor().setSelection(cursor(), length);
431 LCursor & BufferView::cursor()
433 return pimpl_->cursor_;
437 LCursor const & BufferView::cursor() const
439 return pimpl_->cursor_;