]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetBranch.cpp
listerrors.lyx : Update a link.
[lyx.git] / src / insets / InsetBranch.cpp
index 61f3353774e3e9f84914d66a78b2b497a0661c0b..6ab2fb79e73e5712a08c3face65b7bafc933eb6c 100644 (file)
@@ -48,6 +48,7 @@ InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
 
 void InsetBranch::write(ostream & os) const
 {
+       os << "Branch ";
        params_.write(os);
        os << '\n';
        InsetCollapsable::write(os);
@@ -61,17 +62,15 @@ void InsetBranch::read(Lexer & lex)
 }
 
 
-docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
+docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
 {
        docstring const status = isBranchSelected() ? 
                _("active") : _("non-active");
        docstring const heading = 
                support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
-       docstring const contents = InsetCollapsable::toolTip(bv, x, y);
-       if (isOpen(bv) || contents.empty())
+       if (isOpen(bv))
                return heading;
-       else
-               return heading + from_ascii("\n") + contents;
+       return toolTipText(heading + from_ascii("\n"));
 }
 
 
@@ -113,26 +112,43 @@ ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
 
 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       switch (cmd.action) {
+       switch (cmd.action()) {
        case LFUN_INSET_MODIFY: {
                InsetBranchParams params;
                InsetBranch::string2params(to_utf8(cmd.argument()), params);
+
+               cur.recordUndoInset(ATOMIC_UNDO, this);
                params_.branch = params.branch;
+               // what we really want here is a TOC update, but that means
+               // a full buffer update
+               cur.forceBufferUpdate();
                break;
        }
        case LFUN_BRANCH_ACTIVATE:
        case LFUN_BRANCH_DEACTIVATE: {
-               // FIXME: I do not like this cast, but have no other idea...
-               Buffer const * buf = buffer().masterBuffer();
-               BranchList const & branchlist = buf->params().branchlist();
-               Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
-               if (!our_branch) {
+               Buffer * buf = const_cast<Buffer *>(buffer().masterBuffer());
+               // is the branch in our master buffer?
+               bool branch_in_master = (buf != &buffer());
+
+               Branch * our_branch = buf->params().branchlist().find(params_.branch);
+               if (branch_in_master && !our_branch) {
                        // child only?
                        our_branch = buffer().params().branchlist().find(params_.branch);
                        if (!our_branch)
                                break;
+                       branch_in_master = false;
+               }
+               bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE);
+               if (our_branch->isSelected() != activate) {
+                       // FIXME If the branch is in the master document, we cannot
+                       // call recordUndo..., becuase the master may be hidden, and
+                       // the code presently assumes that hidden documents can never
+                       // be dirty. See GuiView::closeBufferAll(), for example.
+                       if (!branch_in_master)
+                               buffer().undo().recordUndoFullDocument(cur);
+                       our_branch->setSelected(activate);
+                       cur.forceBufferUpdate();
                }
-               our_branch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
                break;
        }
        case LFUN_INSET_TOGGLE:
@@ -152,7 +168,7 @@ void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
-       switch (cmd.action) {
+       switch (cmd.action()) {
        case LFUN_INSET_MODIFY:
                flag.setEnabled(true);
                break;
@@ -170,6 +186,7 @@ bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
                        flag.setEnabled(true);
                else
                        return InsetCollapsable::getStatus(cur, cmd, flag);     
+               break;
 
        default:
                return InsetCollapsable::getStatus(cur, cmd, flag);
@@ -194,9 +211,10 @@ bool InsetBranch::isBranchSelected() const
 }
 
 
-int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
+void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
 {
-       return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
+       if (isBranchSelected())
+               InsetText::latex(os, runparams);
 }
 
 
@@ -206,11 +224,8 @@ int InsetBranch::plaintext(odocstream & os,
        if (!isBranchSelected())
                return 0;
 
-       os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
-       InsetText::plaintext(os, runparams);
-       os << "\n]";
-
-       return PLAINTEXT_NEWLINE + 1; // one char on a separate line
+       int len = InsetText::plaintext(os, runparams);
+       return len;
 }
 
 
@@ -223,16 +238,27 @@ int InsetBranch::docbook(odocstream & os,
 
 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 {
-       if (isBranchSelected())
-                return InsetText::xhtml(xs, rp);
+       if (isBranchSelected()) {
+               OutputParams newrp = rp;
+               newrp.par_begin = 0;
+               newrp.par_end = text().paragraphs().size();
+               xhtmlParagraphs(text(), buffer(), xs, newrp);
+       }
        return docstring();
 }
 
 
-void InsetBranch::tocString(odocstream & os) const
+void InsetBranch::toString(odocstream & os) const
+{
+       if (isBranchSelected())
+               InsetCollapsable::toString(os);
+}
+
+
+void InsetBranch::forToc(docstring & os, size_t maxlen) const
 {
        if (isBranchSelected())
-               InsetCollapsable::tocString(os);
+               InsetCollapsable::forToc(os, maxlen);
 }
 
 
@@ -243,9 +269,9 @@ void InsetBranch::validate(LaTeXFeatures & features) const
 }
 
 
-docstring InsetBranch::contextMenu(BufferView const &, int, int) const
+string InsetBranch::contextMenuName() const
 {
-       return from_ascii("context-branch");
+       return "context-branch";
 }
 
 
@@ -260,7 +286,6 @@ string InsetBranch::params2string(InsetBranchParams const & params)
 {
        ostringstream data;
        params.write(data);
-       data << '\n';
        return data.str();
 }
 
@@ -275,19 +300,19 @@ void InsetBranch::string2params(string const & in, InsetBranchParams & params)
        Lexer lex;
        lex.setStream(data);
        lex.setContext("InsetBranch::string2params");
-       lex >> "Branch";
        params.read(lex);
 }
 
 
-void InsetBranch::addToToc(DocIterator const & cpit)
+void InsetBranch::addToToc(DocIterator const & cpit) const
 {
        DocIterator pit = cpit;
-       pit.push_back(CursorSlice(*this));
+       pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
 
        Toc & toc = buffer().tocBackend().toc("branch");
-       docstring const str = params_.branch + ": " + text().getPar(0).asString();
-       toc.push_back(TocItem(pit, 0, str));
+       docstring str = params_.branch + ": ";
+       text().forToc(str, TOC_ENTRY_LENGTH);
+       toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
        // Proceed with the rest of the inset.
        InsetCollapsable::addToToc(cpit);
 }
@@ -295,7 +320,7 @@ void InsetBranch::addToToc(DocIterator const & cpit)
 
 void InsetBranchParams::write(ostream & os) const
 {
-       os << "Branch " << to_utf8(branch);
+       os << to_utf8(branch);
 }