Hello,

this patch moves all code for 'CollapseStatus' from insetert to insetcollapsable. Insetcollapsable has three states now just like ERTs before. You get into the "Inlined" mode, if you press CTRL-I inside an open inset.

Please have a look at the patch. If you like it, please apply it. There are still a couple of open issues but I would like to address them after I got your OK.

  - lyx2lyx converter for the new file format
    (I will provide it this afternoon, if the patch is applied)
  - move ERT dialog (and mailer) to inset Collapsable (?)
  - move Martin's inlined mode from CharStyle to Collapsable

BTW: Why does insetert need metrics(...) & draw(...)? If they are really needed, then we have to fix insetcollapsable instead.

Kind regards,

Michael

PS: I slowly understand why Andre wants to continue the code cleanup for a while. Everything looks much too complicated. Even this simple patch saves us 154 lines of code.


? collapse-patch
Index: src/insets/ChangeLog
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/ChangeLog,v
retrieving revision 1.947
diff -u -r1.947 ChangeLog
--- src/insets/ChangeLog        2003/12/12 10:20:17     1.947
+++ src/insets/ChangeLog        2003/12/12 11:33:49
@@ -1,3 +1,10 @@
+2003-12-12  Michael Schmitt  <[EMAIL PROTECTED]>
+
+       * insetert.[Ch]:
+       * insetcollapsable.[Ch]: Move all CollapseStatus code from
+       insetert to insetcollapsable; introduce virtual function
+       setButtonLabel and invoke it from collapsable
+
 2003-12-12  Angus Leeming  <[EMAIL PROTECTED]>
 
        * insetinclude.C (draw): cache x,y and so enable the
Index: src/insets/insetcharstyle.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetcharstyle.C,v
retrieving revision 1.8
diff -u -r1.8 insetcharstyle.C
--- src/insets/insetcharstyle.C 2003/12/06 09:31:30     1.8
+++ src/insets/insetcharstyle.C 2003/12/12 11:33:50
@@ -105,7 +105,7 @@
        xo_ = x;
        yo_ = y;
 
-       status_ = Inlined;
+       // FIXME: setStatus(Inlined); this is not a const operation
        inset.setDrawFrame(InsetText::NEVER);
        inset.draw(pi, x, y);
 
Index: src/insets/insetcollapsable.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetcollapsable.C,v
retrieving revision 1.222
diff -u -r1.222 insetcollapsable.C
--- src/insets/insetcollapsable.C       2003/12/10 09:45:30     1.222
+++ src/insets/insetcollapsable.C       2003/12/12 11:33:51
@@ -39,55 +39,93 @@
 using std::ostream;
 
 
-InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
-       : UpdatableInset(), inset(bp), status_(collapsed ? Collapsed : Open),
+InsetCollapsable::InsetCollapsable(BufferParams const & bp, CollapseStatus status)
+       : UpdatableInset(), inset(bp), status_(status),
          label("Label")
-#if 0
-       ,autocollapse(false)
-#endif
 {
        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
 {
        inset.setOwner(this);
+
+       setButtonLabel();
 }
 
 
 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
 {
-       os << "collapsed " << (status_ == Collapsed ? "true" : "false") << "\n";
-       inset.text_.write(buf, os);
+       string st;
+
+       switch (status_) {
+       case Open:
+               st = "open";
+               break;
+       case Collapsed:
+               st = "collapsed";
+               break;
+       case Inlined:
+               st = "inlined";
+               break;
+       }
+       os << "status "<< st << "\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) {
+               if (isOpen())
+                       status_ = Open;
+               else
+                       status_ = Collapsed;
+       }
+
+       setButtonLabel();
 }
 
 
@@ -183,32 +221,31 @@
 
        if (cmd.button() == mouse_button::button3) {
                lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
-               if (hitButton(cmd))
-                       showInsetDialog(bv);
+               showInsetDialog(bv);
                return DispatchResult(true, true);
        }
 
-       if (status_ == Collapsed) {
+       switch (status_) {
+       case Collapsed:
                lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
                setStatus(Open);
                edit(bv, 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));
+               } else {
+                       lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
+                       return inset.dispatch(adjustCommand(cmd));
+               }       
+
+       case Inlined:
+               return inset.dispatch(cmd);
        }
 
-       lyxerr << "InsetCollapsable::lfunMouseRelease 5" << endl;
        return DispatchResult(true, true);
 }
 
@@ -275,7 +312,7 @@
 {
        lyxerr << "InsetCollapsable: edit left/right" << endl;
        inset.edit(bv, left);
-       setStatus(Open);
+       open();
        bv->cursor().push(this);
 }
 
