]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcollapsable.C
The speed patch: redraw only rows that have changed
[lyx.git] / src / insets / insetcollapsable.C
index 410cd90a589900a3e998fbf9f51b21d0b88f0034..7d39be20b645f692aa720113a0470bfc59750fea 100644 (file)
@@ -40,9 +40,16 @@ using std::min;
 using std::ostream;
 
 
+InsetCollapsable::CollapseStatus InsetCollapsable::status() const
+{
+       return (autoOpen_ && status_ != Inlined) ? Open : status_;
+}
+
+
 InsetCollapsable::InsetCollapsable
                (BufferParams const & bp, CollapseStatus status)
-       : InsetText(bp), label("Label"), status_(status), openinlined_(false)
+       : InsetText(bp), label("Label"), status_(status),
+         openinlined_(false), autoOpen_(false)
 {
        setAutoBreakRows(true);
        setDrawFrame(true);
@@ -122,14 +129,19 @@ Dimension InsetCollapsable::dimensionCollapsed() const
 
 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+       autoOpen_ = mi.base.bv->cursor().isInside(this);
        mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
-       if (status_ == Inlined) {
+
+       if (status() == Inlined) {
                InsetText::metrics(mi, dim);
        } else {
                dim = dimensionCollapsed();
-               if (status_ == Open) {
+               if (status() == Open) {
                        InsetText::metrics(mi, textdim_);
+                       bool oldopeninlined = openinlined_;
                        openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
+                       if (openinlined_ != oldopeninlined)
+                               InsetText::metrics(mi, textdim_);
                        if (openinlined_) {
                                dim.wid += textdim_.wid;
                                dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
@@ -151,7 +163,7 @@ void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
        const int xx = x + TEXT_TO_INSET_OFFSET;
-       if (status_ == Inlined) {
+       if (status() == Inlined) {
                InsetText::draw(pi, xx, y);
        } else {
                Dimension dimc = dimensionCollapsed();
@@ -162,7 +174,7 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
                button_dim.y2 = top + dimc.height();
 
                pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
-               if (status_ == Open) {
+               if (status() == Open) {
                        int textx, texty;
                        if (openinlined_) {
                                textx = xx + dimc.width();
@@ -181,47 +193,44 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
 {
        x += TEXT_TO_INSET_OFFSET;
-       if (status_ == Open) {
+       if (status() == Open) {
                if (openinlined_)
                        x += dimensionCollapsed().wid;
                else
                        y += dimensionCollapsed().des + textdim_.asc;
        }
-       if (status_ != Collapsed)
+       if (status() != Collapsed)
                InsetText::drawSelection(pi, x, y);
 }
 
 
-void InsetCollapsable::getCursorPos
-       (CursorSlice const & sl, int & x, int & y) const
+void InsetCollapsable::cursorPos
+       (CursorSlice const & sl, bool boundary, int & x, int & y) const
 {
-       if (status_ == Collapsed) {
-               x = xo();
-               y = yo();
-               return;
-       }
-
-       InsetText::getCursorPos(sl, x, y);
-       if (status_ == Open) {
+       BOOST_ASSERT(status() != Collapsed);
+       
+       InsetText::cursorPos(sl, boundary, x, y);
+       
+       if (status() == Open) {
                if (openinlined_)
                        x += dimensionCollapsed().wid;
                else
-                       y += dimensionCollapsed().height() - ascent() + TEXT_TO_INSET_OFFSET + textdim_.asc;
+                       y += dimensionCollapsed().height() - ascent()
+                               + TEXT_TO_INSET_OFFSET + textdim_.asc;
        }
-
        x += TEXT_TO_INSET_OFFSET;
 }
 
 
 InsetBase::EDITABLE InsetCollapsable::editable() const
 {
-       return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
+       return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
 }
 
 
 bool InsetCollapsable::descendable() const
 {
-       return status_ != Collapsed;
+       return status() != Collapsed;
 }
 
 
@@ -239,7 +248,7 @@ string const InsetCollapsable::getNewLabel(string const & l) const
        pos_type const n = min(max_length, p_siz);
        pos_type i = 0;
        pos_type j = 0;
-       for; i < n && j < p_siz; ++j) {
+       for (; i < n && j < p_siz; ++j) {
                if (paragraphs().begin()->isInset(j))
                        continue;
                label += paragraphs().begin()->getChar(j);
@@ -263,7 +272,7 @@ void InsetCollapsable::edit(LCursor & cur, bool left)
 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
 {
        //lyxerr << "InsetCollapsable: edit xy" << endl;
-       if (status_ == Collapsed)
+       if (status() == Collapsed)
                return this;
        cur.push(*this);
        return InsetText::editXY(cur, x, y);
@@ -272,14 +281,14 @@ InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
 
 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
-       lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
-               << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
+       //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
+       //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
 
        switch (cmd.action) {
        case LFUN_MOUSE_PRESS:
-               if (status_ == Inlined)
+               if (status() == Inlined)
                        InsetText::doDispatch(cur, cmd);
-               else if (status_ == Open && !hitButton(cmd))
+               else if (status() == Open && !hitButton(cmd))
                        InsetText::doDispatch(cur, cmd);
                else
                        cur.noUpdate();
@@ -290,7 +299,7 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_MOUSE_TRIPLE:
                if (status_ == Inlined)
                        InsetText::doDispatch(cur, cmd);
-               else if (status_ == Open && !hitButton(cmd))
+               else if (status() && !hitButton(cmd))
                        InsetText::doDispatch(cur, cmd);
                else
                        cur.undispatched();
@@ -302,10 +311,10 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                }
 
-               switch (status_) {
+               switch (status()) {
 
                case Collapsed:
-                       lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
+                       //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
                        setStatus(cur, Open);
                        edit(cur, true);
                        cur.bv().cursor() = cur;
@@ -313,18 +322,18 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
 
                case Open: {
                        if (hitButton(cmd)) {
-                               lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
+                               //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
                                setStatus(cur, Collapsed);
                                cur.bv().cursor() = cur;
                        } else {
-                               lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
+                               //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
                                InsetText::doDispatch(cur, cmd);
                        }
                        break;
                }
 
                case Inlined:
-                       lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
+                       //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
                        InsetText::doDispatch(cur, cmd);
                        break;
                }
@@ -335,8 +344,13 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
                        setStatus(cur, Open);
                else if (cmd.argument == "close")
                        setStatus(cur, Collapsed);
-               else if (cmd.argument == "toggle" || cmd.argument.empty())
-                       setStatus(cur, isOpen() ? Collapsed : Open);
+               else if (cmd.argument == "toggle" || cmd.argument.empty()) 
+                       if (isOpen()) {
+                               setStatus(cur, Collapsed);
+                               cur.forwardPosNoDescend();
+                       }
+                       else
+                               setStatus(cur, Open);
                else // if assign or anything else
                        cur.undispatched();
                cur.dispatched();
@@ -368,17 +382,6 @@ bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
 }
 
 
-int InsetCollapsable::scroll(bool recursive) const
-{
-       int sx = UpdatableInset::scroll(false);
-
-       if (recursive)
-               sx += InsetText::scroll(false);
-
-       return sx;
-}
-
-
 void InsetCollapsable::setLabel(string const & l)
 {
        label = l;
@@ -399,14 +402,3 @@ void InsetCollapsable::setLabelFont(LyXFont & font)
        labelfont_ = font;
 }
 
-
-void InsetCollapsable::scroll(BufferView & bv, double sx) const
-{
-       UpdatableInset::scroll(bv, sx);
-}
-
-
-void InsetCollapsable::scroll(BufferView & bv, int offset) const
-{
-       UpdatableInset::scroll(bv, offset);
-}