#include "buffer_funcs.h"
#include "BufferList.h"
#include "BufferParams.h"
-#include "bufferview_funcs.h"
-#include "callback.h" // added for Dispatch functions
#include "CoordCache.h"
#include "CutAndPaste.h"
#include "debug.h"
#include "TexRow.h"
#include "Text.h"
#include "TextClass.h"
-#include "toc.h"
#include "Undo.h"
#include "VSpace.h"
#include "WordLangTuple.h"
#include "insets/InsetText.h"
#include "frontends/alert.h"
+#include "frontends/Delegates.h"
#include "frontends/FileDialog.h"
#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include "support/filetools.h"
#include "support/Package.h"
#include "support/types.h"
+#include "support/fs_extras.h"
#include <boost/bind.hpp>
#include <boost/current_function.hpp>
+#include <boost/next_prior.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <cerrno>
+#include <fstream>
#include <functional>
#include <vector>
using std::distance;
using std::endl;
+using std::ifstream;
using std::istringstream;
+using std::istream_iterator;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
+namespace fs = boost::filesystem;
namespace lyx {
using support::isDirWriteable;
using support::isFileReadable;
using support::makeDisplayPath;
+using support::makeAbsPath;
using support::package;
namespace Alert = frontend::Alert;
/// Return an inset of this class if it exists at the current cursor position
template <class T>
-T * getInsetByCode(Cursor & cur, Inset::Code code)
+T * getInsetByCode(Cursor const & cur, Inset::Code code)
{
- T * inset = 0;
DocIterator it = cur;
- if (it.nextInset() &&
- it.nextInset()->lyxCode() == code) {
- inset = static_cast<T*>(it.nextInset());
+ Inset * inset = it.nextInset();
+ if (inset && inset->lyxCode() == code)
+ return static_cast<T*>(inset);
+ return 0;
+}
+
+
+bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
+ bool same_content);
+
+bool findNextInset(DocIterator & dit, vector<Inset_code> const & codes,
+ string const & contents)
+{
+ DocIterator tmpdit = dit;
+
+ while (tmpdit) {
+ Inset const * inset = tmpdit.nextInset();
+ if (inset
+ && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
+ && (contents.empty() ||
+ static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
+ dit = tmpdit;
+ return true;
+ }
+ tmpdit.forwardInset();
+ }
+
+ return false;
+}
+
+
+/// Looks for next inset with one of the the given code
+bool findInset(DocIterator & dit, vector<Inset_code> const & codes,
+ bool same_content)
+{
+ string contents;
+ DocIterator tmpdit = dit;
+ tmpdit.forwardInset();
+ if (!tmpdit)
+ return false;
+
+ if (same_content) {
+ Inset const * inset = tmpdit.nextInset();
+ if (inset
+ && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
+ contents = static_cast<InsetCommand const *>(inset)->getContents();
+ }
+ }
+
+ if (!findNextInset(tmpdit, codes, contents)) {
+ if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
+ tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
+ if (!findNextInset(tmpdit, codes, contents))
+ return false;
+ } else
+ return false;
+ }
+
+ dit = tmpdit;
+ return true;
+}
+
+
+/// Looks for next inset with the given code
+void findInset(DocIterator & dit, Inset_code code, bool same_content)
+{
+ findInset(dit, vector<Inset_code>(1, code), same_content);
+}
+
+
+/// Moves cursor to the next inset with one of the given codes.
+void gotoInset(BufferView * bv, vector<Inset_code> const & codes,
+ bool same_content)
+{
+ Cursor tmpcur = bv->cursor();
+ if (!findInset(tmpcur, codes, same_content)) {
+ bv->cursor().message(_("No more insets"));
+ return;
+ }
+
+ tmpcur.clearSelection();
+ bv->setCursor(tmpcur);
+}
+
+
+/// Moves cursor to the next inset with given code.
+void gotoInset(BufferView * bv, Inset_code code, bool same_content)
+{
+ gotoInset(bv, vector<Inset_code>(1, code), same_content);
+}
+
+
+
+/// the type of outline operation
+enum OutlineOp {
+ OutlineUp, // Move this header with text down
+ OutlineDown, // Move this header with text up
+ OutlineIn, // Make this header deeper
+ OutlineOut // Make this header shallower
+};
+
+
+void outline(OutlineOp mode, Cursor & cur)
+{
+ Buffer & buf = cur.buffer();
+ pit_type & pit = cur.pit();
+ ParagraphList & pars = buf.text().paragraphs();
+ ParagraphList::iterator bgn = pars.begin();
+ // The first paragraph of the area to be copied:
+ ParagraphList::iterator start = boost::next(bgn, pit);
+ // The final paragraph of area to be copied:
+ ParagraphList::iterator finish = start;
+ ParagraphList::iterator end = pars.end();
+
+ TextClass::const_iterator lit =
+ buf.params().getTextClass().begin();
+ TextClass::const_iterator const lend =
+ buf.params().getTextClass().end();
+
+ int const thistoclevel = start->layout()->toclevel;
+ int toclevel;
+ switch (mode) {
+ case OutlineUp: {
+ // Move out (down) from this section header
+ if (finish != end)
+ ++finish;
+ // Seek the one (on same level) below
+ for (; finish != end; ++finish) {
+ toclevel = finish->layout()->toclevel;
+ if (toclevel != Layout::NOT_IN_TOC
+ && toclevel <= thistoclevel) {
+ break;
+ }
+ }
+ ParagraphList::iterator dest = start;
+ // Move out (up) from this header
+ if (dest == bgn)
+ break;
+ // Search previous same-level header above
+ do {
+ --dest;
+ toclevel = dest->layout()->toclevel;
+ } while(dest != bgn
+ && (toclevel == Layout::NOT_IN_TOC
+ || toclevel > thistoclevel));
+ // Not found; do nothing
+ if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
+ break;
+ pit_type const newpit = std::distance(bgn, dest);
+ pit_type const len = std::distance(start, finish);
+ pit_type const deletepit = pit + len;
+ recordUndo(cur, Undo::ATOMIC, newpit, deletepit - 1);
+ pars.insert(dest, start, finish);
+ start = boost::next(pars.begin(), deletepit);
+ pit = newpit;
+ pars.erase(start, finish);
+ break;
+ }
+ case OutlineDown: {
+ // Go down out of current header:
+ if (finish != end)
+ ++finish;
+ // Find next same-level header:
+ for (; finish != end; ++finish) {
+ toclevel = finish->layout()->toclevel;
+ if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
+ break;
+ }
+ ParagraphList::iterator dest = finish;
+ // Go one down from *this* header:
+ if (dest != end)
+ ++dest;
+ else
+ break;
+ // Go further down to find header to insert in front of:
+ for (; dest != end; ++dest) {
+ toclevel = dest->layout()->toclevel;
+ if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
+ break;
+ }
+ // One such was found:
+ pit_type newpit = std::distance(bgn, dest);
+ pit_type const len = std::distance(start, finish);
+ recordUndo(cur, Undo::ATOMIC, pit, newpit - 1);
+ pars.insert(dest, start, finish);
+ start = boost::next(bgn, pit);
+ pit = newpit - len;
+ pars.erase(start, finish);
+ break;
+ }
+ case OutlineIn:
+ recordUndo(cur);
+ for (; lit != lend; ++lit) {
+ if ((*lit)->toclevel == thistoclevel + 1 &&
+ start->layout()->labeltype == (*lit)->labeltype) {
+ start->layout((*lit));
+ break;
+ }
+ }
+ break;
+ case OutlineOut:
+ recordUndo(cur);
+ for (; lit != lend; ++lit) {
+ if ((*lit)->toclevel == thistoclevel - 1 &&
+ start->layout()->labeltype == (*lit)->labeltype) {
+ start->layout((*lit));
+ break;
+ }
+ }
+ break;
+ default:
+ break;
}
- return inset;
}
} // anon namespace
+/////////////////////////////////////////////////////////////////////
+//
+// BufferView
+//
+/////////////////////////////////////////////////////////////////////
+
+
BufferView::BufferView(Buffer & buf)
: width_(0), height_(0), buffer_(buf), wh_(0),
cursor_(*this),
multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0),
- need_centering_(false), intl_(new Intl), last_inset_(0)
+ need_centering_(false), intl_(new Intl), last_inset_(0),
+ gui_(0)
{
xsel_cache_.set = false;
intl_->initKeyMapper(lyxrc.use_kbmap);
bool BufferView::fitCursor()
{
- if (bv_funcs::status(this, cursor_) == bv_funcs::CUR_INSIDE) {
+ if (cursorStatus(cursor_) == CUR_INSIDE) {
frontend::FontMetrics const & fm =
theFontMetrics(cursor_.getFont());
int const asc = fm.maxAscent();
int const des = fm.maxDescent();
- Point const p = bv_funcs::getPos(*this, cursor_, cursor_.boundary());
+ Point const p = getPos(cursor_, cursor_.boundary());
if (p.y_ - asc >= 0 && p.y_ + des < height_)
return false;
}
bool const single_par = !full_metrics;
updateMetrics(single_par);
- if (flags & Update::FitCursor && fitCursor())
- updateMetrics(false);
+ if (flags & Update::FitCursor) {
+ //FIXME: updateMetrics() does not update paragraph position
+ // This is done at draw() time. So we need a redraw!
+ buffer_.changed();
+ if (fitCursor())
+ updateMetrics(false);
+ else
+ // The screen has already been updated thanks to the
+ // 'buffer_.changed()' call three line above. So no need
+ // to redraw again.
+ return false;
+ }
// tell the frontend to update the screen.
return true;
int const last = height_ - height;
Cursor & cur = cursor_;
- bv_funcs::CurStatus st = bv_funcs::status(this, cur);
-
- switch (st) {
- case bv_funcs::CUR_ABOVE:
- // We reset the cursor because bv_funcs::status() does not
+ switch (cursorStatus(cur)) {
+ case CUR_ABOVE:
+ // We reset the cursor because cursorStatus() does not
// work when the cursor is within mathed.
cur.reset(buffer_.inset());
tm.setCursorFromCoordinates(cur, 0, first);
cur.clearSelection();
break;
- case bv_funcs::CUR_BELOW:
- // We reset the cursor because bv_funcs::status() does not
+ case CUR_BELOW:
+ // We reset the cursor because cursorStatus() does not
// work when the cursor is within mathed.
cur.reset(buffer_.inset());
tm.setCursorFromCoordinates(cur, 0, last);
cur.clearSelection();
break;
- case bv_funcs::CUR_INSIDE:
- int const y = bv_funcs::getPos(*this, cur, cur.boundary()).y_;
+ case CUR_INSIDE:
+ int const y = getPos(cur, cur.boundary()).y_;
int const newy = min(last, max(y, first));
if (y != newy) {
cur.reset(buffer_.inset());
}
+// this could be used elsewhere as well?
+// FIXME: This does not work within mathed!
+CursorStatus BufferView::cursorStatus(DocIterator const & dit) const
+{
+ Point const p = getPos(dit, dit.boundary());
+ if (p.y_ < 0)
+ return CUR_ABOVE;
+ if (p.y_ > workHeight())
+ return CUR_BELOW;
+ return CUR_INSIDE;
+}
+
+
void BufferView::saveBookmark(unsigned int idx)
{
// tenatively save bookmark, id and pos will be used to
CursorSlice & bot = cursor_.bottom();
TextMetrics & tm = text_metrics_[bot.text()];
ParagraphMetrics const & pm = tm.parMetrics(bot.pit());
- Point p = bv_funcs::coordOffset(*this, cursor_, cursor_.boundary());
- offset_ref_ = p.y_ + pm.ascent() - height_ / 2;
+ int y = coordOffset(cursor_, cursor_.boundary()).y_;
+ offset_ref_ = y + pm.ascent() - height_ / 2;
need_centering_ = false;
}
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
// FIXME UNICODE
- insertPlaintextFile(this, to_utf8(cmd.argument()), true);
+ insertPlaintextFile(to_utf8(cmd.argument()), true);
break;
case LFUN_FILE_INSERT_PLAINTEXT:
// FIXME UNICODE
- insertPlaintextFile(this, to_utf8(cmd.argument()), false);
+ insertPlaintextFile(to_utf8(cmd.argument()), false);
break;
case LFUN_FONT_STATE:
}
case LFUN_OUTLINE_UP:
- toc::outline(toc::Up, cursor_);
+ outline(OutlineUp, cursor_);
cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
updateLabels(buffer_);
break;
case LFUN_OUTLINE_DOWN:
- toc::outline(toc::Down, cursor_);
+ outline(OutlineDown, cursor_);
cursor_.text()->setCursor(cursor_, cursor_.pit(), 0);
updateLabels(buffer_);
break;
case LFUN_OUTLINE_IN:
- toc::outline(toc::In, cursor_);
+ outline(OutlineIn, cursor_);
updateLabels(buffer_);
break;
case LFUN_OUTLINE_OUT:
- toc::outline(toc::Out, cursor_);
+ outline(OutlineOut, cursor_);
updateLabels(buffer_);
break;
case LFUN_NOTE_NEXT:
- bv_funcs::gotoInset(this, Inset::NOTE_CODE, false);
+ gotoInset(this, Inset::NOTE_CODE, false);
break;
case LFUN_REFERENCE_NEXT: {
vector<Inset_code> tmp;
tmp.push_back(Inset::LABEL_CODE);
tmp.push_back(Inset::REF_CODE);
- bv_funcs::gotoInset(this, tmp, true);
+ gotoInset(this, tmp, true);
break;
}
case LFUN_BIBTEX_DATABASE_ADD: {
Cursor tmpcur = cursor_;
- bv_funcs::findInset(tmpcur, Inset::BIBTEX_CODE, false);
+ findInset(tmpcur, Inset::BIBTEX_CODE, false);
InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
Inset::BIBTEX_CODE);
if (inset) {
case LFUN_BIBTEX_DATABASE_DEL: {
Cursor tmpcur = cursor_;
- bv_funcs::findInset(tmpcur, Inset::BIBTEX_CODE, false);
+ findInset(tmpcur, Inset::BIBTEX_CODE, false);
InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
Inset::BIBTEX_CODE);
if (inset) {
case LFUN_SCREEN_UP:
case LFUN_SCREEN_DOWN: {
- Point p = bv_funcs::getPos(*this, cur, cur.boundary());
+ Point p = getPos(cur, cur.boundary());
if (p.y_ < 0 || p.y_ > height_) {
// The cursor is off-screen so recenter before proceeding.
center();
//FIXME: updateMetrics() does not update paragraph position
// This is done at draw() time. So we need a redraw!
buffer_.changed();
- p = bv_funcs::getPos(*this, cur, cur.boundary());
+ p = getPos(cur, cur.boundary());
}
scroll(cmd.action == LFUN_SCREEN_UP? - height_ : height_);
cur.reset(buffer_.inset());
case LFUN_SCREEN_DOWN_SELECT: {
cur.selHandle(true);
size_t initial_depth = cur.depth();
- Point const p = bv_funcs::getPos(*this, cur, cur.boundary());
+ Point const p = getPos(cur, cur.boundary());
scroll(cmd.action == LFUN_SCREEN_UP_SELECT? - height_ : height_);
// FIXME: We need to verify if the cursor stayed within an inset...
//cur.reset(buffer_.inset());
}
-bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
+void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
{
//lyxerr << BOOST_CURRENT_FUNCTION << "[ cmd0 " << cmd0 << "]" << endl;
getCoveringInset(buffer_.text(), cmd.x, cmd.y);
if (covering_inset == last_inset_)
// Same inset, no need to do anything...
- return false;
+ return;
bool need_redraw = false;
// const_cast because of setMouseHover().
need_redraw |= inset->setMouseHover(true);
last_inset_ = inset;
if (!need_redraw)
- return false;
+ return;
// if last metrics update was in singlepar mode, WorkArea::redraw() will
// not expose the button for redraw. We adjust here the metrics dimension
- // to enable a full redraw.
- if (metrics_info_.update_strategy == SingleParUpdate) {
- // Build temporary cursor.
- TextMetrics & tm = text_metrics_[&buffer_.text()];
- tm.editXY(cur, cmd.x, cmd.y);
- // collect cursor paragraph iter bounds
- std::pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
- std::pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
- int y1 = firstpm.second->position() - firstpm.second->ascent();
- int y2 = lastpm.second->position() + lastpm.second->descent();
- metrics_info_ = ViewMetricsInfo(firstpm.first, lastpm.first, y1, y2,
- FullScreenUpdate, buffer_.text().paragraphs().size());
- }
+ // to enable a full redraw in any case as this is not costly.
+ TextMetrics & tm = text_metrics_[&buffer_.text()];
+ std::pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
+ std::pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
+ int y1 = firstpm.second->position() - firstpm.second->ascent();
+ int y2 = lastpm.second->position() + lastpm.second->descent();
+ metrics_info_ = ViewMetricsInfo(firstpm.first, lastpm.first, y1, y2,
+ FullScreenUpdate, buffer_.text().paragraphs().size());
+ // Reinitialize anchor to first pit.
+ anchor_ref_ = firstpm.first;
+ offset_ref_ = -y1;
+ LYXERR(Debug::PAINTING)
+ << "Mouse hover detected at: (" << cmd.x << ", " << cmd.y << ")"
+ << "\nTriggering redraw: y1: " << y1 << " y2: " << y2
+ << " pit1: " << firstpm.first << " pit2: " << lastpm.first << endl;
// This event (moving without mouse click) is not passed further.
// This should be changed if it is further utilized.
- return true;
+ buffer_.changed();
+ return;
}
// Build temporary cursor.
// via the temp cursor. If the inset wishes to change the real
// cursor it has to do so explicitly by using
// cur.bv().cursor() = cur; (or similar)
- if (inset) {
+ if (inset)
inset->dispatch(cur, cmd);
- }
// Now dispatch to the temporary cursor. If the real cursor should
// be modified, the inset's dispatch has to do so explicitly.
//Do we have a selection?
theSelection().haveSelection(cursor().selection());
- // Redraw if requested and necessary.
- if (cur.result().dispatched() && cur.result().update())
- return update(cur.result().update());
-
- return false;
+ // If the command has been dispatched,
+ if (cur.result().dispatched()
+ // an update is asked,
+ && cur.result().update()
+ // and redraw is needed,
+ && update(cur.result().update()))
+ // then trigger a redraw.
+ buffer_.changed();
}
while (true) {
std::pair<pit_type, ParagraphMetrics const *> last = tm.last();
int bottom_pos = last.second->position() + last.second->descent();
- if (last.first == text->paragraphs().size() - 1) {
+ if (last.first + 1 == int(text->paragraphs().size())) {
if (bottom_pos <= height_)
return;
offset = min(offset, bottom_pos - height_);
}
-// FIXME: We should split-up updateMetrics() for the singlepar case.
+bool BufferView::singleParUpdate()
+{
+ Text & buftext = buffer_.text();
+ pit_type const bottom_pit = cursor_.bottom().pit();
+ TextMetrics & tm = textMetrics(&buftext);
+ int old_height = tm.parMetrics(bottom_pit).height();
+
+ // In Single Paragraph mode, rebreak only
+ // the (main text, not inset!) paragraph containing the cursor.
+ // (if this paragraph contains insets etc., rebreaking will
+ // recursively descend)
+ tm.redoParagraph(bottom_pit);
+ ParagraphMetrics const & pm = tm.parMetrics(bottom_pit);
+ if (pm.height() != old_height)
+ // Paragraph height has changed so we cannot proceed to
+ // the singlePar optimisation.
+ return false;
+
+ int y1 = pm.position() - pm.ascent();
+ int y2 = pm.position() + pm.descent();
+ metrics_info_ = ViewMetricsInfo(bottom_pit, bottom_pit, y1, y2,
+ SingleParUpdate, buftext.paragraphs().size());
+ LYXERR(Debug::PAINTING)
+ << BOOST_CURRENT_FUNCTION
+ << "\ny1: " << y1
+ << " y2: " << y2
+ << " pit: " << bottom_pit
+ << " singlepar: 1"
+ << endl;
+ return true;
+}
+
+
void BufferView::updateMetrics(bool singlepar)
{
+ if (singlepar && singleParUpdate())
+ // No need to update the full screen metrics.
+ return;
+
Text & buftext = buffer_.text();
pit_type const npit = int(buftext.paragraphs().size());
offset_ref_ = 0;
}
- if (!singlepar) {
- // Clear out the position cache in case of full screen redraw,
- coord_cache_.clear();
+ // Clear out the position cache in case of full screen redraw,
+ coord_cache_.clear();
- // Clear out paragraph metrics to avoid having invalid metrics
- // in the cache from paragraphs not relayouted below
- // The complete text metrics will be redone.
- text_metrics_.clear();
- }
+ // Clear out paragraph metrics to avoid having invalid metrics
+ // in the cache from paragraphs not relayouted below
+ // The complete text metrics will be redone.
+ text_metrics_.clear();
TextMetrics & tm = textMetrics(&buftext);
- pit_type const bottom_pit = cursor_.bottom().pit();
- // If the paragraph metrics has changed, we can not
- // use the singlepar optimisation.
- if (singlepar
- // In Single Paragraph mode, rebreak only
- // the (main text, not inset!) paragraph containing the cursor.
- // (if this paragraph contains insets etc., rebreaking will
- // recursively descend)
- && !tm.redoParagraph(bottom_pit)) {
-
- updateOffsetRef();
- // collect cursor paragraph iter bounds
- ParagraphMetrics const & pm = tm.parMetrics(bottom_pit);
- int y1 = pm.position() - pm.ascent();
- int y2 = pm.position() + pm.descent();
- metrics_info_ = ViewMetricsInfo(bottom_pit, bottom_pit, y1, y2,
- SingleParUpdate, npit);
- LYXERR(Debug::PAINTING)
- << BOOST_CURRENT_FUNCTION
- << "\ny1: " << y1
- << " y2: " << y2
- << " pit: " << bottom_pit
- << " singlepar: " << singlepar
- << endl;
- return;
- }
-
pit_type const pit = anchor_ref_;
int pit1 = pit;
int pit2 = pit;
// Rebreak anchor paragraph.
tm.redoParagraph(pit);
+ // Take care of anchor offset if case a recentering is needed.
updateOffsetRef();
int y0 = tm.parMetrics(pit).ascent() - offset_ref_;
<< " pit1: " << pit1
<< " pit2: " << pit2
<< " npit: " << npit
- << " singlepar: " << singlepar
+ << " singlepar: 0"
<< endl;
metrics_info_ = ViewMetricsInfo(pit1, pit2, y1, y2,
}
+Point BufferView::coordOffset(DocIterator const & dit, bool boundary) const
+{
+ int x = 0;
+ int y = 0;
+ int lastw = 0;
+
+ // Addup contribution of nested insets, from inside to outside,
+ // keeping the outer paragraph for a special handling below
+ for (size_t i = dit.depth() - 1; i >= 1; --i) {
+ CursorSlice const & sl = dit[i];
+ int xx = 0;
+ int yy = 0;
+
+ // get relative position inside sl.inset()
+ sl.inset().cursorPos(*this, sl, boundary && (i + 1 == dit.depth()), xx, yy);
+
+ // Make relative position inside of the edited inset relative to sl.inset()
+ x += xx;
+ y += yy;
+
+ // In case of an RTL inset, the edited inset will be positioned to the left
+ // of xx:yy
+ if (sl.text()) {
+ bool boundary_i = boundary && i + 1 == dit.depth();
+ bool rtl = textMetrics(sl.text()).isRTL(sl, boundary_i);
+ if (rtl)
+ x -= lastw;
+ }
+
+ // remember width for the case that sl.inset() is positioned in an RTL inset
+ if (i && dit[i - 1].text()) {
+ // If this Inset is inside a Text Inset, retrieve the Dimension
+ // from the containing text instead of using Inset::dimension() which
+ // might not be implemented.
+ // FIXME (Abdel 23/09/2007): this is a bit messy because of the
+ // elimination of Inset::dim_ cache. This coordOffset() method needs
+ // to be rewritten in light of the new design.
+ Dimension const & dim = parMetrics(dit[i - 1].text(),
+ dit[i - 1].pit()).insetDimension(&sl.inset());
+ lastw = dim.wid;
+ } else {
+ Dimension const dim = sl.inset().dimension(*this);
+ lastw = dim.wid;
+ }
+
+ //lyxerr << "Cursor::getPos, i: "
+ // << i << " x: " << xx << " y: " << y << endl;
+ }
+
+ // Add contribution of initial rows of outermost paragraph
+ CursorSlice const & sl = dit[0];
+ TextMetrics const & tm = textMetrics(sl.text());
+ ParagraphMetrics const & pm = tm.parMetrics(sl.pit());
+ BOOST_ASSERT(!pm.rows().empty());
+ y -= pm.rows()[0].ascent();
+#if 1
+ // FIXME: document this mess
+ size_t rend;
+ if (sl.pos() > 0 && dit.depth() == 1) {
+ int pos = sl.pos();
+ if (pos && boundary)
+ --pos;
+// lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << std::endl;
+ rend = pm.pos2row(pos);
+ } else
+ rend = pm.pos2row(sl.pos());
+#else
+ size_t rend = pm.pos2row(sl.pos());
+#endif
+ for (size_t rit = 0; rit != rend; ++rit)
+ y += pm.rows()[rit].height();
+ y += pm.rows()[rend].ascent();
+
+ TextMetrics const & bottom_tm = textMetrics(dit.bottom().text());
+
+ // Make relative position from the nested inset now bufferview absolute.
+ int xx = bottom_tm.cursorX(dit.bottom(), boundary && dit.depth() == 1);
+ x += xx;
+
+ // In the RTL case place the nested inset at the left of the cursor in
+ // the outer paragraph
+ bool boundary_1 = boundary && 1 == dit.depth();
+ bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
+ if (rtl)
+ x -= lastw;
+
+ return Point(x, y);
+}
+
+
+Point BufferView::getPos(DocIterator const & dit, bool boundary) const
+{
+ CursorSlice const & bot = dit.bottom();
+ TextMetrics const & tm = textMetrics(bot.text());
+ if (!tm.has(bot.pit()))
+ return Point(-1, -1);
+
+ Point p = coordOffset(dit, boundary); // offset from outer paragraph
+ p.y_ += tm.parMetrics(bot.pit()).position();
+ return p;
+}
+
+
void BufferView::draw(frontend::Painter & pain)
{
PainterInfo pi(this, pain);
height_ - metrics_info_.y2, Color::bottomarea);
}
+
+void BufferView::message(docstring const & msg)
+{
+ if (gui_)
+ gui_->message(msg);
+}
+
+
+void BufferView::showDialog(std::string const & name)
+{
+ if (gui_)
+ gui_->showDialog(name);
+}
+
+
+void BufferView::showDialogWithData(std::string const & name,
+ std::string const & data)
+{
+ if (gui_)
+ gui_->showDialogWithData(name, data);
+}
+
+
+void BufferView::showInsetDialog(std::string const & name,
+ std::string const & data, Inset * inset)
+{
+ if (gui_)
+ gui_->showInsetDialog(name, data, inset);
+}
+
+
+void BufferView::updateDialog(std::string const & name, std::string const & data)
+{
+ if (gui_)
+ gui_->updateDialog(name, data);
+}
+
+
+void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
+{
+ gui_ = gui;
+}
+
+
+// FIXME: Move this out of BufferView again
+docstring BufferView::contentsOfPlaintextFile(string const & f,
+ bool asParagraph)
+{
+ FileName fname(f);
+
+ if (fname.empty()) {
+ FileDialog fileDlg(_("Select file to insert"),
+ ( asParagraph
+ ? LFUN_FILE_INSERT_PLAINTEXT_PARA
+ : LFUN_FILE_INSERT_PLAINTEXT) );
+
+ FileDialog::Result result =
+ fileDlg.open(from_utf8(buffer().filePath()),
+ FileFilterList(), docstring());
+
+ if (result.first == FileDialog::Later)
+ return docstring();
+
+ fname = makeAbsPath(to_utf8(result.second));
+
+ if (fname.empty())
+ return docstring();
+ }
+
+ if (!fs::is_readable(fname.toFilesystemEncoding())) {
+ docstring const error = from_ascii(strerror(errno));
+ docstring const file = makeDisplayPath(fname.absFilename(), 50);
+ docstring const text =
+ bformat(_("Could not read the specified document\n"
+ "%1$s\ndue to the error: %2$s"), file, error);
+ Alert::error(_("Could not read file"), text);
+ return docstring();
+ }
+
+ ifstream ifs(fname.toFilesystemEncoding().c_str());
+ if (!ifs) {
+ docstring const error = from_ascii(strerror(errno));
+ docstring const file = makeDisplayPath(fname.absFilename(), 50);
+ docstring const text =
+ bformat(_("Could not open the specified document\n"
+ "%1$s\ndue to the error: %2$s"), file, error);
+ Alert::error(_("Could not open file"), text);
+ return docstring();
+ }
+
+ ifs.unsetf(std::ios::skipws);
+ istream_iterator<char> ii(ifs);
+ istream_iterator<char> end;
+#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
+ // We use this until the compilers get better...
+ std::vector<char> tmp;
+ copy(ii, end, back_inserter(tmp));
+ string const tmpstr(tmp.begin(), tmp.end());
+#else
+ // This is what we want to use and what we will use once the
+ // compilers get good enough.
+ //string tmpstr(ii, end); // yet a reason for using std::string
+ // alternate approach to get the file into a string:
+ string tmpstr;
+ copy(ii, end, back_inserter(tmpstr));
+#endif
+
+ // FIXME UNICODE: We don't know the encoding of the file
+ docstring file_content = from_utf8(tmpstr);
+ if (file_content.empty()) {
+ Alert::error(_("Reading not UTF-8 encoded file"),
+ _("The file is not UTF-8 encoded.\n"
+ "It will be read as local 8Bit-encoded.\n"
+ "If this does not give the correct result\n"
+ "then please change the encoding of the file\n"
+ "to UTF-8 with a program other than LyX.\n"));
+ file_content = from_local8bit(tmpstr);
+ }
+
+ return normalize_c(file_content);
+}
+
+
+void BufferView::insertPlaintextFile(string const & f, bool asParagraph)
+{
+ docstring const tmpstr = contentsOfPlaintextFile(f, asParagraph);
+
+ if (tmpstr.empty())
+ return;
+
+ Cursor & cur = cursor();
+ cap::replaceSelection(cur);
+ recordUndo(cur);
+ if (asParagraph)
+ cur.innerText()->insertStringAsParagraphs(cur, tmpstr);
+ else
+ cur.innerText()->insertStringAsLines(cur, tmpstr);
+}
+
+
} // namespace lyx