@@ -303,27 +340,32 @@
        //      << "  button y: " << button_dim.y2 << endl;
        switch (cmd.action) {
                case LFUN_MOUSE_PRESS:
-                       if (!status_ && cmd.y > button_dim.y2)
+                       if (status_ == Inlined)
+                               inset.dispatch(cmd);
+                       else if (status_ == Open && cmd.y > button_dim.y2)
                                inset.dispatch(adjustCommand(cmd));
                        return DispatchResult(true, true);
 
                case LFUN_MOUSE_MOTION:
-                       if (!status_)
+                       if (status_ == Inlined)
+                               inset.dispatch(cmd);
+                       else if (status_ == Open && cmd.y > button_dim.y2)
                                inset.dispatch(adjustCommand(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(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));
@@ -378,7 +420,8 @@
 
 void InsetCollapsable::open()
 {
-       setStatus(Open);
+       if (status_ == Collapsed)   // ...but not inlined
+               setStatus(Open);
 }
 
 
@@ -388,7 +431,7 @@
 }
 
 
-void InsetCollapsable::setLabel(string const & l) const
+void InsetCollapsable::setLabel(string const & l)
 {
        label = l;
 }
@@ -397,6 +440,7 @@
 void InsetCollapsable::setStatus(CollapseStatus s)
 {
        status_ = s;
+       setButtonLabel();
 }
 
 
@@ -422,14 +466,6 @@
 {
        labelfont_ = f;
 }
-
-
-#if 0
-void InsetCollapsable::setAutoCollapse(bool f)
-{
-       autocollapse = f;
-}
-#endif
 
 
 void InsetCollapsable::scroll(BufferView * bv, float sx) const
Index: src/insets/insetcollapsable.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetcollapsable.h,v
retrieving revision 1.157
diff -u -r1.157 insetcollapsable.h
--- src/insets/insetcollapsable.h       2003/12/10 09:45:30     1.157
+++ src/insets/insetcollapsable.h       2003/12/12 11:33:51
@@ -40,9 +40,9 @@
                Collapsed,
                Inlined
        };
-       /// inset is initially collapsed if bool = true
-       InsetCollapsable(BufferParams const &, bool collapsed = false);
        ///
+       InsetCollapsable(BufferParams const &, CollapseStatus status = Open);
+       ///
        InsetCollapsable(InsetCollapsable const & in);
        ///
        void read(Buffer const &, LyXLex &);
@@ -81,13 +81,11 @@
        /// get the screen x,y of the cursor
        void getCursorPos(int & x, int & y) const;
        ///
-       void setLabel(std::string const & l) const;
+       void setLabel(std::string const & l);
        ///
+       virtual void setButtonLabel() {};
+       ///
        void setLabelFont(LyXFont & f);
-#if 0
-       ///
-       void setAutoCollapse(bool f);
-#endif
        /// Appends \c list with all labels found within this inset.
        void getLabelList(Buffer const &, std::vector<std::string> & list) const;
        ///
@@ -117,7 +115,7 @@
        ///
        void setBackgroundColor(LColor_color);
        ///
-       virtual void setStatus(CollapseStatus st);
+       void setStatus(CollapseStatus st);
 
 protected:
        ///
@@ -148,9 +146,10 @@
 public:
        ///
        mutable InsetText inset;
-protected:
+private:
        ///
        mutable CollapseStatus status_;
+protected:
        ///
        LyXFont labelfont_;
        ///
@@ -161,10 +160,6 @@
        mutable int topbaseline;
        ///
        mutable std::string label;
-#if 0
-       ///
-       bool autocollapse;
-#endif
 };
 
 #endif
Index: src/insets/insetert.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetert.C,v
retrieving revision 1.184
diff -u -r1.184 insetert.C
--- src/insets/insetert.C       2003/12/11 15:23:15     1.184
+++ src/insets/insetert.C       2003/12/12 11:33:51
@@ -57,10 +57,9 @@
 }
 
 
-InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
-       : InsetCollapsable(bp, collapsed)
+InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
+       : InsetCollapsable(bp, status)
 {
-       status_ = collapsed ? Collapsed : Open;
        init();
 }
 
@@ -79,11 +78,9 @@
 
 
 InsetERT::InsetERT(BufferParams const & bp,
-                  Language const * l, string const & contents, bool collapsed)
-       : InsetCollapsable(bp, collapsed)
+                  Language const * l, string const & contents, CollapseStatus status)
+       : InsetCollapsable(bp, status)
 {
-       status_ = collapsed ? Collapsed : Open;
-
        LyXFont font(LyXFont::ALL_INHERIT, l);
        string::const_iterator cit = contents.begin();
        string::const_iterator end = contents.end();
@@ -103,92 +100,10 @@
 }
 
 
