]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcollapsable.C
more cursor dispatch
[lyx.git] / src / insets / insetcollapsable.C
index 285d8f4ae3e2addebee532090ab4831886502c76..59857a0a1bb1ef63b29663450ebaf7715080ac89 100644 (file)
@@ -39,66 +39,85 @@ 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)
 {
        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";
-       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();
-                       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;
+
+       setButtonLabel();
 }
 
 
@@ -118,7 +137,6 @@ int InsetCollapsable::height_collapsed() const
 
 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << endl;
        if (status_ == Inlined) {
                inset.metrics(mi, dim);
        } else {
@@ -131,7 +149,6 @@ void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
                }
        }
        dim_ = dim;
-       //lyxerr << "InsetCollapsable::metrics:  dim.wid: " << dim.wid << endl;
 }
 
 
@@ -143,20 +160,18 @@ 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;
+               button_dim.y2 = y - aa + dimc.height();
 
                draw_collapsed(pi, x, y);
                if (status_ == Open) {
@@ -180,46 +195,36 @@ bool InsetCollapsable::descendable() const
 }
 
 
-FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
-{
-       FuncRequest cmd1 = cmd;
-       cmd1.y += ascent() - height_collapsed();
-       return cmd1;
-}
-
-
-DispatchResult InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
+DispatchResult
+InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
 {
-       BufferView * bv = cmd.view();
-
        if (cmd.button() == mouse_button::button3) {
                lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
-               if (hitButton(cmd))
-                       showInsetDialog(bv);
+               showInsetDialog(&cur.bv());
                return DispatchResult(true, true);
        }
 
-       if (status_ == Collapsed) {
+       switch (status_) {
+       case Collapsed:
                lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
                setStatus(Open);
-               edit(bv, true);
+               edit(cur, true);
                return DispatchResult(true, true);
-       }
 
-       if (hitButton(cmd)) {
-               if (status_ == Open) {
-                       setStatus(Collapsed);
+       case Open:
+               if (hitButton(cmd)) {
                        lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
+                       setStatus(Collapsed);
                        return DispatchResult(false, FINISHED_RIGHT);
                }
-               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));
-       }
+               return inset.dispatch(cur, cmd);
 
-       lyxerr << "InsetCollapsable::lfunMouseRelease 5" << endl;
+       case Inlined:
+               return inset.dispatch(cur, cmd);
+       }
+       BOOST_ASSERT(false);
+       // shut up compiler
        return DispatchResult(true, true);
 }
 
@@ -275,25 +280,23 @@ string const InsetCollapsable::getNewLabel(string const & l) const
        if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
                la += "...";
        }
-       if (la.empty()) {
-               la = l;
-       }
-       return la;
+       return la.empty() ? l : la;
 }
 
 
-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)
+void InsetCollapsable::edit(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 {
@@ -302,44 +305,47 @@ void InsetCollapsable::edit(BufferView * bv, int x, int y)
                else
                        y += inset.ascent() - height_collapsed();
        }
-       inset.edit(bv, x, y);
-       bv->cursor().push(this);
+       inset.edit(cur, x, y);
 }
 
 
 DispatchResult
-InsetCollapsable::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
+InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest const & 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 (!status_ && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
+                       if (status_ == Inlined)
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && cmd.y > button_dim.y2)
+                               inset.dispatch(cur, cmd);
                        return DispatchResult(true, true);
 
                case LFUN_MOUSE_MOTION:
-                       if (!status_)
-                               inset.dispatch(adjustCommand(cmd));
+                       if (status_ == Inlined)
+                               inset.dispatch(cur, cmd);
+                       else if (status_ == Open && cmd.y > button_dim.y2)
+                               inset.dispatch(cur, cmd);
                        return DispatchResult(true, true);
 
                case LFUN_MOUSE_RELEASE:
-                       if (!status_ && cmd.y > button_dim.y2)
-                               inset.dispatch(adjustCommand(cmd));
-                       else
-                               return lfunMouseRelease(cmd);
-                       return DispatchResult(true, true);
+                       return lfunMouseRelease(cur, cmd);
 
                case LFUN_INSET_TOGGLE:
                        if (inset.text_.toggleInset())
                                return DispatchResult(true, true);
-                       close();
-                       return DispatchResult(false, FINISHED_RIGHT);
+                       if (status_ == Open) {
+                               setStatus(Inlined);
+                               return DispatchResult(true, true);
+                       } else {
+                               setStatus(Collapsed);
+                               return DispatchResult(false, FINISHED_RIGHT);
+                       }
 
                default:
-                       return inset.dispatch(adjustCommand(cmd));
+                       return inset.dispatch(cur, cmd);
        }
-       //lyxerr << "InsetCollapsable::priv_dispatch (end)" << endl;
 }
 
 
@@ -349,11 +355,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,12 +380,6 @@ int InsetCollapsable::scroll(bool recursive) const
 }
 
 
-ParagraphList * InsetCollapsable::getParagraphs(int i) const
-{
-       return inset.getParagraphs(i);
-}
-
-
 int InsetCollapsable::numParagraphs() const
 {
        return inset.numParagraphs();
@@ -395,7 +394,8 @@ LyXText * InsetCollapsable::getText(int i) const
 
 void InsetCollapsable::open()
 {
-       setStatus(Open);
+       if (status_ == Collapsed)   // ...but not inlined
+               setStatus(Open);
 }
 
 
@@ -405,15 +405,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 +436,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);
 }