2 * \file InsetBranch.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
8 * Full author contact details are available in file CREDITS.
13 #include "InsetBranch.h"
16 #include "BufferParams.h"
17 #include "BranchList.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
25 #include "Paragraph.h"
26 #include "OutputParams.h"
35 using std::istringstream;
37 using std::ostringstream;
40 void InsetBranch::init()
42 setInsetName(from_utf8("Branch"));
47 InsetBranch::InsetBranch(BufferParams const & bp,
48 InsetBranchParams const & params)
49 : InsetCollapsable(bp), params_(params)
55 InsetBranch::InsetBranch(InsetBranch const & in)
56 : InsetCollapsable(in), params_(in.params_)
62 InsetBranch::~InsetBranch()
64 InsetBranchMailer(*this).hideDialog();
68 auto_ptr<InsetBase> InsetBranch::doClone() const
70 return auto_ptr<InsetBase>(new InsetBranch(*this));
74 docstring const InsetBranch::editMessage() const
76 return _("Opened Branch Inset");
80 void InsetBranch::write(Buffer const & buf, ostream & os) const
83 InsetCollapsable::write(buf, os);
87 void InsetBranch::read(Buffer const & buf, Lexer & lex)
90 InsetCollapsable::read(buf, lex);
95 void InsetBranch::setButtonLabel()
97 LyXFont font(LyXFont::ALL_SANE);
101 docstring s = _("Branch: ") + params_.branch;
102 font.setColor(LColor::foreground);
103 if (!params_.branch.empty()) {
105 LColor_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
106 if (c == LColor::none) {
108 s = _("Undef: ") + s;
110 setBackgroundColor(c);
112 setBackgroundColor(LColor::background);
113 setLabel(isOpen() ? s : getNewLabel(s) );
118 bool InsetBranch::showInsetDialog(BufferView * bv) const
120 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
125 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
127 switch (cmd.action) {
128 case LFUN_INSET_MODIFY: {
129 InsetBranchParams params;
130 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
131 params_.branch = params.branch;
136 case LFUN_MOUSE_PRESS:
137 if (cmd.button() != mouse_button::button3)
138 InsetCollapsable::doDispatch(cur, cmd);
143 case LFUN_INSET_DIALOG_UPDATE:
144 InsetBranchMailer(*this).updateDialog(&cur.bv());
147 case LFUN_MOUSE_RELEASE:
148 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
149 InsetBranchMailer(*this).showDialog(&cur.bv());
151 InsetCollapsable::doDispatch(cur, cmd);
155 case LFUN_INSET_TOGGLE:
156 if (cmd.argument() == "assign" || cmd.argument().empty()) {
157 // The branch inset uses "assign".
158 if (isBranchSelected(cur.buffer())) {
159 if (status() != Open)
160 setStatus(cur, Open);
164 if (status() != Collapsed)
165 setStatus(cur, Collapsed);
171 InsetCollapsable::doDispatch(cur, cmd);
175 InsetCollapsable::doDispatch(cur, cmd);
181 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
182 FuncStatus & flag) const
184 switch (cmd.action) {
185 case LFUN_INSET_MODIFY:
186 case LFUN_INSET_DIALOG_UPDATE:
190 case LFUN_INSET_TOGGLE:
191 if (cmd.argument() == "open" || cmd.argument() == "close" ||
192 cmd.argument() == "toggle")
194 else if (cmd.argument() == "assign"
195 || cmd.argument().empty()) {
196 if (isBranchSelected(cur.buffer()))
197 flag.enabled(status() != Open);
199 flag.enabled(status() != Collapsed);
205 return InsetCollapsable::getStatus(cur, cmd, flag);
211 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
213 Buffer const & realbuffer = *buffer.getMasterBuffer();
214 BranchList const & branchlist = realbuffer.params().branchlist();
215 BranchList::const_iterator const end = branchlist.end();
216 BranchList::const_iterator it =
217 std::find_if(branchlist.begin(), end,
218 BranchNamesEqual(params_.branch));
221 return it->getSelected();
225 int InsetBranch::latex(Buffer const & buf, odocstream & os,
226 OutputParams const & runparams) const
228 return isBranchSelected(buf) ?
229 InsetText::latex(buf, os, runparams) : 0;
233 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
234 OutputParams const & runparams) const
236 if (!isBranchSelected(buf))
239 os << '[' << _("branch") << ' ' << params_.branch << ":\n";
240 InsetText::plaintext(buf, os, runparams);
243 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
247 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
248 OutputParams const & runparams) const
250 return isBranchSelected(buf) ?
251 InsetText::docbook(buf, os, runparams) : 0;
255 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
257 if (isBranchSelected(buf))
258 os << paragraphs().begin()->asString(buf, true);
262 void InsetBranch::validate(LaTeXFeatures & features) const
264 InsetText::validate(features);
269 string const InsetBranchMailer::name_("branch");
271 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
276 string const InsetBranchMailer::inset2string(Buffer const &) const
278 return params2string(inset_.params());
282 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
285 data << name_ << ' ';
291 void InsetBranchMailer::string2params(string const & in,
292 InsetBranchParams & params)
294 params = InsetBranchParams();
298 istringstream data(in);
305 return print_mailer_error("InsetBranchMailer", in, 1, name_);
307 // This is part of the inset proper that is usually swallowed
308 // by LyXText::readInset
311 if (!lex || id != "Branch")
312 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
318 void InsetBranchParams::write(ostream & os) const
320 os << "Branch " << to_utf8(branch) << '\n';
324 void InsetBranchParams::read(Lexer & lex)