]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetbranch.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / insetbranch.C
index d1a400e08efa51b936dcf2309362741e60982f82..d39d69c718adfa5716e679c9fdecbfba235e1e53 100644 (file)
 #include "insetbranch.h"
 
 #include "buffer.h"
-#include "BufferView.h"
 #include "bufferparams.h"
 #include "BranchList.h"
 #include "cursor.h"
 #include "dispatchresult.h"
 #include "funcrequest.h"
+#include "FuncStatus.h"
 #include "gettext.h"
 #include "LColor.h"
 #include "lyxlex.h"
 #include "paragraph.h"
 
-#include "support/std_sstream.h"
+#include <sstream>
+
+
+namespace lyx {
 
 using std::string;
 using std::auto_ptr;
@@ -35,7 +38,7 @@ using std::ostringstream;
 
 void InsetBranch::init()
 {
-       setInsetName("Branch");
+       setInsetName(from_utf8("Branch"));
        setButtonLabel();
 }
 
@@ -61,13 +64,13 @@ InsetBranch::~InsetBranch()
 }
 
 
-auto_ptr<InsetBase> InsetBranch::clone() const
+auto_ptr<InsetBase> InsetBranch::doClone() const
 {
        return auto_ptr<InsetBase>(new InsetBranch(*this));
 }
 
 
-string const InsetBranch::editMessage() const
+docstring const InsetBranch::editMessage() const
 {
        return _("Opened Branch Inset");
 }
@@ -94,12 +97,19 @@ void InsetBranch::setButtonLabel()
        font.decSize();
        font.decSize();
 
-       setLabel("Branch: " + params_.branch);
+       docstring s = _("Branch: ") + params_.branch;
        font.setColor(LColor::foreground);
-       if (!params_.branch.empty())
-               setBackgroundColor(lcolor.getFromLyXName(params_.branch));
-       else
+       if (!params_.branch.empty()) {
+               // FIXME UNICODE
+               LColor_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
+               if (c == LColor::none) {
+                       c = LColor::error;
+                       s = _("Undef: ") + s;
+               }
+               setBackgroundColor(c);
+       } else
                setBackgroundColor(LColor::background);
+       setLabel(isOpen() ? s : getNewLabel(s) );
        setLabelFont(font);
 }
 
@@ -111,12 +121,12 @@ bool InsetBranch::showInsetDialog(BufferView * bv) const
 }
 
 