-void InsetERT::read(Buffer const & buf, LyXLex & lex)
-{
-       bool token_found = false;
-       if (lex.isOK()) {
-               lex.next();
-               string const token = lex.getString();
-               if (token == "status") {
-                       lex.next();
-                       string const tmp_token = lex.getString();
-
-                       if (tmp_token == "Inlined") {
-                               status_ = Inlined;
-                       } else if (tmp_token == "Collapsed") {
-                               status_ = Collapsed;
-                       } else {
-                               // leave this as default!
-                               status_ = Open;
-                       }
-
-                       token_found = true;
-               } else {
-                       lyxerr << "InsetERT::Read: Missing 'status'-tag!"
-                                  << endl;
-                       // take countermeasures
-                       lex.pushToken(token);
-               }
-       }
-       inset.read(buf, lex);
-
-       if (!token_found) {
-               if (isOpen())
-                       status_ = Open;
-               else
-                       status_ = Collapsed;
-       }
-       setButtonLabel();
-}
-
-
 void InsetERT::write(Buffer const & buf, ostream & os) const
 {
-       string st;
-
-       switch (status_) {
-       case Open:
-               st = "Open";
-               break;
-       case Collapsed:
-               st = "Collapsed";
-               break;
-       case Inlined:
-               st = "Inlined";
-               break;
-       }
-
-       os << getInsetName() << "\n" << "status "<< st << "\n";
-
-       //inset.writeParagraphData(buf, os);
-       string const layout(buf.params().getLyXTextClass().defaultLayoutName());
-       ParagraphList::iterator par = inset.paragraphs().begin();
-       ParagraphList::iterator end = inset.paragraphs().end();
-       for (; par != end; ++par) {
-               os << "\n\\begin_layout " << layout << "\n";
-               pos_type siz = par->size();
-               for (pos_type i = 0; i < siz; ++i) {
-                       Paragraph::value_type c = par->getChar(i);
-                       switch (c) {
-                       case Paragraph::META_INSET:
-                               if (par->getInset(i)->lyxCode() != 
InsetOld::NEWLINE_CODE) {
-                                       lyxerr << "Element is not allowed in insertERT"
-                                              << endl;
-                               } else {
-                                       par->getInset(i)->write(buf, os);
-                               }
-                               break;
-
-                       case '\\':
-                               os << "\n\\backslash \n";
-                               break;
-                       default:
-                               os << c;
-                               break;
-                       }
-               }
-               os << "\n\\end_layout\n";
-       }
+       os << "ERT" << "\n";
+       InsetCollapsable::write(buf, os);
 }
 
 
@@ -198,70 +113,6 @@
 }
 
 
-void InsetERT::updateStatus(bool swap) const
-{
-       if (status_ != Inlined) {
-               if (isOpen())
-                       status_ = swap ? Collapsed : Open;
-               else
-                       status_ = swap ? Open : Collapsed;
-               setButtonLabel();
-       }
-}
-
-
-void InsetERT::lfunMousePress(FuncRequest const & cmd)
-{
-       if (status_ == Inlined)
-               inset.dispatch(cmd);
-       else {
-               idx_type idx = 0;
-               pos_type pos = 0;
-               InsetCollapsable::priv_dispatch(cmd, idx, pos);
-       }
-}
-
-
-bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
-{
-       BufferView * bv = cmd.view();
-
-       if (cmd.button() == mouse_button::button3) {
-               showInsetDialog(bv);
-               return true;
-       }
-
-       if (status_ != Inlined && hitButton(cmd)) {
-               updateStatus(true);
-       } else {
-               FuncRequest cmd1 = cmd;
-#warning metrics?
-               cmd1.y = ascent() + cmd.y - inset.ascent();
-
-               // inlined is special - the text appears above
-               if (status_ == Inlined)
-                       inset.dispatch(cmd1);
-               else if (isOpen() && cmd.y > buttonDim().y2) {
-                       cmd1.y -= height_collapsed();
-                       inset.dispatch(cmd1);
-               }
-       }
-       return false;
-}
-
-
-void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
-{
-       if (status_ == Inlined)
-               inset.dispatch(cmd);
-       else {
-               idx_type idx = 0;
-               pos_type pos = 0;
-               InsetCollapsable::priv_dispatch(cmd, idx, pos);
-       }
-}
-
-
 int InsetERT::latex(Buffer const &, ostream & os,
                    OutputParams const &) const
 {
@@ -357,37 +208,19 @@
 }
 
 
