]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
Cosmetics.
[lyx.git] / src / BufferView.cpp
index 77c53e507a8334ba81e899f5a2529d899a77a742..79662d07d27b083ace12c0625dc214e5a14b0c0a 100644 (file)
@@ -20,7 +20,6 @@
 #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"
@@ -50,7 +49,6 @@
 #include "TexRow.h"
 #include "Text.h"
 #include "TextClass.h"
-#include "toc.h"
 #include "Undo.h"
 #include "VSpace.h"
 #include "WordLangTuple.h"
@@ -204,9 +202,139 @@ void gotoInset(BufferView * bv, Inset_code code, bool 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;
+       }
+}
+
 } // anon namespace
 
 
+/////////////////////////////////////////////////////////////////////
+//
+// BufferView
+//
+/////////////////////////////////////////////////////////////////////
+
+
 BufferView::BufferView(Buffer & buf)
        : width_(0), height_(0), buffer_(buf), wh_(0),
          cursor_(*this),
@@ -252,12 +380,12 @@ Buffer const & BufferView::buffer() const
 
 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;
        }
@@ -446,25 +574,23 @@ void BufferView::setCursorFromScrollbar()
        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());
@@ -484,6 +610,19 @@ Change const BufferView::getCurrentChange() const
 }
 
 
+// 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
@@ -597,8 +736,8 @@ void BufferView::updateOffsetRef()
        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;
 }
@@ -821,21 +960,21 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
        }
 
        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;
 
@@ -1044,7 +1183,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
 
        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();
@@ -1052,7 +1191,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                        //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());
@@ -1068,7 +1207,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
        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());
@@ -1691,6 +1830,109 @@ void BufferView::menuInsertLyXFile(string const & filenm)
 }
 
 
+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);