]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcollapsable.C
insetcharstyle drawing cosmetics
[lyx.git] / src / insets / insetcollapsable.C
index c9714b04a59de776a3597134853567e909e3e173..3d16d9a15d3143af022f4e6a2ad8678a2b1a477d 100644 (file)
@@ -39,12 +39,23 @@ using std::min;
 using std::ostream;
 
 
+void leaveInset(LCursor & cur, InsetBase const & in)
+{
+       for (unsigned int i = 0; i != cur.size(); ++i) {
+               if (&cur[i].inset() == &in) {
+                       cur.resize(i);
+                       return;
+               }
+       }
+}
+
+
 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
        CollapseStatus status)
        : InsetText(bp), label("Label"), status_(status), openinlined_(false)
 {
        setAutoBreakRows(true);
-       setDrawFrame(InsetText::ALWAYS);
+       setDrawFrame(true);
        setFrameColor(LColor::collapsableframe);
        setInsetName("Collapsable");
        setButtonLabel();
@@ -119,24 +130,28 @@ void InsetCollapsable::dimension_collapsed(Dimension & dim) const
 
 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+       mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
        if (status_ == Inlined) {
                InsetText::metrics(mi, dim);
        } else {
                dimension_collapsed(dim);
                if (status_ == Open) {
-                       Dimension insetdim;
-                       InsetText::metrics(mi, insetdim);
-                       openinlined_ = (insetdim.wid + dim.wid <= mi.base.textwidth);
+                       InsetText::metrics(mi, textdim_);
+                       openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
                        if (openinlined_) {
-                               dim.wid += insetdim.wid;
-                               dim.des = max(dim.des, insetdim.des);
-                               dim.asc = max(dim.asc, insetdim.asc);
+                               dim.wid += textdim_.wid;
+                               dim.des = max(dim.des, textdim_.des);
+                               dim.asc = max(dim.asc, textdim_.asc);
                        } else {
-                               dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
-                               dim.wid = max(dim.wid, insetdim.wid);
+                               dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
+                               dim.wid = max(dim.wid, textdim_.wid);
                        }
                }
        }
+       dim.asc += TEXT_TO_INSET_OFFSET;
+       dim.des += TEXT_TO_INSET_OFFSET;
+       dim.wid += 2 * TEXT_TO_INSET_OFFSET;
+       mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
        dim_ = dim;
 }
 
@@ -149,7 +164,8 @@ void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
 
 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
-       setPosCache(pi, x, y);
+       x += TEXT_TO_INSET_OFFSET;
+       y += TEXT_TO_INSET_OFFSET;
 
        if (status_ == Inlined) {
                InsetText::draw(pi, x, y);
@@ -162,15 +178,17 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
                button_dim.y1 = y - aa + pi.base.bv->top_y();
                button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
 
-               draw_collapsed(pi, x, y);
+               draw_collapsed(pi, x, y - aa + dimc.asc);
                if (status_ == Open) {
-                       x += scroll();
                        if (openinlined_)
-                               InsetText::draw(pi, x + dimc.width(), y - aa + InsetText::ascent());
-                       else 
-                               InsetText::draw(pi, x, y - aa + dimc.height() + InsetText::ascent());
+                               InsetText::draw(pi, x + dimc.width(),
+                                               y - aa + textdim_.asc);
+                       else
+                               InsetText::draw(pi, x, dimc.height()
+                                               + y - aa + textdim_.asc);
                }
        }
+       setPosCache(pi, x, y);
 }
 
 
@@ -222,17 +240,12 @@ void InsetCollapsable::edit(LCursor & cur, bool left)
 }
 
 
-InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
+InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y) const
 {
-       cur.push(*this);
+       cur.push(const_cast<InsetCollapsable&>(*this));
        //lyxerr << "InsetCollapsable: edit xy" << endl;
        if (status_ == Collapsed) {
-               setStatus(Open);
-               // We are not calling editXY() because the row cache of the
-               // inset might be invalid. 'Entering from the left' should be
-               // ok, though.
-               InsetText::edit(cur, true);
-               return this;
+               return const_cast<InsetCollapsable*>(this);
        }
        return InsetText::editXY(cur, x, y);
 }
