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"
33 using std::istringstream;
35 using std::ostringstream;
38 void InsetBranch::init()
40 setInsetName("Branch");
45 InsetBranch::InsetBranch(BufferParams const & bp,
46 InsetBranchParams const & params)
47 : InsetCollapsable(bp), params_(params)
53 InsetBranch::InsetBranch(InsetBranch const & in)
54 : InsetCollapsable(in), params_(in.params_)
60 InsetBranch::~InsetBranch()
62 InsetBranchMailer(*this).hideDialog();
66 auto_ptr<InsetBase> InsetBranch::doClone() const
68 return auto_ptr<InsetBase>(new InsetBranch(*this));
72 docstring const InsetBranch::editMessage() const
74 return _("Opened Branch Inset");
78 void InsetBranch::write(Buffer const & buf, ostream & os) const
81 InsetCollapsable::write(buf, os);
85 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
88 InsetCollapsable::read(buf, lex);
93 void InsetBranch::setButtonLabel()
95 LyXFont font(LyXFont::ALL_SANE);
100 docstring s = _("Branch: ") + lyx::from_utf8(params_.branch);
101 font.setColor(LColor::foreground);
102 if (!params_.branch.empty()) {
103 LColor_color c = lcolor.getFromLyXName(params_.branch);
104 if (c == LColor::none) {
106 s = _("Undef: ") + s;
108 setBackgroundColor(c);
110 setBackgroundColor(LColor::background);
111 setLabel(isOpen() ? s : getNewLabel(s) );
116 bool InsetBranch::showInsetDialog(BufferView * bv) const
118 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
123 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
125 switch (cmd.action) {
126 case LFUN_INSET_MODIFY: {
127 InsetBranchParams params;
128 InsetBranchMailer::string2params(lyx::to_utf8(cmd.argument()), params);
129 params_.branch = params.branch;
134 case LFUN_MOUSE_PRESS:
135 if (cmd.button() != mouse_button::button3)
136 InsetCollapsable::doDispatch(cur, cmd);
141 case LFUN_INSET_DIALOG_UPDATE:
142 InsetBranchMailer(*this).updateDialog(&cur.bv());
145 case LFUN_MOUSE_RELEASE:
146 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
147 InsetBranchMailer(*this).showDialog(&cur.bv());
149 InsetCollapsable::doDispatch(cur, cmd);
153 case LFUN_INSET_TOGGLE:
154 if (cmd.argument() == "assign" || cmd.argument().empty()) {
155 // The branch inset uses "assign".
156 if (isBranchSelected(cur.buffer())) {
157 if (status() != Open)
158 setStatus(cur, Open);
162 if (status() != Collapsed)
163 setStatus(cur, Collapsed);
169 InsetCollapsable::doDispatch(cur, cmd);
173 InsetCollapsable::doDispatch(cur, cmd);
179 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
180 FuncStatus & flag) const
182 switch (cmd.action) {
183 case LFUN_INSET_MODIFY:
184 case LFUN_INSET_DIALOG_UPDATE:
188 case LFUN_INSET_TOGGLE:
189 if (cmd.argument() == "open" || cmd.argument() == "close" ||
190 cmd.argument() == "toggle")
192 else if (cmd.argument() == "assign"
193 || cmd.argument().empty()) {
194 if (isBranchSelected(cur.buffer()))
195 flag.enabled(status() != Open);
197 flag.enabled(status() != Collapsed);
203 return InsetCollapsable::getStatus(cur, cmd, flag);
209 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
211 Buffer const & realbuffer = *buffer.getMasterBuffer();
212 BranchList const & branchlist = realbuffer.params().branchlist();
213 BranchList::const_iterator const end = branchlist.end();
214 BranchList::const_iterator it =
215 std::find_if(branchlist.begin(), end,
216 BranchNamesEqual(params_.branch));
219 return it->getSelected();
223 int InsetBranch::latex(Buffer const & buf, ostream & os,
224 OutputParams const & runparams) const
226 return isBranchSelected(buf) ?
227 InsetText::latex(buf, os, runparams) : 0;
231 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
232 OutputParams const & runparams) const
234 return isBranchSelected(buf) ?
235 InsetText::docbook(buf, os, runparams) : 0;
239 int InsetBranch::plaintext(Buffer const & buf, lyx::odocstream & os,
240 OutputParams const & runparams) const
242 return isBranchSelected(buf) ?
243 InsetText::plaintext(buf, os, runparams): 0;
247 void InsetBranch::validate(LaTeXFeatures & features) const
249 InsetText::validate(features);
254 string const InsetBranchMailer::name_("branch");
256 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
261 string const InsetBranchMailer::inset2string(Buffer const &) const
263 return params2string(inset_.params());
267 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
270 data << name_ << ' ';
276 void InsetBranchMailer::string2params(string const & in,
277 InsetBranchParams & params)
279 params = InsetBranchParams();
283 istringstream data(in);
290 return print_mailer_error("InsetBranchMailer", in, 1, name_);
292 // This is part of the inset proper that is usually swallowed
293 // by LyXText::readInset
296 if (!lex || id != "Branch")
297 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
303 void InsetBranchParams::write(ostream & os) const
305 os << "Branch " << branch << '\n';
309 void InsetBranchParams::read(LyXLex & lex)