]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetBranch.cpp
Move isMultiCell() to Cursor, and use it.
[lyx.git] / src / insets / InsetBranch.cpp
index 9d968e7aa93f4d7476cb51f4f635c431876d91cc..3eea5ddefb2032cce8f7cb38b608ce58620b3417 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "Buffer.h"
 #include "BufferParams.h"
+#include "BufferView.h"
 #include "BranchList.h"
 #include "Color.h"
 #include "Counters.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "support/gettext.h"
 #include "Lexer.h"
 #include "OutputParams.h"
 #include "TextClass.h"
+#include "TocBackend.h"
 
-#include <sstream>
+#include "support/debug.h"
+#include "support/gettext.h"
 
-using namespace std;
+#include "frontends/Application.h"
 
-namespace lyx {
+#include <sstream>
 
+using namespace std;
 
-InsetBranch::InsetBranch(BufferParams const & bp,
-                        InsetBranchParams const & params)
-       : InsetCollapsable(bp), params_(params)
-{}
 
+namespace lyx {
 
-InsetBranch::InsetBranch(InsetBranch const & in)
-       : InsetCollapsable(in), params_(in.params_)
+InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
+       : InsetCollapsable(buf), params_(params)
 {}
 
 
 InsetBranch::~InsetBranch()
 {
-       InsetBranchMailer(*this).hideDialog();
-}
-
-
-Inset * InsetBranch::clone() const
-{
-       return new InsetBranch(*this);
+       hideDialogs("branch", this);
 }
 
 
@@ -76,7 +70,7 @@ void InsetBranch::read(Lexer & lex)
 }
 
 
-docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
+docstring InsetBranch::toolTip(BufferView const &, int, int) const
 {
        return _("Branch: ") + params_.branch;
 }
@@ -88,9 +82,8 @@ void InsetBranch::setButtonLabel()
        if (!params_.branch.empty()) {
                // FIXME UNICODE
                ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
-               if (c == Color_none) {
+               if (c == Color_none)
                        s = _("Undef: ") + s;
-               }
        }
        if (decoration() == InsetLayout::Classic)
                setLabel(isOpen() ? s : getNewLabel(s) );
@@ -101,21 +94,20 @@ void InsetBranch::setButtonLabel()
 
 ColorCode InsetBranch::backgroundColor() const
 {
-       if (!params_.branch.empty()) {
-               // FIXME UNICODE
-               ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
-               if (c == Color_none) {
-                       c = Color_error;
-               }
-               return c;
-       } else
+       if (params_.branch.empty())
                return Inset::backgroundColor();
+       // FIXME UNICODE
+       ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
+       if (c == Color_none)
+               c = Color_error;
+       return c;
 }
 
 
 bool InsetBranch::showInsetDialog(BufferView * bv) const
 {
-       InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
+       bv->showDialog("branch", params2string(params()),
+                       const_cast<InsetBranch *>(this));
        return true;
 }
 
@@ -125,7 +117,7 @@ void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
        switch (cmd.action) {
        case LFUN_INSET_MODIFY: {
                InsetBranchParams params;
-               InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
+               InsetBranch::string2params(to_utf8(cmd.argument()), params);
                params_.branch = params.branch;
                setLayout(cur.buffer().params());
                break;
@@ -139,17 +131,9 @@ void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_INSET_DIALOG_UPDATE:
-               InsetBranchMailer(*this).updateDialog(&cur.bv());
-               break;
-
-       case LFUN_MOUSE_RELEASE:
-               if (cmd.button() == mouse_button::button3 && hitButton(cmd))
-                       InsetBranchMailer(*this).showDialog(&cur.bv());
-               else
-                       InsetCollapsable::doDispatch(cur, cmd);
+               cur.bv().updateDialog("branch", params2string(params()));
                break;
 
-
        case LFUN_INSET_TOGGLE:
                if (cmd.argument() == "assign") {
                        // The branch inset uses "assign".
@@ -182,20 +166,20 @@ bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
        switch (cmd.action) {
        case LFUN_INSET_MODIFY:
        case LFUN_INSET_DIALOG_UPDATE:
-               flag.enabled(true);
+               flag.setEnabled(true);
                break;
 
        case LFUN_INSET_TOGGLE:
                if (cmd.argument() == "open" || cmd.argument() == "close" ||
                    cmd.argument() == "toggle")
-                       flag.enabled(true);
+                       flag.setEnabled(true);
                else if (cmd.argument() == "assign" || cmd.argument().empty()) {
                        if (isBranchSelected())
-                               flag.enabled(status() != Open);
+                               flag.setEnabled(status() != Open);
                        else
-                               flag.enabled(status() != Collapsed);
+                               flag.setEnabled(status() != Collapsed);
                } else
-                       flag.enabled(true);
+                       flag.setEnabled(true);
                break;
 
        default:
@@ -209,26 +193,10 @@ bool InsetBranch::isBranchSelected() const
 {
        Buffer const & realbuffer = *buffer().masterBuffer();
        BranchList const & branchlist = realbuffer.params().branchlist();
-       BranchList::const_iterator const end = branchlist.end();
-       BranchList::const_iterator it =
-               find_if(branchlist.begin(), end,
-                            BranchNamesEqual(params_.branch));
-       if (it == end)
+       Branch const * ourBranch = branchlist.find(params_.branch);
+       if (!ourBranch)
                return false;
-       return it->getSelected();
-}
-
-
-void InsetBranch::updateLabels(ParIterator const & it)
-{
-       if (isBranchSelected())
-               InsetCollapsable::updateLabels(it);
-       else {
-               TextClass const & tclass = buffer().params().textClass();
-               Counters savecnt = tclass.counters();
-               InsetCollapsable::updateLabels(it);
-               tclass.counters() = savecnt;
-       }
+       return ourBranch->getSelected();
 }
 
 
@@ -262,7 +230,7 @@ int InsetBranch::docbook(odocstream & os,
 void InsetBranch::textString(odocstream & os) const
 {
        if (isBranchSelected())
-               os << paragraphs().begin()->asString(true);
+               os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
 }
 
 
@@ -279,52 +247,40 @@ bool InsetBranch::isMacroScope() const
 }
 
 
-string const InsetBranchMailer::name_("branch");
-
-InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
-       : inset_(inset)
-{}
-
-
-string const InsetBranchMailer::inset2string(Buffer const &) const
-{
-       return params2string(inset_.params());
-}
-
-
-string const InsetBranchMailer::params2string(InsetBranchParams const & params)
+string InsetBranch::params2string(InsetBranchParams const & params)
 {
        ostringstream data;
-       data << name_ << ' ';
+       data << "branch" << ' ';
        params.write(data);
        return data.str();
 }
 
 
-void InsetBranchMailer::string2params(string const & in,
-                                     InsetBranchParams & params)
+void InsetBranch::string2params(string const & in, InsetBranchParams & params)
 {
        params = InsetBranchParams();
        if (in.empty())
                return;
 
        istringstream data(in);
-       Lexer lex(0,0);
+       Lexer lex;
        lex.setStream(data);
+       lex.setContext("InsetBranch::string2params");
+       lex >> "branch" >> "Branch";
+       params.read(lex);
+}
 
-       string name;
-       lex >> name;
-       if (name != name_)
-               return print_mailer_error("InsetBranchMailer", in, 1, name_);
-
-       // This is part of the inset proper that is usually swallowed
-       // by Text::readInset
-       string id;
-       lex >> id;
-       if (!lex || id != "Branch")
-               return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
 
-       params.read(lex);
+void InsetBranch::addToToc(DocIterator const & cpit)
+{
+       DocIterator pit = cpit;
+       pit.push_back(CursorSlice(*this));
+
+       Toc & toc = buffer().tocBackend().toc("branch");
+       docstring const str = params_.branch + ": " + text().getPar(0).asString();
+       toc.push_back(TocItem(pit, 0, str));
+       // Proceed with the rest of the inset.
+       InsetCollapsable::addToToc(cpit);
 }