]> 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 9f8cac0f42738a934c3f55c3413c40debb771553..0c8a5bef1b166d02abd3712134ca0a32047ec63c 100644 (file)
@@ -39,86 +39,96 @@ using std::min;
 using std::ostream;
 
 
-InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
-       : UpdatableInset(), inset(bp), status_(collapsed ? Collapsed : Open),
-         label("Label")
-#if 0
-       ,autocollapse(false)
-#endif
+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)
-#if 0
-         ,autocollapse(in.autocollapse)
-#endif
+       : UpdatableInset(in), inset(in.inset),
+         labelfont_(in.labelfont_), label(in.label), status_(in.status_)
 {
        inset.setOwner(this);
-}
-
-
-bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
-{
-       if (!insetAllowed(in->lyxCode())) {
-               lyxerr << "InsetCollapsable::InsertInset: "
-                       "Unable to insert inset." << endl;
-               return false;
-       }
-       return inset.insertInset(bv, in);
+       setButtonLabel();
 }
 
 
 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
 {
-       os << "collapsed " << (status_ == Collapsed ? "true" : "false") << "\n";
+       os << "status ";
+       switch (status_) {
+       case Open:
+               os << "open";
+               break;
+       case Collapsed:
+               os << "collapsed";
+               break;
+       case Inlined:
+               os << "inlined";
+               break;
+       }
+       os << "\n";
        inset.text_.write(buf, os);
 }
 
 
 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
 {
+       bool token_found = false;
        if (lex.isOK()) {
                lex.next();
                string const token = lex.getString();
-               if (token == "collapsed") {
+               if (token == "status") {
                        lex.next();
-                       status_ = lex.getBool() ? Collapsed : Open;
+                       string const tmp_token = lex.getString();
+
+                       if (tmp_token == "inlined") {
+                               status_ = Inlined;
+                               token_found = true;
+                       } else if (tmp_token == "collapsed") {
+                               status_ = Collapsed;
+                               token_found = true;
+                       } else if (tmp_token == "open") {
+                               status_ = Open;
+                               token_found = true;
+                       } else {
+                               lyxerr << "InsetCollapsable::read: Missing status!"
+                                      << endl;
+                               // Take countermeasures
+                               lex.pushToken(token);
+                       }
                } else {
-                       lyxerr << "InsetCollapsable::Read: Missing collapsed!"
-                              << endl;
-                       // Take countermeasures
+                       lyxerr << "InsetCollapsable::Read: Missing 'status'-tag!"
+                                  << endl;
+                       // take countermeasures
                        lex.pushToken(token);
                }
        }
        inset.read(buf, lex);
-}
 
+       if (!token_found)
+               status_ = isOpen() ? Open : Collapsed;
 
-void InsetCollapsable::dimension_collapsed(Dimension & dim) const
-{
-       font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
+       setButtonLabel();
 }
 
 
-int InsetCollapsable::height_collapsed() const
+void InsetCollapsable::dimension_collapsed(Dimension & dim) 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 {
@@ -126,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;
 }
 
 
@@ -143,84 +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;
-               if (hitButton(cmd))
-                       showInsetDialog(bv);
-               return DispatchResult(true, true);
+               showInsetDialog(&cur.bv());
+               return;
        }
 
-       if (status_ == Collapsed) {
+       switch (status_) {
+
+       case Collapsed:
                lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
                setStatus(Open);
-               edit(bv, true);
-               return DispatchResult(true, true);
-       }
+               edit(cur, true);
+               break;
 
-       if (hitButton(cmd)) {
-               if (status_ == Open) {
-                       setStatus(Collapsed);
+       case Open: {
+               FuncRequest cmd1 = cmd;
+               if (hitButton(cmd1)) {
                        lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
-                       return DispatchResult(false, FINISHED_RIGHT);
+                       setStatus(Collapsed);
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       break;
                }
-               setStatus(Open);
                lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
-       } else if (status_ == Open && cmd.y > button_dim.y2) {
-               lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
-               return inset.dispatch(adjustCommand(cmd));
+               inset.dispatch(cur, cmd);
+               break;
        }
 
-       lyxerr << "InsetCollapsable::lfunMouseRelease 5" << endl;
-       return DispatchResult(true, true);
+       case Inlined:
+               inset.dispatch(cur, cmd);
+               break;
+       }
 }
 
 
@@ -252,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);
 }
@@ -260,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);
@@ -269,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 += "...";
        }
-       if (la.empty()) {
-               la = l;
-       }
-       return 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);
-       setStatus(Open);
-       bv->cursor().push(this);
+       //lyxerr << "InsetCollapsable: edit left/right" << endl;
+       cur.push(*this);
+       inset.edit(cur, left);
+       open();
 }
 
 
-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_ && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
-                       return DispatchResult(true, true);
+                       if (status_ == Inlined)
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && !hitButton(cmd))
+                               inset.dispatch(cur, cmd);
+                       break;
 
                case LFUN_MOUSE_MOTION:
-                       if (!status_)
-                               inset.dispatch(adjustCommand(cmd));
-                       return DispatchResult(true, true);
+                       if (status_ == Inlined)
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && !hitButton(cmd))
+                               inset.dispatch(cur, cmd);
+                       break;
 
                case LFUN_MOUSE_RELEASE:
-                       if (!status_ && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
-                       else
-                               return lfunMouseRelease(cmd);
-                       return DispatchResult(true, true);
+                       lfunMouseRelease(cur, cmd);
+                       break;
 
                case LFUN_INSET_TOGGLE:
-                       if (inset.text_.toggleInset())
-                               return DispatchResult(true, true);
-                       close();
-                       return DispatchResult(false, FINISHED_RIGHT);
+                       if (inset.text_.toggleInset(cur))
+                               break;
+                       if (status_ == Open) {
+                               setStatus(Inlined);
+                               break;
+                       }
+                       setStatus(Collapsed);
+                       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);
 }
 
 
@@ -349,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);
 }
 
 
@@ -375,15 +399,9 @@ int InsetCollapsable::scroll(bool recursive) const
 }
 
 
-ParagraphList * InsetCollapsable::getParagraphs(int i) const
-{
-       return inset.getParagraphs(i);
-}
-
-
-int InsetCollapsable::numParagraphs() const
+size_t InsetCollapsable::nargs() const
 {
-       return inset.numParagraphs();
+       return inset.nargs();
 }
 
 
@@ -395,7 +413,8 @@ LyXText * InsetCollapsable::getText(int i) const
 
 void InsetCollapsable::open()
 {
-       setStatus(Open);
+       if (status_ == Collapsed)   // ...but not inlined
+               setStatus(Open);
 }
 
 
@@ -405,15 +424,16 @@ void InsetCollapsable::close()
 }
 
 
-void InsetCollapsable::setLabel(string const & l) const
+void InsetCollapsable::setLabel(string const & l)
 {
        label = l;
 }
 
 
-void InsetCollapsable::setStatus(CollapseStatus s)
+void InsetCollapsable::setStatus(CollapseStatus st)
 {
-       status_ = s;
+       status_ = st;
+       setButtonLabel();
 }
 
 
@@ -435,27 +455,19 @@ bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
 }
 
 
-void InsetCollapsable::setLabelFont(LyXFont & f)
-{
-       labelfont_ = f;
-}
-
-
-#if 0
-void InsetCollapsable::setAutoCollapse(bool f)
+void InsetCollapsable::setLabelFont(LyXFont & font)
 {
-       autocollapse = f;
+       labelfont_ = font;
 }
-#endif
 
 
-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);
 }