]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcollapsable.C
split LyXFunc::getStatus() into inset specific chunks
[lyx.git] / src / insets / insetcollapsable.C
index 702c0e7651f332e5bea391d1af460ef2eae75f43..0c8a5bef1b166d02abd3712134ca0a32047ec63c 100644 (file)
@@ -39,23 +39,22 @@ using std::min;
 using std::ostream;
 
 
-InsetCollapsable::InsetCollapsable(BufferParams const & bp, CollapseStatus status)
-       : UpdatableInset(), inset(bp), status_(status),
-         label("Label")
+InsetCollapsable::InsetCollapsable(BufferParams const & bp,
+       CollapseStatus status)
+       : inset(bp), label("Label"), status_(status), openinlined_(false)
 {
        inset.setOwner(this);
        inset.setAutoBreakRows(true);
        inset.setDrawFrame(InsetText::ALWAYS);
        inset.setFrameColor(LColor::collapsableframe);
        setInsetName("Collapsable");
-
        setButtonLabel();
 }
 
 
 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
-       : UpdatableInset(in), inset(in.inset), status_(in.status_),
-         labelfont_(in.labelfont_), label(in.label)
+       : UpdatableInset(in), inset(in.inset),
+         labelfont_(in.labelfont_), label(in.label), status_(in.status_)
 {
        inset.setOwner(this);
        setButtonLabel();
@@ -64,20 +63,19 @@ InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
 
 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
 {
-       string st;
-
+       os << "status ";
        switch (status_) {
        case Open:
-               st = "open";
+               os << "open";
                break;
        case Collapsed:
-               st = "collapsed";
+               os << "collapsed";
                break;
        case Inlined:
-               st = "inlined";
+               os << "inlined";
                break;
        }
-       os << "status " << st << "\n";
+       os << "\n";
        inset.text_.write(buf, os);
 }
 
@@ -116,12 +114,8 @@ void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
        }
        inset.read(buf, lex);
 
-       if (!token_found) {
-               if (isOpen())
-                       status_ = Open;
-               else
-                       status_ = Collapsed;
-       }
+       if (!token_found)
+               status_ = isOpen() ? Open : Collapsed;
 
        setButtonLabel();
 }
@@ -133,17 +127,8 @@ void InsetCollapsable::dimension_collapsed(Dimension & dim) const
 }
 
 
-int InsetCollapsable::height_collapsed() const
-{
-       Dimension dim;
-       font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
-       return dim.asc + dim.des;
-}
-
-
 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << endl;
        if (status_ == Inlined) {
                inset.metrics(mi, dim);
        } else {
@@ -151,12 +136,19 @@ void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
                if (status_ == Open) {
                        Dimension insetdim;
                        inset.metrics(mi, insetdim);
-                       dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
-                       dim.wid = max(dim.wid, insetdim.wid);
+                       openinlined_ = (insetdim.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);
+                       } else {
+                               dim.des += insetdim.height()
+                                       + TEXT_TO_BOTTOM_OFFSET;
+                               dim.wid = max(dim.wid, insetdim.wid);
+                       }
                }
        }
        dim_ = dim;
-       //lyxerr << "InsetCollapsable::metrics:  dim.wid: " << dim.wid << endl;
 }
 
 
@@ -168,83 +160,83 @@ void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
 
 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
-       xo_ = x;
-       yo_ = y;
+       setPosCache(pi, x, y);
 
        if (status_ == Inlined) {
                inset.draw(pi, x, y);
        } else {
                Dimension dimc;
                dimension_collapsed(dimc);
-
                int const aa  = ascent();
-               button_dim.x1 = 0;
-               button_dim.x2 = dimc.width();
-               button_dim.y1 = -aa;
-               button_dim.y2 = -aa + dimc.height();
+               button_dim.x1 = x + 0;
+               button_dim.x2 = x + dimc.width();
+               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);
                if (status_ == Open) {
                        if (!owner())
                                x += scroll();
-                       inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
+                       
+                       if (openinlined_)
+                               inset.draw(pi, x + dimc.width(), y - aa + inset.ascent());
+                       else 
+                               inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
                }
        }
 }
 
 
-InsetOld::EDITABLE InsetCollapsable::editable() const
+void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
 {
-       return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
+       inset.drawSelection(pi, x, y);
 }
 
 
-bool InsetCollapsable::descendable() const
+InsetOld::EDITABLE InsetCollapsable::editable() const
 {
-       return status_ != Collapsed;
+       return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
 }
 
 
-FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
+bool InsetCollapsable::descendable() const
 {
-       FuncRequest cmd1 = cmd;
-       cmd1.y += ascent() - height_collapsed();
-       return cmd1;
+       return status_ != Collapsed;
 }
 
 
-DispatchResult InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
+void InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
 {
-       BufferView * bv = cmd.view();
-
        if (cmd.button() == mouse_button::button3) {
-               lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
-               showInsetDialog(bv);
-               return DispatchResult(true, true);
+               showInsetDialog(&cur.bv());
+               return;
        }
 
        switch (status_) {
+
        case Collapsed:
                lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
                setStatus(Open);
-               edit(bv, true);
-               return DispatchResult(true, true);
+               edit(cur, true);
+               break;
 
-       case Open:
-               if (hitButton(cmd)) {
+       case Open: {
+               FuncRequest cmd1 = cmd;
+               if (hitButton(cmd1)) {
                        lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
                        setStatus(Collapsed);
-                       return DispatchResult(false, FINISHED_RIGHT);
-               } else {
-                       lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
-                       return inset.dispatch(adjustCommand(cmd));
-               }       
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       break;
+               }
+               lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
+               inset.dispatch(cur, cmd);
+               break;
+       }
 
        case Inlined:
-               return inset.dispatch(cmd);
+               inset.dispatch(cur, cmd);
+               break;
        }
-
-       return DispatchResult(true, true);
 }
 
 
