]> 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 9f57b483dc9db6cfc63f448927bcb45fac979768..0c8a5bef1b166d02abd3712134ca0a32047ec63c 100644 (file)
@@ -35,97 +35,120 @@ using lyx::graphics::PreviewLoader;
 using std::endl;
 using std::string;
 using std::max;
+using std::min;
 using std::ostream;
 
 
-InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
-       : UpdatableInset(), inset(bp), collapsed_(collapsed), 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), collapsed_(in.collapsed_),
-         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 " << (collapsed_ ? "true" : "false") << "\n";
-       inset.writeParagraphData(buf, os);
+       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();
-                       collapsed_ = lex.getBool();
+                       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;
-       dimension_collapsed(dim);
-       if (!collapsed_) {
-               Dimension insetdim;
-               inset.metrics(mi, insetdim);
-               dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
-               dim.wid = max(dim.wid, insetdim.wid);
+       if (status_ == Inlined) {
+               inset.metrics(mi, dim);
+       } else {
+               dimension_collapsed(dim);
+               if (status_ == Open) {
+                       Dimension insetdim;
+                       inset.metrics(mi, insetdim);
+                       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;
 }
 
 
@@ -135,94 +158,85 @@ void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
 }
 
 
-void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
+void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
-       Dimension dim_collapsed;
-       dimension_collapsed(dim_collapsed);
-
-       int const aa  = ascent();
-       button_dim.x1 = 0;
-       button_dim.x2 = dim_collapsed.width();
-       button_dim.y1 = -aa;
-       button_dim.y2 = -aa + dim_collapsed.height();
+       setPosCache(pi, x, y);
 
-       xo_ = x;
-       yo_ = y;
-
-       if (!isOpen()) {
-               draw_collapsed(pi, x, y);
-               return;
-       }
-
-       int old_x = x;
-
-       if (!owner())
-               x += scroll();
-
-       if (inlined) {
+       if (status_ == Inlined) {
                inset.draw(pi, x, y);
        } else {
-               int const bl = y - aa + dim_collapsed.ascent();
-               draw_collapsed(pi, old_x, bl);
-               inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
+               Dimension dimc;
+               dimension_collapsed(dimc);
+               int const aa  = ascent();
+               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();
+                       
+                       if (openinlined_)
+                               inset.draw(pi, x + dimc.width(), y - aa + inset.ascent());
+                       else 
+                               inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
+               }
        }
 }
 
 
-void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
+void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
 {
-       // by default, we are not inlined-drawing
-       draw(pi, x, y, false);
+       inset.drawSelection(pi, x, y);
 }
 
 
 InsetOld::EDITABLE InsetCollapsable::editable() const
 {
-       return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
+       return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
 }
 
 
-FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
+bool InsetCollapsable::descendable() const
 {
-       FuncRequest cmd1 = cmd;
-       cmd1.y = ascent() + cmd.y - height_collapsed() - inset.ascent();
-       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 (collapsed_) {
+       switch (status_) {
+
+       case Collapsed:
                lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
-               collapsed_ = false;
-               edit(bv, true);
-               return DispatchResult(true, true);
-       }
+               setStatus(Open);
+               edit(cur, true);
+               break;
 
-       if (hitButton(cmd)) {
-               if (!collapsed_) {
-                       collapsed_ = true;
+       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;
                }
-               collapsed_ = false;
                lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
-       } else if (!collapsed_ && 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;
+       }
 }
 
 
@@ -254,72 +268,103 @@ 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);
 }
 
 
-void InsetCollapsable::edit(BufferView * bv, bool left)
+string const InsetCollapsable::getNewLabel(string const & l) const
 {
-       lyxerr << "InsetCollapsable: edit left/right" << endl;
-       inset.edit(bv, left);
-       open(bv);
-       bv->cursor().push(this);
+       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);
+       pos_type i = 0;
+       pos_type j = 0;
+       for( ; i < n && j < p_siz; ++j) {
+               if (inset.paragraphs().begin()->isInset(j))
+                       continue;
+               label += inset.paragraphs().begin()->getChar(j);
+               ++i;
+       }
+       if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
+               label += "...";
+       }
+       return label.empty() ? l : label;
 }
 
 
