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)),
67 BufferView::~BufferView()
74 Buffer * BufferView::buffer() const
76 return pimpl_->buffer_;
80 LyXScreen & BufferView::screen() const
82 return pimpl_->screen();
86 LyXView * BufferView::owner() const
88 return pimpl_->owner_;
92 Painter & BufferView::painter() const
94 return pimpl_->painter();
98 void BufferView::buffer(Buffer * b)
104 bool BufferView::newFile(string const & fn, string const & tn, bool named)
106 return pimpl_->newFile(fn, tn, named);
110 bool BufferView::loadLyXFile(string const & fn, bool tl)
112 return pimpl_->loadLyXFile(fn, tl);
116 void BufferView::reload()
118 string const fn = buffer()->fileName();
119 if (bufferlist.close(buffer(), false))
124 void BufferView::resize()
127 pimpl_->resizeCurrentBuffer();
131 bool BufferView::fitCursor()
133 return pimpl_->fitCursor();
137 void BufferView::update()
143 void BufferView::updateScrollbar()
145 pimpl_->updateScrollbar();
149 void BufferView::scrollDocView(int value)
151 pimpl_->scrollDocView(value);
155 void BufferView::redoCurrentBuffer()
157 pimpl_->redoCurrentBuffer();
161 bool BufferView::available() const
163 return pimpl_->available();
167 Change const BufferView::getCurrentChange()
169 return pimpl_->getCurrentChange();
173 void BufferView::savePosition(unsigned int i)
175 pimpl_->savePosition(i);
179 void BufferView::restorePosition(unsigned int i)
181 pimpl_->restorePosition(i);
185 bool BufferView::isSavedPosition(unsigned int i)
187 return pimpl_->isSavedPosition(i);
191 void BufferView::switchKeyMap()
193 pimpl_->switchKeyMap();
197 int BufferView::workWidth() const
199 return pimpl_->workarea().workWidth();
203 void BufferView::center()
209 int BufferView::top_y() const
211 return pimpl_->top_y();
215 void BufferView::top_y(int y)
221 string const BufferView::getClipboard() const
223 return pimpl_->workarea().getClipboard();
227 void BufferView::stuffClipboard(string const & stuff) const
229 pimpl_->stuffClipboard(stuff);
233 bool BufferView::dispatch(FuncRequest const & ev)
235 return pimpl_->dispatch(ev);
239 void BufferView::scroll(int lines)
241 pimpl_->scroll(lines);
245 // Inserts a file into current document
246 bool BufferView::insertLyXFile(string const & filen)
248 // Copyright CHT Software Service GmbH
251 // Insert a LyXformat - file into current buffer
253 // Moved from lyx_cb.C (Lgb)
255 BOOST_ASSERT(!filen.empty());
257 string const fname = MakeAbsPath(filen);
259 text->clearSelection();
260 text->breakParagraph(buffer()->paragraphs());
262 bool res = buffer()->readFile(fname, text->cursorPar());
268 void BufferView::showErrorList(string const & action) const
270 if (getErrorList().size()) {
271 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
272 action, buffer()->fileName());
273 owner()->getDialogs().show("errorlist", title);
274 pimpl_->errorlist_.clear();
279 ErrorList const & BufferView::getErrorList() const
281 return pimpl_->errorlist_;
285 void BufferView::setCursorFromRow(int row)
290 buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
293 text->setCursor(0, 0);
295 text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
299 bool BufferView::insertInset(InsetOld * inset, string const & lout)
301 return pimpl_->insertInset(inset, lout);
305 void BufferView::gotoLabel(string const & label)
307 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
308 it != buffer()->inset_iterator_end(); ++it) {
309 vector<string> labels;
310 it->getLabelList(*buffer(), labels);
311 if (find(labels.begin(),labels.end(),label) != labels.end()) {
312 text->clearSelection();
314 std::distance(text->ownerParagraphs().begin(), it.getPar()),
316 text->selection.cursor = text->cursor;
324 void BufferView::undo()
329 owner()->message(_("Undo"));
330 text->clearSelection();
332 owner()->message(_("No further undo information"));
338 void BufferView::redo()
343 owner()->message(_("Redo"));
344 text->clearSelection();
346 owner()->message(_("No further redo information"));
352 void BufferView::replaceWord(string const & replacestring)
357 LyXText * text = getLyXText();
359 text->replaceSelectionWithString(replacestring);
360 text->setSelectionRange(replacestring.length());
362 // Go back so that replacement string is also spellchecked
363 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
364 text->cursorLeft(this);
366 // FIXME: should be done through LFUN
367 buffer()->markDirty();
372 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
374 lyxerr << "BufferView::fitLockedInsetCursor x: " << x
375 << " y: " << y << std::endl;
376 UpdatableInset * tli =
377 static_cast<UpdatableInset *>(cursor().innerInset());
378 if (tli && available()) {
379 lyxerr << " text->cursor.y: " << text->cursor.y() << std::endl;
380 lyxerr << " insetInInsetY: " << tli->insetInInsetY() << std::endl;
381 y += text->cursor.y() + tli->insetInInsetY();
382 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
391 void BufferView::hideCursor()
393 screen().hideCursor();
397 void BufferView::updateInset(InsetOld const * inset)
399 pimpl_->updateInset(inset);
403 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
405 // Check if the label 'from' appears more than once
406 vector<string> labels;
407 buffer()->getLabelList(labels);
409 if (lyx::count(labels.begin(), labels.end(), from) > 1)
412 return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
416 UpdatableInset * BufferView::innerInset() const
418 return static_cast<UpdatableInset*>(cursor().innerInset());
422 LyXText * BufferView::getLyXText() const
424 return cursor().innerText();
428 Language const * BufferView::getParentLanguage(InsetOld * inset) const
430 Paragraph const & par = ownerPar(*buffer(), inset);
431 return par.getFontSettings(buffer()->params(),
432 par.getPositionOfInset(inset)).language();
436 Encoding const * BufferView::getEncoding() const
438 LyXText * text = getLyXText();
439 return text->cursorPar()->getFont(
442 outerFont(text->cursorPar(), text->ownerParagraphs())
443 ).language()->encoding();
447 void BufferView::haveSelection(bool sel)
449 pimpl_->workarea().haveSelection(sel);
453 int BufferView::workHeight() const
455 return pimpl_->workarea().workHeight();
459 LCursor & BufferView::cursor()
461 return pimpl_->cursor_;
465 LCursor const & BufferView::cursor() const
467 return pimpl_->cursor_;
471 void BufferView::x_target(int x)
477 int BufferView::x_target() const
483 void BufferView::updateParagraphDialog()
485 pimpl_->updateParagraphDialog();