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"
33 #include "WordLangTuple.h"
35 #include "frontends/Alert.h"
36 #include "frontends/Dialogs.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/screen.h"
39 #include "frontends/WorkArea.h"
41 #include "insets/insetcommand.h" // ChangeRefs
42 #include "insets/updatableinset.h"
44 #include "support/filetools.h"
45 #include "support/lyxalgo.h" // lyx_count
47 using lyx::support::bformat;
48 using lyx::support::MakeAbsPath;
55 extern BufferList bufferlist;
58 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
59 int width, int height)
60 : 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 text()->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 text()->clearSelection();
317 std::distance(text()->paragraphs().begin(), it.getPar()),
327 void BufferView::undo()
332 owner()->message(_("Undo"));
333 text()->clearSelection();
335 owner()->message(_("No further undo information"));
341 void BufferView::redo()
346 owner()->message(_("Redo"));
347 text()->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 UpdatableInset * BufferView::innerInset() const
396 return static_cast<UpdatableInset*>(fullCursor().innerInset());
400 LyXText * BufferView::getLyXText() const
402 return fullCursor().innerText();
406 Language const * BufferView::getParentLanguage(InsetOld * inset) const
408 Paragraph const & par = ownerPar(*buffer(), inset);
409 return par.getFontSettings(buffer()->params(),
410 par.getPositionOfInset(inset)).language();
414 Encoding const * BufferView::getEncoding() const
416 LyXText * t = getLyXText();
419 return t->cursorPar()->getFont(
422 outerFont(t->cursorPar(), t->paragraphs())
423 ).language()->encoding();
427 void BufferView::haveSelection(bool sel)
429 pimpl_->workarea().haveSelection(sel);
433 int BufferView::workHeight() const
435 return pimpl_->workarea().workHeight();
439 void BufferView::fullCursor(LCursor const & cur)
441 pimpl_->cursor_ = cur;
445 LCursor & BufferView::fullCursor()
447 return pimpl_->cursor_;
451 LCursor const & BufferView::fullCursor() const
453 return pimpl_->cursor_;
457 CursorSlice & BufferView::cursor()
459 return fullCursor().cursor_.back();
463 CursorSlice const & BufferView::cursor() const
465 return fullCursor().cursor_.back();
469 CursorSlice & BufferView::anchor()
471 return fullCursor().anchor_.back();
475 CursorSlice const & BufferView::anchor() const
477 return fullCursor().anchor_.back();
481 Selection & BufferView::selection()
487 Selection const & BufferView::selection() const
493 void BufferView::x_target(int x)
499 int BufferView::x_target() const
505 void BufferView::updateParagraphDialog()
507 pimpl_->updateParagraphDialog();
511 LyXText * BufferView::text() const
513 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
517 void BufferView::resetAnchor()
519 return fullCursor().resetAnchor();
523 CursorSlice const & BufferView::selStart() const
525 if (!selection().set())
527 // can't use std::min as this creates a new object
528 return anchor() < cursor() ? anchor() : cursor();
532 CursorSlice const & BufferView::selEnd() const
534 if (!selection().set())
536 return anchor() > cursor() ? anchor() : cursor();
540 CursorSlice & BufferView::selStart()
542 if (!selection().set())
544 return anchor() < cursor() ? anchor() : cursor();
548 CursorSlice & BufferView::selEnd()
550 if (selection().set())
552 return anchor() > cursor() ? anchor() : cursor();