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 width, int height)
69 : pimpl_(new Pimpl(*this, owner, 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 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
246 return pimpl_->getStatus(cmd);
250 bool BufferView::dispatch(FuncRequest const & ev)
252 return pimpl_->dispatch(ev);
256 void BufferView::scroll(int lines)
258 pimpl_->scroll(lines);
262 // Inserts a file into current document
263 bool BufferView::insertLyXFile(string const & filen)
265 // Copyright CHT Software Service GmbH
268 // Insert a LyXformat - file into current buffer
270 // Moved from lyx_cb.C (Lgb)
272 BOOST_ASSERT(!filen.empty());
274 string const fname = MakeAbsPath(filen);
276 cursor().clearSelection();
277 text()->breakParagraph(cursor());
279 BOOST_ASSERT(cursor().inTexted());
280 bool res = buffer()->readFile(fname, cursor().par());
286 void BufferView::showErrorList(string const & action) const
288 if (getErrorList().size()) {
289 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
290 action, buffer()->fileName());
291 owner()->getDialogs().show("errorlist", title);
292 pimpl_->errorlist_.clear();
297 ErrorList const & BufferView::getErrorList() const
299 return pimpl_->errorlist_;
303 void BufferView::setCursorFromRow(int row)
308 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
311 text()->setCursor(cursor(), 0, 0);
313 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(),
318 void BufferView::gotoLabel(string const & label)
320 for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
321 vector<string> labels;
322 it->getLabelList(*buffer(), labels);
323 if (find(labels.begin(),labels.end(),label) != labels.end()) {
324 cursor().clearSelection();
325 text()->setCursor(cursor(), it.par(), it.pos());
326 cursor().resetAnchor();
334 void BufferView::hideCursor()
336 screen().hideCursor();
340 LyXText * BufferView::getLyXText() const
342 LyXText * text = cursor().innerText();
348 Language const * BufferView::getParentLanguage(InsetOld * inset) const
350 Paragraph const & par = ownerPar(*buffer(), inset);
351 return par.getFontSettings(buffer()->params(),
352 par.getPositionOfInset(inset)).language();
356 void BufferView::haveSelection(bool sel)
358 pimpl_->workarea().haveSelection(sel);
362 int BufferView::workHeight() const
364 return pimpl_->workarea().workHeight();
368 LyXText * BufferView::text() const
370 return buffer() ? &buffer()->text() : 0;
374 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
376 int const last = par.size();
377 for (int i = 0; i < last; ++i)
378 par[i].inset().edit(cursor(), true);
380 cursor().setCursor(makeDocIterator(par, pos), false);
381 par.bottom().text()->redoParagraph(par.bottom().par());
385 void BufferView::putSelectionAt(DocIterator const & cur,
386 int length, bool backwards)
388 ParIterator par(cur);
390 cursor().clearSelection();
392 setCursor(par, cur.pos());
396 cursor().setSelection(cursor(), -length);
397 DocIterator const it = cursor();
398 cursor().setCursor(cursor().anchor_, true);
399 cursor().anchor_ = it;
401 cursor().setSelection(cursor(), length);
406 LCursor & BufferView::cursor()
408 return pimpl_->cursor_;
412 LCursor const & BufferView::cursor() const
414 return pimpl_->cursor_;