]> 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 b5115422dcefde213eb9a8fb1d5f955241a34627..d39d69c718adfa5716e679c9fdecbfba235e1e53 100644 (file)
@@ -5,45 +5,48 @@
  *
  * \author Martin Vermeer
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#include "Lsstream.h"
-
 #include "insetbranch.h"
-#include "gettext.h"
-#include "lyxfont.h"
-#include "language.h"
+
 #include "buffer.h"
-#include "BufferView.h"
-#include "LaTeXFeatures.h"
-#include "latexrunparams.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 "lyxtext.h"
-#include "insets/insettext.h"
-#include "support/LOstream.h"
-#include "support/lstrings.h"
-#include "debug.h"
+#include "paragraph.h"
 
-using std::ostream;
+#include <sstream>
+
+
+namespace lyx {
+
+using std::string;
 using std::auto_ptr;
+using std::istringstream;
+using std::ostream;
+using std::ostringstream;
+
 
 void InsetBranch::init()
 {
-       setInsetName("Branch");
+       setInsetName(from_utf8("Branch"));
        setButtonLabel();
 }
 
 
-InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
-       : InsetCollapsable(bp)
+InsetBranch::InsetBranch(BufferParams const & bp,
+                        InsetBranchParams const & params)
+       : InsetCollapsable(bp), params_(params)
 {
-       params_.branch = label;
-       // Hack: stash the list of all allowable branch labels from this
-       // buffer into inset's parm list as a "stowaway":
-       params_.branchlist = bp.branchlist;
        init();
 }
 
@@ -57,36 +60,32 @@ InsetBranch::InsetBranch(InsetBranch const & in)
 
 InsetBranch::~InsetBranch()
 {
-       InsetBranchMailer mailer("branch", *this);
-       mailer.hideDialog();
+       InsetBranchMailer(*this).hideDialog();
 }
 
 
-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");
 }
 
 
-void InsetBranch::write(Buffer const * buf, ostream & os) const
+void InsetBranch::write(Buffer const & buf, ostream & os) const
 {
        params_.write(os);
        InsetCollapsable::write(buf, os);
 }
 
 
-void InsetBranch::read(Buffer const * buf, LyXLex & lex)
+void InsetBranch::read(Buffer const & buf, LyXLex & lex)
 {
-       if (lex.isOK()) {
-               lex.next();
-               params_.branch = lex.getString();
-       }
+       params_.read(lex);
        InsetCollapsable::read(buf, lex);
        setButtonLabel();
 }
@@ -98,180 +97,226 @@ void InsetBranch::setButtonLabel()
        font.decSize();
        font.decSize();
 
-       setLabel(params_.branch);
+       docstring s = _("Branch: ") + params_.branch;
        font.setColor(LColor::foreground);
-       string const color = params_.branchlist.getColor(params_.branch);
-       if (!color.empty()) {
-               setBackgroundColor(lcolor.getFromLyXName(params_.branch));
+       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);
 }
 
 
-void InsetBranch::metrics(MetricsInfo & mi, Dimension & dim) const
-{
-       InsetCollapsable::metrics(mi, dim);
-       dim_ = dim;
-}
-
-
-void InsetBranch::draw(PainterInfo & pi, int x, int y) const
-{
-       InsetCollapsable::draw(pi, x, y);
-}
-
-
 bool InsetBranch::showInsetDialog(BufferView * bv) const
 {
-       InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
+       InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
        return true;
 }
 
 
-dispatch_result InsetBranch::localDispatch(FuncRequest const & cmd)
+void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
-       BufferView * bv = cmd.view();
        switch (cmd.action) {
-       case LFUN_INSET_MODIFY:
-               {
+       case LFUN_INSET_MODIFY: {
                InsetBranchParams params;
-               InsetBranchMailer::string2params(cmd.argument, params);
+               InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
                params_.branch = params.branch;
                setButtonLabel();
-               bv->updateInset();
-               return DISPATCHED;
-               }
-       case LFUN_INSET_EDIT:
-               if (cmd.button() != mouse_button::button3) 
-                       return InsetCollapsable::localDispatch(cmd);
-               return UNDISPATCHED;
+               break;
+       }
+
+       case LFUN_MOUSE_PRESS:
+               if (cmd.button() != mouse_button::button3)
+                       InsetCollapsable::doDispatch(cur, cmd);
+               else
+                       cur.undispatched();
+               break;
+
        case LFUN_INSET_DIALOG_UPDATE:
-               InsetBranchMailer("branch", *this).updateDialog(bv);
-               return DISPATCHED;
+               InsetBranchMailer(*this).updateDialog(&cur.bv());
+               break;
+
        case LFUN_MOUSE_RELEASE:
-               if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
-                   InsetBranchMailer("branch", *this).showDialog(bv);
-                       return DISPATCHED;
+               if (cmd.button() == mouse_button::button3 && hitButton(cmd))
+                       InsetBranchMailer(*this).showDialog(&cur.bv());
+               else
+                       InsetCollapsable::doDispatch(cur, cmd);
+               break;
+
+
+       case LFUN_INSET_TOGGLE:
+               if (cmd.argument() == "assign" || cmd.argument().empty()) {
+                       // The branch inset uses "assign".
+                       if (isBranchSelected(cur.buffer())) {
+                               if (status() != Open)
+                                       setStatus(cur, Open);
+                               else
+                                       cur.undispatched();
+                       } else {
+                               if (status() != Collapsed)
+                                       setStatus(cur, Collapsed);
+                               else
+                                       cur.undispatched();
+                       }
                }
-               // fallthrough:
+               else
+                       InsetCollapsable::doDispatch(cur, cmd);
+               break;
+
        default:
-               return InsetCollapsable::localDispatch(cmd);
+               InsetCollapsable::doDispatch(cur, cmd);
+               break;
        }
 }
 
 
