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"
27 #include "insetiterator.h"
29 #include "lyxlayout.h"
31 #include "lyxtextclass.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "pariterator.h"
37 #include "WordLangTuple.h"
39 #include "frontends/Alert.h"
40 #include "frontends/Dialogs.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/screen.h"
43 #include "frontends/WorkArea.h"
45 #include "insets/insetcommand.h" // ChangeRefs
46 #include "insets/updatableinset.h"
47 #include "insets/insettext.h"
49 #include "support/filetools.h"
50 #include "support/lyxalgo.h" // lyx_count
52 using lyx::support::bformat;
53 using lyx::support::MakeAbsPath;
55 using lyx::cap::setSelectionRange;
64 extern BufferList bufferlist;
67 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
68 int width, int height)
69 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
73 BufferView::~BufferView()
79 void BufferView::unsetXSel()
81 pimpl_->xsel_cache_.set = false;
85 Buffer * BufferView::buffer() const
87 return pimpl_->buffer_;
91 LyXScreen & BufferView::screen() const
93 return pimpl_->screen();
97 LyXView * BufferView::owner() const
99 return pimpl_->owner_;
103 Painter & BufferView::painter() const
105 return pimpl_->painter();
109 void BufferView::setBuffer(Buffer * b)
111 pimpl_->setBuffer(b);
115 void BufferView::newFile(string const & fn, string const & tn, bool named)
117 pimpl_->newFile(fn, tn, named);
121 bool BufferView::loadLyXFile(string const & fn, bool tl)
123 return pimpl_->loadLyXFile(fn, tl);
127 void BufferView::reload()
129 string const fn = buffer()->fileName();
130 if (bufferlist.close(buffer(), false))
135 void BufferView::resize()
138 pimpl_->resizeCurrentBuffer();
142 bool BufferView::fitCursor()
144 return pimpl_->fitCursor();
148 void BufferView::update()
154 void BufferView::updateScrollbar()
156 pimpl_->updateScrollbar();
160 void BufferView::scrollDocView(int value)
162 pimpl_->scrollDocView(value);
166 void BufferView::redoCurrentBuffer()
168 pimpl_->redoCurrentBuffer();
172 bool BufferView::available() const
174 return pimpl_->available();
178 Change const BufferView::getCurrentChange()
180 return pimpl_->getCurrentChange();
184 void BufferView::savePosition(unsigned int i)
186 pimpl_->savePosition(i);
190 void BufferView::restorePosition(unsigned int i)
192 pimpl_->restorePosition(i);
196 bool BufferView::isSavedPosition(unsigned int i)
198 return pimpl_->isSavedPosition(i);
202 void BufferView::switchKeyMap()
204 pimpl_->switchKeyMap();
208 int BufferView::workWidth() const
210 return pimpl_->workarea().workWidth();
214 void BufferView::center()
220 int BufferView::top_y() const
222 return pimpl_->top_y();
226 void BufferView::top_y(int y)
232 string const BufferView::getClipboard() const
234 return pimpl_->workarea().getClipboard();
238 void BufferView::stuffClipboard(string const & stuff) const
240 pimpl_->stuffClipboard(stuff);
244 bool BufferView::dispatch(FuncRequest const & ev)
246 return pimpl_->dispatch(ev);
250 void BufferView::scroll(int lines)
252 pimpl_->scroll(lines);
256 // Inserts a file into current document
257 bool BufferView::insertLyXFile(string const & filen)
259 // Copyright CHT Software Service GmbH
262 // Insert a LyXformat - file into current buffer
264 // Moved from lyx_cb.C (Lgb)
266 BOOST_ASSERT(!filen.empty());
268 string const fname = MakeAbsPath(filen);
270 cursor().clearSelection();
271 text()->breakParagraph(cursor());
273 BOOST_ASSERT(cursor().inTexted());
274 bool res = buffer()->readFile(fname, cursor().par());
280 void BufferView::showErrorList(string const & action) const
282 if (getErrorList().size()) {
283 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
284 action, buffer()->fileName());
285 owner()->getDialogs().show("errorlist", title);
286 pimpl_->errorlist_.clear();
291 ErrorList const & BufferView::getErrorList() const
293 return pimpl_->errorlist_;
297 void BufferView::setCursorFromRow(int row)
302 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
305 text()->setCursor(cursor(), 0, 0);
307 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(),
312 void BufferView::gotoLabel(string const & label)
314 for (InsetIterator it(buffer()->inset()); it; ++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(), it.par(), it.pos());
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();
371 cur.setCursor(makeDocumentIterator(par, pos), false);
376 if the fitCursor call refers to some point in never-explored-land, then we
377 don't have y information in insets there, then we cannot even do an update
378 to get it (because we need the y infomation for setting top_y first). So
379 this is solved in putSelectionAt with:
381 - setting top_y to the y of the outerPar (that has good info)
383 - calling cursor().updatePos()
384 - then call fitCursor()
389 void BufferView::putSelectionAt(DocumentIterator const & cur,
390 int length, bool backwards)
392 ParIterator par(cur);
394 cursor().clearSelection();
396 LyXText & text = *par.text();
397 setCursor(par, cur.pos());
399 // hack for the chicken and egg problem
400 top_y(text.getPar(par.outerPar()).y);
403 text.setCursor(cursor(), cur.par(), cur.pos());
404 cursor().updatePos();
407 setSelectionRange(cursor(), length);
408 cursor().setSelection();
410 DocumentIterator const it = cursor();
411 cursor().setCursor(cursor().anchor_, false);
412 cursor().anchor_ = it;
421 LCursor & BufferView::cursor()
423 return pimpl_->cursor_;
427 LCursor const & BufferView::cursor() const
429 return pimpl_->cursor_;