]> 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 7967caec2a6cd84abd2bc6631ec5c5c4ceee15a1..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,13 +193,13 @@ 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);
 }
 
@@ -195,32 +207,30 @@ void InsetCollapsable::drawSelection(PainterInfo & pi, 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();
-       } else {
-               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;
-               }
-               x += TEXT_TO_INSET_OFFSET;
+       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;
        }
+       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;
 }
 
 
@@ -262,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);
@@ -276,9 +286,9 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
 
        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();
@@ -289,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();
@@ -301,7 +311,7 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                }
 
-               switch (status_) {
+               switch (status()) {
 
                case Collapsed:
                        //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
@@ -334,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();
@@ -367,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;