]> git.lyx.org Git - features.git/commitdiff
Michael Schmitt's collapse status patch
authorAndré Pönitz <poenitz@gmx.net>
Fri, 12 Dec 2003 14:02:14 +0000 (14:02 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Fri, 12 Dec 2003 14:02:14 +0000 (14:02 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8241 a592a061-630c-0410-9148-cb99ea01b6c8

src/insets/ChangeLog
src/insets/insetcharstyle.C
src/insets/insetcollapsable.C
src/insets/insetcollapsable.h
src/insets/insetert.C
src/insets/insetert.h
src/insets/insetoptarg.C

index 925466afc8a4d56c8f6dc4ca10b25995ae35c95b..b74da170610fa5512f12fbad087daf5e22cb3407 100644 (file)
@@ -1,3 +1,10 @@
+2003-12-12  Michael Schmitt  <michael.schmitt@teststep.org>
+
+       * 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  <leeming@lyx.org>
 
        * insetinclude.C (draw): cache x,y and so enable the
index afea5713240b900977092ea2aa318b71344a5610..8de473e8e5eb002635f610d62f602b1eb71bdfbe 100644 (file)
@@ -105,7 +105,7 @@ void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
        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 49d4ff405261dfa7665b5ddcbcee80061ac0275d..702c0e7651f332e5bea391d1af460ef2eae75f43 100644 (file)
@@ -39,55 +39,91 @@ using std::min;
 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";
+       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 +219,31 @@ DispatchResult InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
 
        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);
 }
 
@@ -264,10 +299,7 @@ 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;
 }
 
 
@@ -275,7 +307,7 @@ void InsetCollapsable::edit(BufferView * bv, bool left)
 {
        lyxerr << "InsetCollapsable: edit left/right" << endl;
        inset.edit(bv, left);
-       setStatus(Open);
+       open();
        bv->cursor().push(this);
 }
 
@@ -303,26 +335,29 @@ InsetCollapsable::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
        //      << "  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();
+                       if (status_ == Open)
+                               setStatus(Inlined);
+                               return DispatchResult(true, true);
+                       setStatus(Collapsed);
                        return DispatchResult(false, FINISHED_RIGHT);
 
                default:
@@ -378,7 +413,8 @@ LyXText * InsetCollapsable::getText(int i) const
 
 void InsetCollapsable::open()
 {
-       setStatus(Open);
+       if (status_ == Collapsed)   // ...but not inlined
+               setStatus(Open);
 }
 
 
@@ -388,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();
 }
 
 
@@ -418,18 +455,10 @@ 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
index 9d4ebbc1d0e86ad709daf08913dc6b7f2d456a8c..4cbecbed74f9b807f480520b4c48f0768825aaeb 100644 (file)
@@ -40,8 +40,8 @@ public:
                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);
        ///
@@ -81,13 +81,11 @@ public:
        /// 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 @@ public:
        ///
        void setBackgroundColor(LColor_color);
        ///
-       virtual void setStatus(CollapseStatus st);
+       void setStatus(CollapseStatus st);
 
 protected:
        ///
@@ -148,9 +146,10 @@ private:
 public:
        ///
        mutable InsetText inset;
-protected:
+private:
        ///
        mutable CollapseStatus status_;
+protected:
        ///
        LyXFont labelfont_;
        ///
@@ -161,10 +160,6 @@ protected:
        mutable int topbaseline;
        ///
        mutable std::string label;
-#if 0
-       ///
-       bool autocollapse;
-#endif
 };
 
 #endif
index fbf577bbac3df9663dac4e8c1bcff05cd8abe798..e81ec4795f7ec1e08aa23a4d7d1d5ff4480dce0a 100644 (file)
@@ -57,10 +57,9 @@ void InsetERT::init()
 }
 
 
-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 @@ auto_ptr<InsetBase> InsetERT::clone() const
 
 
 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 @@ InsetERT::~InsetERT()
 }
 
 
-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 @@ string const InsetERT::editMessage() const
 }
 
 
-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 @@ int InsetERT::docbook(Buffer const &, ostream & os,
 }
 
 
-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 @@ InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
 }
 
 
-void InsetERT::setButtonLabel() const
+void InsetERT::setButtonLabel()
 {
-       setLabel(status_ == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
+       setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
 }
 
 
@@ -443,13 +276,6 @@ void InsetERT::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-void InsetERT::setStatus(CollapseStatus st)
-{
-       status_ = st;
-       setButtonLabel();
-}
-
-
 bool InsetERT::showInsetDialog(BufferView * bv) const
 {
        InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
index 3fb04dd44f3191b6aa49466fc3db869d4f17268a..ef23eb1f6cf7f782ad7017e84dfb5a72601a4787 100644 (file)
@@ -30,21 +30,19 @@ class Language;
 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,32 +72,16 @@ public:
        void getDrawFont(LyXFont &) const;
        ///
        bool forceDefaultParagraphs(InsetOld const *) const { return true; }
-       ///
-       void setStatus(CollapseStatus st);
 protected:
        ///
        virtual
        DispatchResult
        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 55a8f2b380237299a65c1dff506d77ff40776a64..9b5e42b441d11cb6839e2a7f59c16c008d9c4b82 100644 (file)
@@ -24,7 +24,7 @@ using std::ostream;
 
 
 InsetOptArg::InsetOptArg(BufferParams const & ins)
-       : InsetCollapsable(ins, true)
+       : InsetCollapsable(ins, Collapsed)
 {
        LyXFont font(LyXFont::ALL_SANE);
        font.setColor(LColor::collapsable);