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 "BufferView_pimpl.h"
23 #include "funcrequest.h"
25 #include "iterators.h"
27 #include "lyxlayout.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "PosIterator.h"
34 #include "WordLangTuple.h"
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/screen.h"
40 #include "frontends/WorkArea.h"
42 #include "insets/insetcommand.h" // ChangeRefs
43 #include "insets/updatableinset.h"
45 #include "support/filetools.h"
46 #include "support/lyxalgo.h" // lyx_count
48 using lyx::support::bformat;
49 using lyx::support::MakeAbsPath;
56 extern BufferList bufferlist;
59 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
60 int width, int height)
61 : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
65 BufferView::~BufferView()
71 void BufferView::unsetXSel()
73 pimpl_->xsel_cache_.set = false;
77 Buffer * BufferView::buffer() const
79 return pimpl_->buffer_;
83 LyXScreen & BufferView::screen() const
85 return pimpl_->screen();
89 LyXView * BufferView::owner() const
91 return pimpl_->owner_;
95 Painter & BufferView::painter() const
97 return pimpl_->painter();
101 void BufferView::buffer(Buffer * b)
107 bool BufferView::newFile(string const & fn, string const & tn, bool named)
109 return pimpl_->newFile(fn, tn, named);
113 bool BufferView::loadLyXFile(string const & fn, bool tl)
115 return pimpl_->loadLyXFile(fn, tl);
119 void BufferView::reload()
121 string const fn = buffer()->fileName();
122 if (bufferlist.close(buffer(), false))
127 void BufferView::resize()
130 pimpl_->resizeCurrentBuffer();
134 bool BufferView::fitCursor()
136 return pimpl_->fitCursor();
140 void BufferView::update()
146 void BufferView::updateScrollbar()
148 pimpl_->updateScrollbar();
152 void BufferView::scrollDocView(int value)
154 pimpl_->scrollDocView(value);
158 void BufferView::redoCurrentBuffer()
160 pimpl_->redoCurrentBuffer();
164 bool BufferView::available() const
166 return pimpl_->available();
170 Change const BufferView::getCurrentChange()
172 return pimpl_->getCurrentChange();
176 void BufferView::savePosition(unsigned int i)
178 pimpl_->savePosition(i);
182 void BufferView::restorePosition(unsigned int i)
184 pimpl_->restorePosition(i);
188 bool BufferView::isSavedPosition(unsigned int i)
190 return pimpl_->isSavedPosition(i);
194 void BufferView::switchKeyMap()
196 pimpl_->switchKeyMap();
200 int BufferView::workWidth() const
202 return pimpl_->workarea().workWidth();
206 void BufferView::center()
212 int BufferView::top_y() const
214 return pimpl_->top_y();
218 void BufferView::top_y(int y)
224 string const BufferView::getClipboard() const
226 return pimpl_->workarea().getClipboard();
230 void BufferView::stuffClipboard(string const & stuff) const
232 pimpl_->stuffClipboard(stuff);
236 bool BufferView::dispatch(FuncRequest const & ev)
238 return pimpl_->dispatch(ev);
242 void BufferView::scroll(int lines)
244 pimpl_->scroll(lines);
248 // Inserts a file into current document
249 bool BufferView::insertLyXFile(string const & filen)
251 // Copyright CHT Software Service GmbH
254 // Insert a LyXformat - file into current buffer
256 // Moved from lyx_cb.C (Lgb)
258 BOOST_ASSERT(!filen.empty());
260 string const fname = MakeAbsPath(filen);
262 cursor().clearSelection();
263 text()->breakParagraph(buffer()->paragraphs());
265 bool res = buffer()->readFile(fname, text()->cursorPar());
271 void BufferView::showErrorList(string const & action) const
273 if (getErrorList().size()) {
274 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
275 action, buffer()->fileName());
276 owner()->getDialogs().show("errorlist", title);
277 pimpl_->errorlist_.clear();
282 ErrorList const & BufferView::getErrorList() const
284 return pimpl_->errorlist_;
288 void BufferView::setCursorFromRow(int row)
293 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
296 text()->setCursor(0, 0);
298 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
302 bool BufferView::insertInset(InsetOld * inset, string const & lout)
304 return pimpl_->insertInset(inset, lout);
308 void BufferView::gotoLabel(string const & label)
310 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
311 it != buffer()->inset_iterator_end(); ++it) {
312 vector<string> labels;
313 it->getLabelList(*buffer(), labels);
314 if (find(labels.begin(),labels.end(),label) != labels.end()) {
315 cursor().clearSelection();
317 std::distance(text()->paragraphs().begin(), it.getPar()),
319 cursor().resetAnchor();
327 void BufferView::undo()
332 owner()->message(_("Undo"));
333 cursor().clearSelection();
335 owner()->message(_("No further undo information"));
341 void BufferView::redo()
346 owner()->message(_("Redo"));
347 cursor().clearSelection();
349 owner()->message(_("No further redo information"));
355 void BufferView::replaceWord(string const & replacestring)
360 LyXText * t = getLyXText();
362 t->replaceSelectionWithString(replacestring);
363 t->setSelectionRange(replacestring.length());
365 // Go back so that replacement string is also spellchecked
366 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
369 // FIXME: should be done through LFUN
370 buffer()->markDirty();
375 void BufferView::hideCursor()
377 screen().hideCursor();
381 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
383 // Check if the label 'from' appears more than once
384 vector<string> labels;
385 buffer()->getLabelList(labels);
387 if (lyx::count(labels.begin(), labels.end(), from) > 1)
390 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
394 LyXText * BufferView::getLyXText() const
396 return cursor().innerText();
400 Language const * BufferView::getParentLanguage(InsetOld * inset) const
402 Paragraph const & par = ownerPar(*buffer(), inset);
403 return par.getFontSettings(buffer()->params(),
404 par.getPositionOfInset(inset)).language();
408 Encoding const * BufferView::getEncoding() const
410 LyXText * t = getLyXText();
413 return t->cursorPar()->getFont(
416 outerFont(t->cursorPar(), t->paragraphs())
417 ).language()->encoding();
421 void BufferView::haveSelection(bool sel)
423 pimpl_->workarea().haveSelection(sel);
427 int BufferView::workHeight() const
429 return pimpl_->workarea().workHeight();
433 void BufferView::updateParagraphDialog()
435 pimpl_->updateParagraphDialog();
439 LyXText * BufferView::text() const
441 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
448 if the fitCursor call refers to some point in never-explored-land, then we
449 don't have y information in insets there, then we cannot even do an update
450 to get it (because we need the y infomation for setting top_y first). So
451 this is solved in put_selection_at with:
453 - setting top_y to the y of the outerPar (that has good info)
455 - calling cursor().updatePos()
456 - then call fitCursor()
461 void BufferView::putSelectionAt(PosIterator const & cur,
462 int length, bool backwards)
464 ParIterator par(cur);
466 cursor().clearSelection();
468 LyXText * text = par.text(*buffer());
470 // hack for the chicken and egg problem
472 top_y(par.outerPar()->y);
474 text->setCursor(cur.pit(), cur.pos());
475 cursor().updatePos();
478 text->setSelectionRange(length);
479 cursor().setSelection();
481 std::swap(cursor().cursor_, cursor().anchor_);
489 LCursor & BufferView::cursor()
491 return pimpl_->cursor_;
495 LCursor const & BufferView::cursor() const
497 return pimpl_->cursor_;