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 Buffer * BufferView::buffer() const
73 return pimpl_->buffer_;
77 LyXScreen & BufferView::screen() const
79 return pimpl_->screen();
83 LyXView * BufferView::owner() const
85 return pimpl_->owner_;
89 Painter & BufferView::painter() const
91 return pimpl_->painter();
95 void BufferView::buffer(Buffer * b)
101 bool BufferView::newFile(string const & fn, string const & tn, bool named)
103 return pimpl_->newFile(fn, tn, named);
107 bool BufferView::loadLyXFile(string const & fn, bool tl)
109 return pimpl_->loadLyXFile(fn, tl);
113 void BufferView::reload()
115 string const fn = buffer()->fileName();
116 if (bufferlist.close(buffer(), false))
121 void BufferView::resize()
124 pimpl_->resizeCurrentBuffer();
128 bool BufferView::fitCursor()
130 return pimpl_->fitCursor();
134 void BufferView::update()
140 void BufferView::updateScrollbar()
142 pimpl_->updateScrollbar();
146 void BufferView::scrollDocView(int value)
148 pimpl_->scrollDocView(value);
152 void BufferView::redoCurrentBuffer()
154 pimpl_->redoCurrentBuffer();
158 bool BufferView::available() const
160 return pimpl_->available();
164 Change const BufferView::getCurrentChange()
166 return pimpl_->getCurrentChange();
170 void BufferView::savePosition(unsigned int i)
172 pimpl_->savePosition(i);
176 void BufferView::restorePosition(unsigned int i)
178 pimpl_->restorePosition(i);
182 bool BufferView::isSavedPosition(unsigned int i)
184 return pimpl_->isSavedPosition(i);
188 void BufferView::switchKeyMap()
190 pimpl_->switchKeyMap();
194 int BufferView::workWidth() const
196 return pimpl_->workarea().workWidth();
200 void BufferView::center()
206 int BufferView::top_y() const
208 return pimpl_->top_y();
212 void BufferView::top_y(int y)
218 string const BufferView::getClipboard() const
220 return pimpl_->workarea().getClipboard();
224 void BufferView::stuffClipboard(string const & stuff) const
226 pimpl_->stuffClipboard(stuff);
230 bool BufferView::dispatch(FuncRequest const & ev)
232 return pimpl_->dispatch(ev);
236 void BufferView::scroll(int lines)
238 pimpl_->scroll(lines);
242 // Inserts a file into current document
243 bool BufferView::insertLyXFile(string const & filen)
245 // Copyright CHT Software Service GmbH
248 // Insert a LyXformat - file into current buffer
250 // Moved from lyx_cb.C (Lgb)
252 BOOST_ASSERT(!filen.empty());
254 string const fname = MakeAbsPath(filen);
256 text()->clearSelection();
257 text()->breakParagraph(buffer()->paragraphs());
259 bool res = buffer()->readFile(fname, text()->cursorPar());
265 void BufferView::showErrorList(string const & action) const
267 if (getErrorList().size()) {
268 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
269 action, buffer()->fileName());
270 owner()->getDialogs().show("errorlist", title);
271 pimpl_->errorlist_.clear();
276 ErrorList const & BufferView::getErrorList() const
278 return pimpl_->errorlist_;
282 void BufferView::setCursorFromRow(int row)
287 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
290 text()->setCursor(0, 0);
292 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
296 bool BufferView::insertInset(InsetOld * inset, string const & lout)
298 return pimpl_->insertInset(inset, lout);
302 void BufferView::gotoLabel(string const & label)
304 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
305 it != buffer()->inset_iterator_end(); ++it) {
306 vector<string> labels;
307 it->getLabelList(*buffer(), labels);
308 if (find(labels.begin(),labels.end(),label) != labels.end()) {
309 text()->clearSelection();
311 std::distance(text()->ownerParagraphs().begin(), it.getPar()),
313 text()->selection.cursor = text()->cursor;
321 void BufferView::undo()
326 owner()->message(_("Undo"));
327 text()->clearSelection();
329 owner()->message(_("No further undo information"));
335 void BufferView::redo()
340 owner()->message(_("Redo"));
341 text()->clearSelection();
343 owner()->message(_("No further redo information"));
349 void BufferView::replaceWord(string const & replacestring)
354 LyXText * t = getLyXText();
356 t->replaceSelectionWithString(replacestring);
357 t->setSelectionRange(replacestring.length());
359 // Go back so that replacement string is also spellchecked
360 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
363 // FIXME: should be done through LFUN
364 buffer()->markDirty();
369 void BufferView::hideCursor()
371 screen().hideCursor();
375 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
377 // Check if the label 'from' appears more than once
378 vector<string> labels;
379 buffer()->getLabelList(labels);
381 if (lyx::count(labels.begin(), labels.end(), from) > 1)
384 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
388 UpdatableInset * BufferView::innerInset() const
390 return static_cast<UpdatableInset*>(cursor().innerInset());
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->ownerParagraphs())
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 LCursor & BufferView::cursor()
435 return pimpl_->cursor_;
439 LCursor const & BufferView::cursor() const
441 return pimpl_->cursor_;
445 void BufferView::x_target(int x)
451 int BufferView::x_target() const
457 void BufferView::updateParagraphDialog()
459 pimpl_->updateParagraphDialog();
463 LyXText * BufferView::text() const
465 return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;