-void InsetERT::edit(BufferView * bv, bool left)
-{
-       if (status_ == Inlined)
-               inset.edit(bv, left);
-       else
-               InsetCollapsable::edit(bv, left);
-       updateStatus();
-}
-
-
 DispatchResult
 InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
 {
        switch (cmd.action) {
 
-       case LFUN_MOUSE_PRESS:
-               lfunMousePress(cmd);
-               return DispatchResult(true, true);
-
-       case LFUN_MOUSE_MOTION:
-               lfunMouseMotion(cmd);
-               return DispatchResult(true, true);
-
-       case LFUN_MOUSE_RELEASE:
-               lfunMouseRelease(cmd);
-               return DispatchResult(true, true);
-
        case LFUN_INSET_MODIFY:
-               InsetERTMailer::string2params(cmd.argument, status_);
-               setButtonLabel();
-               return DispatchResult(true, true);
+                {
+                       InsetCollapsable::CollapseStatus st;
+
+                       InsetERTMailer::string2params(cmd.argument, st);
+                       setStatus(st);
+                       return DispatchResult(true, true);
+               }
 
        case LFUN_LAYOUT:
        case LFUN_BOLD:
@@ -412,9 +245,9 @@
 }
 
 
-void InsetERT::setButtonLabel() const
+void InsetERT::setButtonLabel()
 {
-       setLabel(status_ == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
+       setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
 }
 
 
@@ -440,13 +273,6 @@
        getDrawFont(pi.base.font);
        InsetCollapsable::draw(pi, x, y);
        pi.base.font = tmpfont;
-}
-
-
-void InsetERT::setStatus(CollapseStatus st)
-{
-       status_ = st;
-       setButtonLabel();
 }
 
 
Index: src/insets/insetert.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetert.h,v
retrieving revision 1.101
diff -u -r1.101 insetert.h
--- src/insets/insetert.h       2003/12/10 09:45:30     1.101
+++ src/insets/insetert.h       2003/12/12 11:33:51
@@ -30,21 +30,19 @@
 class InsetERT : public InsetCollapsable {
 public:
        ///
-       InsetERT(BufferParams const &, bool collapsed = false);
+       InsetERT(BufferParams const &, CollapseStatus status = Open);
        ///
        InsetERT(InsetERT const &);
        ///
        virtual std::auto_ptr<InsetBase> clone() const;
        ///
        InsetERT(BufferParams const &,
-                Language const *, std::string const & contents, bool collapsed);
+                Language const *, std::string const & contents, CollapseStatus 
status);
        ///
        ~InsetERT();
        ///
        InsetOld::Code lyxCode() const { return InsetOld::ERT_CODE; }
        ///
-       void read(Buffer const & buf, LyXLex & lex);
-       ///
        void write(Buffer const & buf, std::ostream & os) const;
        ///
        std::string const editMessage() const;
@@ -74,8 +72,6 @@
        void getDrawFont(LyXFont &) const;
        ///
        bool forceDefaultParagraphs(InsetOld const *) const { return true; }
-       ///
-       void setStatus(CollapseStatus st);
 protected:
        ///
        virtual
@@ -83,23 +79,9 @@
        priv_dispatch(FuncRequest const &, idx_type &, pos_type &);
 private:
        ///
-       void lfunMousePress(FuncRequest const &);
-       ///
-       // the bool return is used to see if we opened a dialog so that we can
-       // check this from an outer inset and open the dialog of the outer inset
-       // if that one has one!
-       ///
-       bool lfunMouseRelease(FuncRequest const &);
-       ///
-       void lfunMouseMotion(FuncRequest const &);
-       ///
        void init();
-       ///
-       void setButtonLabel() const;
-       /// update status on button
-       void updateStatus(bool = false) const;
        ///
-       void edit(BufferView * bv, bool left);
+       void setButtonLabel();
        ///
        bool allowSpellCheck() const { return false; }
 };
Index: src/insets/insetoptarg.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetoptarg.C,v
retrieving revision 1.21
diff -u -r1.21 insetoptarg.C
--- src/insets/insetoptarg.C    2003/11/05 12:06:16     1.21
+++ src/insets/insetoptarg.C    2003/12/12 11:33:51
@@ -24,7 +24,7 @@
 
 
 InsetOptArg::InsetOptArg(BufferParams const & ins)
-       : InsetCollapsable(ins, true)
+       : InsetCollapsable(ins, Collapsed)
 {
        LyXFont font(LyXFont::ALL_SANE);
        font.setColor(LColor::collapsable);

Reply via email to