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 font.setColor(LColor::foreground);
99 if (!params_.branch.empty()) {
100 LColor_color c = lcolor.getFromLyXName(params_.branch);
101 if (c == LColor::none) {
103 s = _("Undef: ") + s;
105 setBackgroundColor(c);
107 setBackgroundColor(LColor::background);
108 setLabel(isOpen() ? s : getNewLabel(s) );
113 bool InsetBranch::showInsetDialog(BufferView * bv) const
115 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
120 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
122 switch (cmd.action) {
123 case LFUN_INSET_MODIFY: {
124 InsetBranchParams params;
125 InsetBranchMailer::string2params(lyx::to_utf8(cmd.argument()), params);
126 params_.branch = params.branch;
131 case LFUN_MOUSE_PRESS:
132 if (cmd.button() != mouse_button::button3)
133 InsetCollapsable::doDispatch(cur, cmd);
138 case LFUN_INSET_DIALOG_UPDATE:
139 InsetBranchMailer(*this).updateDialog(&cur.bv());
142 case LFUN_MOUSE_RELEASE:
143 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
144 InsetBranchMailer(*this).showDialog(&cur.bv());
146 InsetCollapsable::doDispatch(cur, cmd);
150 case LFUN_INSET_TOGGLE:
151 if (cmd.argument() == "assign" || cmd.argument().empty()) {
152 // The branch inset uses "assign".
153 if (isBranchSelected(cur.buffer())) {
154 if (status() != Open)
155 setStatus(cur, Open);
159 if (status() != Collapsed)
160 setStatus(cur, Collapsed);
166 InsetCollapsable::doDispatch(cur, cmd);
170 InsetCollapsable::doDispatch(cur, cmd);
176 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
177 FuncStatus & flag) const
179 switch (cmd.action) {
180 case LFUN_INSET_MODIFY:
181 case LFUN_INSET_DIALOG_UPDATE:
185 case LFUN_INSET_TOGGLE:
186 if (cmd.argument() == "open" || cmd.argument() == "close" ||
187 cmd.argument() == "toggle")
189 else if (cmd.argument() == "assign"
190 || cmd.argument().empty()) {
191 if (isBranchSelected(cur.buffer()))
192 flag.enabled(status() != Open);
194 flag.enabled(status() != Collapsed);
200 return InsetCollapsable::getStatus(cur, cmd, flag);
206 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
208 Buffer const & realbuffer = *buffer.getMasterBuffer();
209 BranchList const & branchlist = realbuffer.params().branchlist();
210 BranchList::const_iterator const end = branchlist.end();
211 BranchList::const_iterator it =
212 std::find_if(branchlist.begin(), end,
213 BranchNamesEqual(params_.branch));
216 return it->getSelected();
220 int InsetBranch::latex(Buffer const & buf, ostream & os,
221 OutputParams const & runparams) const
223 return isBranchSelected(buf) ?
224 InsetText::latex(buf, os, runparams) : 0;
228 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
229 OutputParams const & runparams) const
231 return isBranchSelected(buf) ?
232 InsetText::docbook(buf, os, runparams) : 0;
236 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
237 OutputParams const & runparams) const
239 return isBranchSelected(buf) ?
240 InsetText::plaintext(buf, os, runparams): 0;
244 void InsetBranch::validate(LaTeXFeatures & features) const
246 InsetText::validate(features);
251 string const InsetBranchMailer::name_("branch");
253 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
258 string const InsetBranchMailer::inset2string(Buffer const &) const
260 return params2string(inset_.params());
264 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
267 data << name_ << ' ';
273 void InsetBranchMailer::string2params(string const & in,
274 InsetBranchParams & params)
276 params = InsetBranchParams();
280 istringstream data(in);
287 return print_mailer_error("InsetBranchMailer", in, 1, name_);
289 // This is part of the inset proper that is usually swallowed
290 // by LyXText::readInset
293 if (!lex || id != "Branch")
294 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
300 void InsetBranchParams::write(ostream & os) const
302 os << "Branch " << branch << '\n';
306 void InsetBranchParams::read(LyXLex & lex)