@@ -240,73 +253,86 @@ InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
 
 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
 {
-       //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
-       //      << "  button y: " << button_dim.y2 << endl;
+//     lyxerr << "InsetCollapsable::priv_dispatch (begin): cmd: " << cmd
+//            << "  button y: " << button_dim.y2
+//            << "  coll/inline/open: " << status_ << endl;
+
+       lyxerr << "InsetCollapsable::priv_dispatch (begin): cmd: " << cmd
+               << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
+
        switch (cmd.action) {
-               case LFUN_MOUSE_PRESS:
-                       if (status_ == Inlined)
-                               InsetText::priv_dispatch(cur, cmd);
-                       else if (status_ == Open && !hitButton(cmd))
-                               InsetText::priv_dispatch(cur, cmd);
-                       break;
+       case LFUN_MOUSE_PRESS:
+               if (status_ == Inlined)
+                       InsetText::priv_dispatch(cur, cmd);
+               else if (status_ == Open && !hitButton(cmd))
+                       InsetText::priv_dispatch(cur, cmd);
+               else
+                 cur.noUpdate();
+               break;
 
-               case LFUN_MOUSE_MOTION:
-                       if (status_ == Inlined)
-                               InsetText::priv_dispatch(cur, cmd);
-                       else if (status_ == Open && !hitButton(cmd))
-                               InsetText::priv_dispatch(cur, cmd);
-                       break;
+       case LFUN_MOUSE_MOTION:
+               if (status_ == Inlined)
+                       InsetText::priv_dispatch(cur, cmd);
+               else if (status_ == Open && !hitButton(cmd))
+                       InsetText::priv_dispatch(cur, cmd);
+               break;
 
-               case LFUN_MOUSE_RELEASE:
-                       if (cmd.button() == mouse_button::button3) {
-                               showInsetDialog(&cur.bv());
-                               break;
-                       }
+       case LFUN_MOUSE_RELEASE:
+               if (cmd.button() == mouse_button::button3) {
+                       showInsetDialog(&cur.bv());
+                       break;
+               }
 
-                       switch (status_) {
+               switch (status_) {
 
-                       case Collapsed:
-                               lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
-                               setStatus(Open);
-                               edit(cur, true);
-                               break;
-
-                       case Open: {
-                               FuncRequest cmd1 = cmd;
-                               if (hitButton(cmd1)) {
-                                       lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
-                                       setStatus(Collapsed);
-                                       cur.undispatched();
-                                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
-                               } else {
-                                       lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
-                                       InsetText::priv_dispatch(cur, cmd);
-                               }
-                               break;
-                       }
+               case Collapsed:
+                       lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
+                       setStatus(Open);
+                       edit(cur, true);
+                       cur.bv().cursor() = cur;
+                       break;
 
-                       case Inlined:
-                               lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
+               case Open: {
+                       if (hitButton(cmd)) {
+                               lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
+                               setStatus(Collapsed);
+                               leaveInset(cur, *this);
+                               cur.bv().cursor() = cur;
+                       } else {
+                               lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
                                InsetText::priv_dispatch(cur, cmd);
-                               break;
                        }
                        break;
+               }
 
-               case LFUN_INSET_TOGGLE:
-                       if (InsetText::text_.toggleInset(cur))
-                               break;
-                       if (status_ == Open) {
-                               setStatus(Inlined);
-                               break;
-                       }
+               case Inlined:
+                       lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
+                       InsetText::priv_dispatch(cur, cmd);
+                       break;
+               }
+               break;
+
+       case LFUN_INSET_TOGGLE:
+               if (cmd.argument == "open")
+                       setStatus(Open);
+               else if (cmd.argument == "close") {
                        setStatus(Collapsed);
+                       leaveInset(cur, *this);
+               } else if (cmd.argument == "toggle"
+                          || cmd.argument.empty()) {
+                       if (isOpen()) {
+                               setStatus(Collapsed);
+                               leaveInset(cur, *this);
+                       } else
+                               setStatus(Open);
+               } else // if assign or anything else
                        cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
-                       break;
+               cur.dispatched();
+               break;
 
-               default:
-                       InsetText::priv_dispatch(cur, cmd);
-                       break;
+       default:
+               InsetText::priv_dispatch(cur, cmd);
+               break;
        }
 }