-void InsetCollapsable::edit(BufferView * bv, int x, int y)
+void InsetCollapsable::edit(LCursor & cur, bool left)
 {
-       lyxerr << "InsetCollapsable: edit xy" << endl;
-       if (collapsed_) {
-               collapsed_ = false;
-               // set this only here as it should be recollapsed only if
-               // it was already collapsed!
-               inset.edit(bv, x, y);
-       } else {
-               if (y <= button_dim.y2)
-                       inset.edit(bv, x, 0);
-               else
-                       inset.edit(bv, x, ascent() + y - height_collapsed() + inset.ascent());
+       //lyxerr << "InsetCollapsable: edit left/right" << endl;
+       cur.push(*this);
+       inset.edit(cur, left);
+       open();
+}
+
+
+InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
+{
+       cur.push(*this);
+       //lyxerr << "InsetCollapsable: edit xy" << endl;
+       if (status_ == Collapsed) {
+               setStatus(Open);
+               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;
        }
-       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;
+       //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
+       //      << "  button y: " << button_dim.y2 << endl;
        switch (cmd.action) {
                case LFUN_MOUSE_PRESS:
-                       if (!collapsed_ && 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 (!collapsed_ && 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_RELEASE:
-                       if (!collapsed_ && 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(cur))
+                               break;
+                       if (status_ == Open) {
+                               setStatus(Inlined);
+                               break;
+                       }
+                       setStatus(Collapsed);
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       break;
 
                default:
-                       return inset.dispatch(cmd);
+                       inset.dispatch(cur, cmd);
+                       break;
        }
-       lyxerr << "InsetCollapsable::priv_dispatch (end)" << endl;
 }
 
 
-int InsetCollapsable::insetInInsetY() const
+bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
+       FuncStatus & flag) const
 {
-       return inset.y() - yo_ + inset.insetInInsetY();
+       return inset.getStatus(cur, cmd, flag);
 }
 
 
@@ -329,23 +374,10 @@ void InsetCollapsable::validate(LaTeXFeatures & features) const
 }
 
 
-void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
-{
-       inset.getCursorPos(bv, x , y);
-       y += - ascent() + height_collapsed() + inset.ascent();
-}
-
-
-void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
-                              bool toggleall, bool selectall)
-{
-       inset.setFont(bv, font, toggleall, selectall);
-}
-
-
-void InsetCollapsable::deleteLyXText(BufferView * bv) const
+void InsetCollapsable::getCursorPos(CursorSlice const & cur,
+       int & x, int & y) const
 {
-       inset.deleteLyXText(bv);
+       inset.getCursorPos(cur, x, y);
 }
 
 
@@ -361,21 +393,15 @@ int InsetCollapsable::scroll(bool recursive) const
        int sx = UpdatableInset::scroll(false);
 
        if (recursive)
-               sx += inset.scroll(recursive);
+               sx += inset.scroll(false);
 
        return sx;
 }
 
 
-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();
 }
 
 
@@ -385,33 +411,29 @@ LyXText * InsetCollapsable::getText(int i) const
 }
 
 
-void InsetCollapsable::open(BufferView * bv)
+void InsetCollapsable::open()
 {
-       if (!collapsed_)
-               return;
-
-       collapsed_ = false;
+       if (status_ == Collapsed)   // ...but not inlined
+               setStatus(Open);
 }
 
 
-void InsetCollapsable::close(BufferView * bv) const
+void InsetCollapsable::close()
 {
-       if (collapsed_)
-               return;
-
-       collapsed_ = true;
+       setStatus(Collapsed);
 }
 
 
-void InsetCollapsable::setLabel(string const & l) const
+void InsetCollapsable::setLabel(string const & l)
 {
        label = l;
 }
 
 
-void InsetCollapsable::setCollapsed(bool c) const
+void InsetCollapsable::setStatus(CollapseStatus st)
 {
-       collapsed_ = c;
+       status_ = st;
+       setButtonLabel();
 }
 
 
@@ -433,44 +455,32 @@ bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
 }
 
 
-void InsetCollapsable::fitInsetCursor(BufferView * bv) const
-{
-       inset.fitInsetCursor(bv);
-}
-
-
-void InsetCollapsable::setLabelFont(LyXFont & f)
+void InsetCollapsable::setLabelFont(LyXFont & font)
 {
-       labelfont_ = f;
+       labelfont_ = font;
 }
 
-#if 0
-void InsetCollapsable::setAutoCollapse(bool f)
-{
-       autocollapse = f;
-}
-#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);
 }
 
 
-bool InsetCollapsable::isOpen() const
+Box const & InsetCollapsable::buttonDim() const
 {
-       return !collapsed_;
+       return button_dim;
 }
 
 
-Box const & InsetCollapsable::buttonDim() const
+void InsetCollapsable::setBackgroundColor(LColor_color color)
 {
-       return button_dim;
+       InsetOld::setBackgroundColor(color);
+       inset.setBackgroundColor(color);
 }
-