-
-int InsetBranch::latex(Buffer const * buf, ostream & os,
-       LatexRunParams const & runparams) const
+bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
+               FuncStatus & flag) const
 {
-       string const branch_sel = buf->params.branchlist.allSelected();
-       if (branch_sel.find(params_.branch, 0) != string::npos)
-               return inset.latex(buf, os, runparams);
-       return 0;
+       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;
 }
 
 
-int InsetBranch::linuxdoc(Buffer const *, std::ostream &) const
-{ 
-       return 0; 
+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,
+                            BranchNamesEqual(params_.branch));
+       if (it == end)
+               return false;
+       return it->getSelected();
 }
 
 
-int InsetBranch::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
+int InsetBranch::latex(Buffer const & buf, odocstream & os,
+                      OutputParams const & runparams) const
 {
-       // untested - MV
-       string const branch_sel = buf->params.branchlist.allSelected();
-       if (branch_sel.find(params_.branch, 0) != string::npos) 
-               return inset.docbook(buf, os, mixcont);
-       return 0;
+       return isBranchSelected(buf) ?
+               InsetText::latex(buf, os, runparams) : 0;
 }
 
 
-int InsetBranch::ascii(Buffer const * buf, std::ostream & os, int ll) const
+int InsetBranch::docbook(Buffer const & buf, odocstream & os,
+                        OutputParams const & runparams) const
 {
-       string const branch_sel = buf->params.branchlist.allSelected();
-       if (branch_sel.find(params_.branch, 0) != string::npos) {
-               return inset.ascii(buf, os, ll);
-       }
-       return 0;
+       return isBranchSelected(buf) ?
+               InsetText::docbook(buf, os, runparams) : 0;
 }
 
 
-void InsetBranch::validate(LaTeXFeatures & features) const
+int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
+                          OutputParams const & runparams) const
 {
-       inset.validate(features);
+       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);
+}
+
 
-InsetBranchMailer::InsetBranchMailer(string const & name,
-                                               InsetBranch & inset)
-       : name_(name), inset_(inset)
+void InsetBranch::validate(LaTeXFeatures & features) const
 {
+       InsetText::validate(features);
 }
 
 
-string const InsetBranchMailer::inset2string(Buffer const & buf) const
+
+string const InsetBranchMailer::name_("branch");
+
+InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
+       : inset_(inset)
+{}
+
+
+string const InsetBranchMailer::inset2string(Buffer const &) const
 {
-       InsetBranchParams params = inset_.params();
-       params.branchlist = buf.params.branchlist;
-       inset_.setParams(params);
-       return params2string(name_, params);
+       return params2string(inset_.params());
 }
 
 
-string const InsetBranchMailer::params2string(string const & name,
-                               InsetBranchParams const & params)
+string const InsetBranchMailer::params2string(InsetBranchParams const & params)
 {
        ostringstream data;
-       data << name << ' ';
+       data << name_ << ' ';
        params.write(data);
-       // Add all_branches parameter to data:
-       data << params.branchlist.allBranches() << "\n";
-       return STRCONV(data.str());
+       return data.str();
 }
 
 
 void InsetBranchMailer::string2params(string const & in,
-                                    InsetBranchParams & params)
+                                     InsetBranchParams & params)
 {
        params = InsetBranchParams();
-
        if (in.empty())
                return;
 
-       istringstream data(STRCONV(in));
+       istringstream data(in);
        LyXLex lex(0,0);
        lex.setStream(data);
+
+       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 LyXText::readInset
+       string id;
+       lex >> id;
+       if (!lex || id != "Branch")
+               return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
+
        params.read(lex);
-       // Process all_branches here:
-       if (lex.isOK()) {
-               lex.next();
-               params.branchlist.add(lex.getString());
-       }
 }
 
 
 void InsetBranchParams::write(ostream & os) const
 {
-       os << "Branch" << " " << branch << "\n";
+       os << "Branch " << to_utf8(branch) << '\n';
 }
 
 
 void InsetBranchParams::read(LyXLex & lex)
 {
-       if (lex.isOK()) {
-               lex.next();
-               string token = lex.getString();
-       }
-       if (lex.isOK()) {
-               lex.next();
-               string token = lex.getString();
-       }
-       if (lex.isOK()) {
-               lex.next();
-               branch = lex.getString();
-       }
+       lex >> branch;
 }
 
+} // namespace lyx