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"
18 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
24 #include "paragraph.h"
26 #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);
115 InsetBranch::priv_dispatch(FuncRequest const & cmd,
116 idx_type & idx, pos_type & pos)
118 BufferView * bv = cmd.view();
119 switch (cmd.action) {
120 case LFUN_INSET_MODIFY: {
121 InsetBranchParams params;
122 InsetBranchMailer::string2params(cmd.argument, params);
123 params_.branch = params.branch;
125 return DispatchResult(true, true);
128 case LFUN_MOUSE_PRESS:
129 if (cmd.button() != mouse_button::button3)
130 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
131 return DispatchResult(false);
133 case LFUN_INSET_DIALOG_UPDATE:
134 InsetBranchMailer(*this).updateDialog(bv);
135 return DispatchResult(true);
137 case LFUN_MOUSE_RELEASE:
138 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
139 InsetBranchMailer(*this).showDialog(bv);
140 return DispatchResult(true);
142 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
145 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
153 SameBranch(string const & branch_name) : bn(branch_name) {}
154 bool operator()(Branch const & branch) const
155 { return bn == branch.getBranch(); }
163 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
165 BranchList::const_iterator it = branchlist.begin();
166 BranchList::const_iterator const end = branchlist.end();
167 it = std::find_if(it, end, SameBranch(params_.branch));
170 return it->getSelected();
174 int InsetBranch::latex(Buffer const & buf, ostream & os,
175 OutputParams const & runparams) const
177 return isBranchSelected(buf.params().branchlist()) ?
178 inset.latex(buf, os, runparams) : 0;
182 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
183 OutputParams const & runparams) const
185 return isBranchSelected(buf.params().branchlist()) ?
186 inset.linuxdoc(buf, os, runparams) : 0;
190 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
191 OutputParams const & runparams) const
193 return isBranchSelected(buf.params().branchlist()) ?
194 inset.docbook(buf, os, runparams) : 0;
198 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
199 OutputParams const & runparams) const
201 return isBranchSelected(buf.params().branchlist()) ?
202 inset.plaintext(buf, os, runparams): 0;
206 void InsetBranch::validate(LaTeXFeatures & features) const
208 inset.validate(features);
213 string const InsetBranchMailer:: name_("branch");
215 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
220 string const InsetBranchMailer::inset2string(Buffer const &) const
222 return params2string(inset_.params());
226 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
229 data << name_ << ' ';
235 void InsetBranchMailer::string2params(string const & in,
236 InsetBranchParams & params)
238 params = InsetBranchParams();
242 istringstream data(in);
249 return print_mailer_error("InsetBranchMailer", in, 1, name_);
251 // This is part of the inset proper that is usually swallowed
252 // by LyXText::readInset
255 if (!lex || id != "Branch")
256 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
262 void InsetBranchParams::write(ostream & os) const
264 os << "Branch " << branch << '\n';
268 void InsetBranchParams::read(LyXLex & lex)