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 "BufferView.h"
17 #include "bufferparams.h"
18 #include "BranchList.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
25 #include "paragraph.h"
27 #include "support/std_sstream.h"
31 using std::istringstream;
33 using std::ostringstream;
36 void InsetBranch::init()
38 setInsetName("Branch");
43 InsetBranch::InsetBranch(BufferParams const & bp,
44 InsetBranchParams const & params)
45 : InsetCollapsable(bp), params_(params)
51 InsetBranch::InsetBranch(InsetBranch const & in)
52 : InsetCollapsable(in), params_(in.params_)
58 InsetBranch::~InsetBranch()
60 InsetBranchMailer(*this).hideDialog();
64 auto_ptr<InsetBase> InsetBranch::clone() const
66 return auto_ptr<InsetBase>(new InsetBranch(*this));
70 string const InsetBranch::editMessage() const
72 return _("Opened Branch Inset");
76 void InsetBranch::write(Buffer const & buf, ostream & os) const
79 InsetCollapsable::write(buf, os);
83 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
86 InsetCollapsable::read(buf, lex);
91 void InsetBranch::setButtonLabel()
93 LyXFont font(LyXFont::ALL_SANE);
97 setLabel("Branch: " + params_.branch);
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);
153 // The branch inset specialises its behaviour on "toggle".
154 else if (cmd.argument == "toggle"
155 || cmd.argument.empty()) {
156 BranchList const & branchlist =
157 cur.bv().buffer()->params().branchlist();
158 if (isBranchSelected(branchlist)) {
159 if (status() != Open)
164 if (status() != Collapsed)
165 setStatus(Collapsed);
173 InsetCollapsable::priv_dispatch(cur, cmd);
179 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
181 BranchList::const_iterator const end = branchlist.end();
182 BranchList::const_iterator it =
183 std::find_if(branchlist.begin(), end,
184 BranchNamesEqual(params_.branch));
187 return it->getSelected();
191 int InsetBranch::latex(Buffer const & buf, ostream & os,
192 OutputParams const & runparams) const
194 return isBranchSelected(buf.params().branchlist()) ?
195 InsetText::latex(buf, os, runparams) : 0;
199 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
200 OutputParams const & runparams) const
202 return isBranchSelected(buf.params().branchlist()) ?
203 InsetText::linuxdoc(buf, os, runparams) : 0;
207 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
208 OutputParams const & runparams) const
210 return isBranchSelected(buf.params().branchlist()) ?
211 InsetText::docbook(buf, os, runparams) : 0;
215 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
216 OutputParams const & runparams) const
218 return isBranchSelected(buf.params().branchlist()) ?
219 InsetText::plaintext(buf, os, runparams): 0;
223 void InsetBranch::validate(LaTeXFeatures & features) const
225 InsetText::validate(features);
230 string const InsetBranchMailer::name_("branch");
232 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
237 string const InsetBranchMailer::inset2string(Buffer const &) const
239 return params2string(inset_.params());
243 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
246 data << name_ << ' ';
252 void InsetBranchMailer::string2params(string const & in,
253 InsetBranchParams & params)
255 params = InsetBranchParams();
259 istringstream data(in);
266 return print_mailer_error("InsetBranchMailer", in, 1, name_);
268 // This is part of the inset proper that is usually swallowed
269 // by LyXText::readInset
272 if (!lex || id != "Branch")
273 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
279 void InsetBranchParams::write(ostream & os) const
281 os << "Branch " << branch << '\n';
285 void InsetBranchParams::read(LyXLex & lex)