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"
47 #include "support/filetools.h"
48 #include "support/lyxalgo.h" // lyx_count
50 using lyx::support::bformat;
51 using lyx::support::MakeAbsPath;
60 extern BufferList bufferlist;
63 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
64 int width, int height)
65 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
69 BufferView::~BufferView()
75 void BufferView::unsetXSel()
77 pimpl_->xsel_cache_.set = false;
81 Buffer * BufferView::buffer() const
83 return pimpl_->buffer_;
87 LyXScreen & BufferView::screen() const
89 return pimpl_->screen();
93 LyXView * BufferView::owner() const
95 return pimpl_->owner_;
99 Painter & BufferView::painter() const
101 return pimpl_->painter();
105 void BufferView::buffer(Buffer * b)
111 bool BufferView::newFile(string const & fn, string const & tn, bool named)
113 return pimpl_->newFile(fn, tn, named);
117 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 return pimpl_->loadLyXFile(fn, tl);
123 void BufferView::reload()
125 string const fn = buffer()->fileName();
126 if (bufferlist.close(buffer(), false))
131 void BufferView::resize()
134 pimpl_->resizeCurrentBuffer();
138 bool BufferView::fitCursor()
140 return pimpl_->fitCursor();
144 void BufferView::update()
150 void BufferView::updateScrollbar()
152 pimpl_->updateScrollbar();
156 void BufferView::scrollDocView(int value)
158 pimpl_->scrollDocView(value);
162 void BufferView::redoCurrentBuffer()
164 pimpl_->redoCurrentBuffer();
168 bool BufferView::available() const
170 return pimpl_->available();
174 Change const BufferView::getCurrentChange()
176 return pimpl_->getCurrentChange();
180 void BufferView::savePosition(unsigned int i)
182 pimpl_->savePosition(i);
186 void BufferView::restorePosition(unsigned int i)
188 pimpl_->restorePosition(i);
192 bool BufferView::isSavedPosition(unsigned int i)
194 return pimpl_->isSavedPosition(i);
198 void BufferView::switchKeyMap()
200 pimpl_->switchKeyMap();
204 int BufferView::workWidth() const
206 return pimpl_->workarea().workWidth();
210 void BufferView::center()
216 int BufferView::top_y() const
218 return pimpl_->top_y();
222 void BufferView::top_y(int y)
228 string const BufferView::getClipboard() const
230 return pimpl_->workarea().getClipboard();
234 void BufferView::stuffClipboard(string const & stuff) const
236 pimpl_->stuffClipboard(stuff);
240 bool BufferView::dispatch(FuncRequest const & ev)
242 return pimpl_->dispatch(ev);
246 void BufferView::scroll(int lines)
248 pimpl_->scroll(lines);
252 // Inserts a file into current document
253 bool BufferView::insertLyXFile(string const & filen)
255 // Copyright CHT Software Service GmbH
258 // Insert a LyXformat - file into current buffer
260 // Moved from lyx_cb.C (Lgb)
262 BOOST_ASSERT(!filen.empty());
264 string const fname = MakeAbsPath(filen);
266 cursor().clearSelection();
267 text()->breakParagraph(buffer()->paragraphs());
269 bool res = buffer()->readFile(fname, text()->cursorPar());
275 void BufferView::showErrorList(string const & action) const
277 if (getErrorList().size()) {
278 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
279 action, buffer()->fileName());
280 owner()->getDialogs().show("errorlist", title);
281 pimpl_->errorlist_.clear();
286 ErrorList const & BufferView::getErrorList() const
288 return pimpl_->errorlist_;
292 void BufferView::setCursorFromRow(int row)
297 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
300 text()->setCursor(0, 0);
302 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
306 void BufferView::gotoLabel(string const & label)
308 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
309 it != buffer()->inset_iterator_end(); ++it) {
310 vector<string> labels;
311 it->getLabelList(*buffer(), labels);
312 if (find(labels.begin(),labels.end(),label) != labels.end()) {
313 cursor().clearSelection();
315 distance(text()->paragraphs().begin(), it.getPar()),
317 cursor().resetAnchor();
325 void BufferView::undo()
330 owner()->message(_("Undo"));
331 cursor().clearSelection();
332 if (!textUndo(*this))
333 owner()->message(_("No further undo information"));
339 void BufferView::redo()
344 owner()->message(_("Redo"));
345 cursor().clearSelection();
346 if (!textRedo(*this))
347 owner()->message(_("No further redo information"));
353 void BufferView::replaceWord(string const & replacestring)
358 LyXText * t = getLyXText();
360 t->replaceSelectionWithString(replacestring);
361 t->setSelectionRange(replacestring.length());
363 // Go back so that replacement string is also spellchecked
364 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
367 // FIXME: should be done through LFUN
368 buffer()->markDirty();
373 void BufferView::hideCursor()
375 screen().hideCursor();
379 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
381 // Check if the label 'from' appears more than once
382 vector<string> labels;
383 buffer()->getLabelList(labels);
385 if (lyx::count(labels.begin(), labels.end(), from) > 1)
388 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
392 LyXText * BufferView::getLyXText() const
394 return cursor().innerText();
398 Language const * BufferView::getParentLanguage(InsetOld * inset) const
400 Paragraph const & par = ownerPar(*buffer(), inset);
401 return par.getFontSettings(buffer()->params(),
402 par.getPositionOfInset(inset)).language();
406 Encoding const * BufferView::getEncoding() const
408 LyXText * t = getLyXText();
411 CursorSlice const & cur = cursor().innerTextSlice();
412 return t->getPar(cur.par())->getFont(
413 buffer()->params(), cur.pos(),
414 outerFont(t->getPar(cur.par()), t->paragraphs())
415 ).language()->encoding();
419 void BufferView::haveSelection(bool sel)
421 pimpl_->workarea().haveSelection(sel);
425 int BufferView::workHeight() const
427 return pimpl_->workarea().workHeight();
431 LyXText * BufferView::text() const
433 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
437 void BufferView::setCursor(ParIterator const & par,
440 LCursor & cur = cursor();
442 ParIterator::PosHolder const & positions = par.positions();
443 int const last = par.size() - 1;
444 for (int i = 0; i < last; ++i)
445 (*positions[i].it)->inset->edit(cur, true);
447 LyXText * lt = par.text(*buffer());
448 lt->setCursor(par.pit(), pos);
453 if the fitCursor call refers to some point in never-explored-land, then we
454 don't have y information in insets there, then we cannot even do an update
455 to get it (because we need the y infomation for setting top_y first). So
456 this is solved in putSelectionAt with:
458 - setting top_y to the y of the outerPar (that has good info)
460 - calling cursor().updatePos()
461 - then call fitCursor()
466 void BufferView::putSelectionAt(PosIterator const & cur,
467 int length, bool backwards)
469 ParIterator par(cur);
471 cursor().clearSelection();
473 LyXText * text = par.text(*buffer());
474 setCursor(par, cur.pos());
476 // hack for the chicken and egg problem
478 top_y(par.outerPar()->y);
480 text->setCursor(cur.pit(), cur.pos());
481 cursor().updatePos();
484 text->setSelectionRange(length);
485 cursor().setSelection();
487 swap(cursor().cursor_, cursor().anchor_);
495 LCursor & BufferView::cursor()
497 return pimpl_->cursor_;
501 LCursor const & BufferView::cursor() const
503 return pimpl_->cursor_;