-void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
+void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
        case LFUN_INSET_MODIFY: {
                InsetBranchParams params;
-               InsetBranchMailer::string2params(cmd.argument, params);
+               InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
                params_.branch = params.branch;
                setButtonLabel();
                break;
@@ -124,7 +134,7 @@ void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_PRESS:
                if (cmd.button() != mouse_button::button3)
-                       InsetCollapsable::priv_dispatch(cur, cmd);
+                       InsetCollapsable::doDispatch(cur, cmd);
                else
                        cur.undispatched();
                break;
@@ -137,49 +147,70 @@ void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                if (cmd.button() == mouse_button::button3 && hitButton(cmd))
                        InsetBranchMailer(*this).showDialog(&cur.bv());
                else
-                       InsetCollapsable::priv_dispatch(cur, cmd);
+                       InsetCollapsable::doDispatch(cur, cmd);
                break;
 
 
        case LFUN_INSET_TOGGLE:
-               // We assume that this lfun is indeed going to be dispatched.
-               cur.dispatched();
-
-               if (cmd.argument == "open")
-                       setStatus(Open);
-               else if (cmd.argument == "close") {
-                       setStatus(Collapsed);
-                       leaveInset(cur, *this);
-
-               // The branch inset specialises its behaviour on "toggle".
-               } else if (cmd.argument == "toggle"
-                          || cmd.argument.empty()) {
-                       BranchList const & branchlist =
-                               cur.bv().buffer()->params().branchlist();
-                       if (isBranchSelected(branchlist)) {
+               if (cmd.argument() == "assign" || cmd.argument().empty()) {
+                       // The branch inset uses "assign".
+                       if (isBranchSelected(cur.buffer())) {
                                if (status() != Open)
-                                       setStatus(Open);
+                                       setStatus(cur, Open);
                                else
                                        cur.undispatched();
                        } else {
-                               if (status() != Collapsed) {
-                                       setStatus(Collapsed);
-                                       leaveInset(cur, *this);
-                               } else
+                               if (status() != Collapsed)
+                                       setStatus(cur, Collapsed);
+                               else
                                        cur.undispatched();
                        }
                }
+               else
+                       InsetCollapsable::doDispatch(cur, cmd);
                break;
 
        default:
-               InsetCollapsable::priv_dispatch(cur, cmd);
+               InsetCollapsable::doDispatch(cur, cmd);
+               break;
+       }
+}
+
+
+bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
+               FuncStatus & flag) const
+{
+       switch (cmd.action) {
+       case LFUN_INSET_MODIFY:
+       case LFUN_INSET_DIALOG_UPDATE:
+               flag.enabled(true);
+               break;
+
+       case LFUN_INSET_TOGGLE:
+               if (cmd.argument() == "open" || cmd.argument() == "close" ||
+                   cmd.argument() == "toggle")
+                       flag.enabled(true);
+               else if (cmd.argument() == "assign"
+                          || cmd.argument().empty()) {
+                       if (isBranchSelected(cur.buffer()))
+                               flag.enabled(status() != Open);
+                       else
+                               flag.enabled(status() != Collapsed);
+               } else
+                       flag.enabled(true);
                break;
+
+       default:
+               return InsetCollapsable::getStatus(cur, cmd, flag);
        }
+       return true;
 }
 
 
-bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
+bool InsetBranch::isBranchSelected(Buffer const & buffer) const
 {
+       Buffer const & realbuffer = *buffer.getMasterBuffer();
+       BranchList const & branchlist = realbuffer.params().branchlist();
        BranchList::const_iterator const end = branchlist.end();
        BranchList::const_iterator it =
                std::find_if(branchlist.begin(), end,
@@ -190,38 +221,37 @@ bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
 }
 
 
-int InsetBranch::latex(Buffer const & buf, ostream & os,
+int InsetBranch::latex(Buffer const & buf, odocstream & os,
                       OutputParams const & runparams) const
 {
-       return isBranchSelected(buf.params().branchlist()) ?
+       return isBranchSelected(buf) ?
                InsetText::latex(buf, os, runparams) : 0;
 }
 
 
-int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
-                         OutputParams const & runparams) const
-{
-       return isBranchSelected(buf.params().branchlist()) ?
-               InsetText::linuxdoc(buf, os, runparams) : 0;
-}
-
-
-int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
+int InsetBranch::docbook(Buffer const & buf, odocstream & os,
                         OutputParams const & runparams) const
 {
-       return isBranchSelected(buf.params().branchlist()) ?
+       return isBranchSelected(buf) ?
                InsetText::docbook(buf, os, runparams) : 0;
 }
 
 
-int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
+int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
                           OutputParams const & runparams) const
 {
-       return isBranchSelected(buf.params().branchlist()) ?
+       return isBranchSelected(buf) ?
                InsetText::plaintext(buf, os, runparams): 0;
 }
 
 
+void InsetBranch::textString(Buffer const & buf, odocstream & os) const
+{
+       if (isBranchSelected(buf))
+               os << paragraphs().begin()->asString(buf, true);
+}
+
+
 void InsetBranch::validate(LaTeXFeatures & features) const
 {
        InsetText::validate(features);
@@ -280,7 +310,7 @@ void InsetBranchMailer::string2params(string const & in,
 
 void InsetBranchParams::write(ostream & os) const
 {
-       os << "Branch " << branch << '\n';
+       os << "Branch " << to_utf8(branch) << '\n';
 }
 
 
@@ -288,3 +318,5 @@ void InsetBranchParams::read(LyXLex & lex)
 {
        lex >> branch;
 }
+
+} // namespace lyx