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"
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::doClone() 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 string s = "Branch: " + params_.branch;
98 setLabel(isOpen() ? s : getNewLabel(s) );
99 font.setColor(LColor::foreground);
100 if (!params_.branch.empty())
101 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
103 setBackgroundColor(LColor::background);
108 bool InsetBranch::showInsetDialog(BufferView * bv) const
110 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
115 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
117 switch (cmd.action) {
118 case LFUN_INSET_MODIFY: {
119 InsetBranchParams params;
120 InsetBranchMailer::string2params(cmd.argument, params);
121 params_.branch = params.branch;
126 case LFUN_MOUSE_PRESS:
127 if (cmd.button() != mouse_button::button3)
128 InsetCollapsable::doDispatch(cur, cmd);
133 case LFUN_INSET_DIALOG_UPDATE:
134 InsetBranchMailer(*this).updateDialog(&cur.bv());
137 case LFUN_MOUSE_RELEASE:
138 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
139 InsetBranchMailer(*this).showDialog(&cur.bv());
141 InsetCollapsable::doDispatch(cur, cmd);
145 case LFUN_INSET_TOGGLE:
146 // We assume that this lfun is indeed going to be dispatched.
149 if (cmd.argument == "open")
150 setStatus(cur, Open);
151 else if (cmd.argument == "close")
152 setStatus(cur, Collapsed);
153 else if (cmd.argument == "toggle")
154 setStatus(cur, isOpen() ? Collapsed : Open);
155 else if (cmd.argument == "assign" || cmd.argument.empty()) {
156 // The branch inset uses "assign".
157 BranchList const & branchlist =
158 cur.buffer().params().branchlist();
159 if (isBranchSelected(branchlist)) {
160 if (status() != Open)
161 setStatus(cur, Open);
165 if (status() != Collapsed)
166 setStatus(cur, Collapsed);
174 InsetCollapsable::doDispatch(cur, cmd);
180 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
181 FuncStatus & flag) const
183 switch (cmd.action) {
184 case LFUN_INSET_MODIFY:
185 case LFUN_INSET_DIALOG_UPDATE:
189 case LFUN_INSET_TOGGLE:
190 if (cmd.argument == "open" || cmd.argument == "close" ||
191 cmd.argument == "toggle")
193 else if (cmd.argument == "assign"
194 || cmd.argument.empty()) {
195 BranchList const & branchlist =
196 cur.buffer().params().branchlist();
197 if (isBranchSelected(branchlist))
198 flag.enabled(status() != Open);
200 flag.enabled(status() != Collapsed);
206 return InsetCollapsable::getStatus(cur, cmd, flag);
212 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
214 BranchList::const_iterator const end = branchlist.end();
215 BranchList::const_iterator it =
216 std::find_if(branchlist.begin(), end,
217 BranchNamesEqual(params_.branch));
220 return it->getSelected();
224 int InsetBranch::latex(Buffer const & buf, ostream & os,
225 OutputParams const & runparams) const
227 return isBranchSelected(buf.params().branchlist()) ?
228 InsetText::latex(buf, os, runparams) : 0;
232 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
233 OutputParams const & runparams) const
235 return isBranchSelected(buf.params().branchlist()) ?
236 InsetText::linuxdoc(buf, os, runparams) : 0;
240 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
241 OutputParams const & runparams) const
243 return isBranchSelected(buf.params().branchlist()) ?
244 InsetText::docbook(buf, os, runparams) : 0;
248 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
249 OutputParams const & runparams) const
251 return isBranchSelected(buf.params().branchlist()) ?
252 InsetText::plaintext(buf, os, runparams): 0;
256 void InsetBranch::validate(LaTeXFeatures & features) const
258 InsetText::validate(features);
263 string const InsetBranchMailer::name_("branch");
265 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
270 string const InsetBranchMailer::inset2string(Buffer const &) const
272 return params2string(inset_.params());
276 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
279 data << name_ << ' ';
285 void InsetBranchMailer::string2params(string const & in,
286 InsetBranchParams & params)
288 params = InsetBranchParams();
292 istringstream data(in);
299 return print_mailer_error("InsetBranchMailer", in, 1, name_);
301 // This is part of the inset proper that is usually swallowed
302 // by LyXText::readInset
305 if (!lex || id != "Branch")
306 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
312 void InsetBranchParams::write(ostream & os) const
314 os << "Branch " << branch << '\n';
318 void InsetBranchParams::read(LyXLex & lex)