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"
24 #include "paragraph.h"
26 #include "support/std_sstream.h"
30 using std::istringstream;
32 using std::ostringstream;
35 void InsetBranch::init()
37 setInsetName("Branch");
42 InsetBranch::InsetBranch(BufferParams const & bp,
43 InsetBranchParams const & params)
44 : InsetCollapsable(bp), params_(params)
50 InsetBranch::InsetBranch(InsetBranch const & in)
51 : InsetCollapsable(in), params_(in.params_)
57 InsetBranch::~InsetBranch()
59 InsetBranchMailer(*this).hideDialog();
63 auto_ptr<InsetBase> InsetBranch::clone() const
65 return auto_ptr<InsetBase>(new InsetBranch(*this));
69 string const InsetBranch::editMessage() const
71 return _("Opened Branch Inset");
75 void InsetBranch::write(Buffer const & buf, ostream & os) const
78 InsetCollapsable::write(buf, os);
82 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
85 InsetCollapsable::read(buf, lex);
90 void InsetBranch::setButtonLabel()
92 LyXFont font(LyXFont::ALL_SANE);
96 string s = "Branch: " + params_.branch;
97 setLabel(isOpen() ? s : getNewLabel(s) );
98 font.setColor(LColor::foreground);
99 if (!params_.branch.empty())
100 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
102 setBackgroundColor(LColor::background);
107 bool InsetBranch::showInsetDialog(BufferView * bv) const
109 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
114 void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
116 switch (cmd.action) {
117 case LFUN_INSET_MODIFY: {
118 InsetBranchParams params;
119 InsetBranchMailer::string2params(cmd.argument, params);
120 params_.branch = params.branch;
125 case LFUN_MOUSE_PRESS:
126 if (cmd.button() != mouse_button::button3)
127 InsetCollapsable::priv_dispatch(cur, cmd);
132 case LFUN_INSET_DIALOG_UPDATE:
133 InsetBranchMailer(*this).updateDialog(&cur.bv());
136 case LFUN_MOUSE_RELEASE:
137 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
138 InsetBranchMailer(*this).showDialog(&cur.bv());
140 InsetCollapsable::priv_dispatch(cur, cmd);
144 case LFUN_INSET_TOGGLE:
145 // We assume that this lfun is indeed going to be dispatched.
148 if (cmd.argument == "open")
150 else if (cmd.argument == "close") {
151 setStatus(Collapsed);
152 leaveInset(cur, *this);
153 } else if (cmd.argument == "toggle") {
155 setStatus(Collapsed);
156 leaveInset(cur, *this);
160 // The branch inset uses "assign".
161 } else if (cmd.argument == "assign"
162 || cmd.argument.empty()) {
163 BranchList const & branchlist =
164 cur.buffer().params().branchlist();
165 if (isBranchSelected(branchlist)) {
166 if (status() != Open)
171 if (status() != Collapsed) {
172 setStatus(Collapsed);
173 leaveInset(cur, *this);
181 InsetCollapsable::priv_dispatch(cur, cmd);
187 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
189 BranchList::const_iterator const end = branchlist.end();
190 BranchList::const_iterator it =
191 std::find_if(branchlist.begin(), end,
192 BranchNamesEqual(params_.branch));
195 return it->getSelected();
199 int InsetBranch::latex(Buffer const & buf, ostream & os,
200 OutputParams const & runparams) const
202 return isBranchSelected(buf.params().branchlist()) ?
203 InsetText::latex(buf, os, runparams) : 0;
207 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
208 OutputParams const & runparams) const
210 return isBranchSelected(buf.params().branchlist()) ?
211 InsetText::linuxdoc(buf, os, runparams) : 0;
215 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
216 OutputParams const & runparams) const
218 return isBranchSelected(buf.params().branchlist()) ?
219 InsetText::docbook(buf, os, runparams) : 0;
223 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
224 OutputParams const & runparams) const
226 return isBranchSelected(buf.params().branchlist()) ?
227 InsetText::plaintext(buf, os, runparams): 0;
231 void InsetBranch::validate(LaTeXFeatures & features) const
233 InsetText::validate(features);
238 string const InsetBranchMailer::name_("branch");
240 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
245 string const InsetBranchMailer::inset2string(Buffer const &) const
247 return params2string(inset_.params());
251 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
254 data << name_ << ' ';
260 void InsetBranchMailer::string2params(string const & in,
261 InsetBranchParams & params)
263 params = InsetBranchParams();
267 istringstream data(in);
274 return print_mailer_error("InsetBranchMailer", in, 1, name_);
276 // This is part of the inset proper that is usually swallowed
277 // by LyXText::readInset
280 if (!lex || id != "Branch")
281 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
287 void InsetBranchParams::write(ostream & os) const
289 os << "Branch " << branch << '\n';
293 void InsetBranchParams::read(LyXLex & lex)