@@ -276,7 +268,7 @@ int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
 }
 
 
-bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
+bool InsetCollapsable::hitButton(FuncRequest & cmd) const
 {
        return button_dim.contains(cmd.x, cmd.y);
 }
@@ -284,7 +276,7 @@ bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
 
 string const InsetCollapsable::getNewLabel(string const & l) const
 {
-       string la;
+       string label;
        pos_type const max_length = 15;
        pos_type const p_siz = inset.paragraphs().begin()->size();
        pos_type const n = min(max_length, p_siz);
@@ -293,77 +285,86 @@ string const InsetCollapsable::getNewLabel(string const & l) const
        for( ; i < n && j < p_siz; ++j) {
                if (inset.paragraphs().begin()->isInset(j))
                        continue;
-               la += inset.paragraphs().begin()->getChar(j);
+               label += inset.paragraphs().begin()->getChar(j);
                ++i;
        }
        if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
-               la += "...";
+               label += "...";
        }
-       return la.empty() ? l : la;
+       return label.empty() ? l : label;
 }
 
 
-void InsetCollapsable::edit(BufferView * bv, bool left)
+void InsetCollapsable::edit(LCursor & cur, bool left)
 {
-       lyxerr << "InsetCollapsable: edit left/right" << endl;
-       inset.edit(bv, left);
+       //lyxerr << "InsetCollapsable: edit left/right" << endl;
+       cur.push(*this);
+       inset.edit(cur, left);
        open();
-       bv->cursor().push(this);
 }
 
 
-void InsetCollapsable::edit(BufferView * bv, int x, int y)
+InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
 {
-       lyxerr << "InsetCollapsable: edit xy" << endl;
+       cur.push(*this);
+       //lyxerr << "InsetCollapsable: edit xy" << endl;
        if (status_ == Collapsed) {
                setStatus(Open);
-       } else {
-               if (y <= button_dim.y2)
-                       y = 0;
-               else
-                       y += inset.ascent() - height_collapsed();
+               inset.edit(cur, true);
+#warning look here
+//we are not calling edit(x,y) because there are no coordinates in the
+//inset yet. I personally think it's ok. (ab)
+               return this;
        }
-       inset.edit(bv, x, y);
-       bv->cursor().push(this);
+       return inset.editXY(cur, x, y);
 }
 
 
-DispatchResult
-InsetCollapsable::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
+void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
        //      << "  button y: " << button_dim.y2 << endl;
        switch (cmd.action) {
                case LFUN_MOUSE_PRESS:
                        if (status_ == Inlined)
-                               inset.dispatch(cmd);
-                       else if (status_ == Open && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
-                       return DispatchResult(true, true);
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && !hitButton(cmd))
+                               inset.dispatch(cur, cmd);
+                       break;
 
                case LFUN_MOUSE_MOTION:
                        if (status_ == Inlined)
-                               inset.dispatch(cmd);
-                       else if (status_ == Open && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
-                       return DispatchResult(true, true);
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && !hitButton(cmd))
+                               inset.dispatch(cur, cmd);
+                       break;
 
                case LFUN_MOUSE_RELEASE:
-                       return lfunMouseRelease(cmd);
+                       lfunMouseRelease(cur, cmd);
+                       break;
 
                case LFUN_INSET_TOGGLE:
-                       if (inset.text_.toggleInset())
-                               return DispatchResult(true, true);
-                       if (status_ == Open)
+                       if (inset.text_.toggleInset(cur))
+                               break;
+                       if (status_ == Open) {
                                setStatus(Inlined);
-                               return DispatchResult(true, true);
+                               break;
+                       }
                        setStatus(Collapsed);
-                       return DispatchResult(false, FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       break;
 
                default:
-                       return inset.dispatch(adjustCommand(cmd));
+                       inset.dispatch(cur, cmd);
+                       break;
        }
-       //lyxerr << "InsetCollapsable::priv_dispatch (end)" << endl;
+}
+
+
+bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
+       FuncStatus & flag) const
+{
+       return inset.getStatus(cur, cmd, flag);
 }
 
 
@@ -373,11 +374,10 @@ void InsetCollapsable::validate(LaTeXFeatures & features) const
 }
 
 
-void InsetCollapsable::getCursorPos(int & x, int & y) const
+void InsetCollapsable::getCursorPos(CursorSlice const & cur,
+       int & x, int & y) const
 {
-       inset.getCursorPos(x, y);
-       if (status_ != Inlined)
-               y += - ascent() + height_collapsed() + inset.ascent();
+       inset.getCursorPos(cur, x, y);
 }
 
 
@@ -399,9 +399,9 @@ int InsetCollapsable::scroll(bool recursive) const
 }
 
 
-int InsetCollapsable::numParagraphs() const
+size_t InsetCollapsable::nargs() const
 {
-       return inset.numParagraphs();
+       return inset.nargs();
 }
 
 
@@ -461,13 +461,13 @@ void InsetCollapsable::setLabelFont(LyXFont & font)
 }
 
 
-void InsetCollapsable::scroll(BufferView * bv, float sx) const
+void InsetCollapsable::scroll(BufferView & bv, float sx) const
 {
        UpdatableInset::scroll(bv, sx);
 }
 
 
-void InsetCollapsable::scroll(BufferView * bv, int offset) const
+void InsetCollapsable::scroll(BufferView & bv, int offset) const
 {
        UpdatableInset::scroll(bv, offset);
 }