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 string s = lyx::to_utf8(_("Branch: ")) + 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) {
107 s = lyx::to_utf8(_("Undef: ")) + s;
109 setBackgroundColor(c);
111 setBackgroundColor(LColor::background);
112 setLabel(isOpen() ? s : getNewLabel(s) );
117 bool InsetBranch::showInsetDialog(BufferView * bv) const
119 InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
124 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
126 switch (cmd.action) {
127 case LFUN_INSET_MODIFY: {
128 InsetBranchParams params;
129 InsetBranchMailer::string2params(lyx::to_utf8(cmd.argument()), params);
130 params_.branch = params.branch;
135 case LFUN_MOUSE_PRESS:
136 if (cmd.button() != mouse_button::button3)
137 InsetCollapsable::doDispatch(cur, cmd);
142 case LFUN_INSET_DIALOG_UPDATE:
143 InsetBranchMailer(*this).updateDialog(&cur.bv());
146 case LFUN_MOUSE_RELEASE:
147 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
148 InsetBranchMailer(*this).showDialog(&cur.bv());
150 InsetCollapsable::doDispatch(cur, cmd);
154 case LFUN_INSET_TOGGLE:
155 if (cmd.argument() == "assign" || cmd.argument().empty()) {
156 // The branch inset uses "assign".
157 if (isBranchSelected(cur.buffer())) {
158 if (status() != Open)
159 setStatus(cur, Open);
163 if (status() != Collapsed)
164 setStatus(cur, Collapsed);
170 InsetCollapsable::doDispatch(cur, cmd);
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 if (isBranchSelected(cur.buffer()))
196 flag.enabled(status() != Open);
198 flag.enabled(status() != Collapsed);
204 return InsetCollapsable::getStatus(cur, cmd, flag);
210 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
212 Buffer const & realbuffer = *buffer.getMasterBuffer();
213 BranchList const & branchlist = realbuffer.params().branchlist();
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) ?
228 InsetText::latex(buf, os, runparams) : 0;
232 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
233 OutputParams const & runparams) const
235 return isBranchSelected(buf) ?
236 InsetText::docbook(buf, os, runparams) : 0;
240 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
241 OutputParams const & runparams) const
243 return isBranchSelected(buf) ?
244 InsetText::plaintext(buf, os, runparams): 0;
248 void InsetBranch::validate(LaTeXFeatures & features) const
250 InsetText::validate(features);
255 string const InsetBranchMailer::name_("branch");
257 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
262 string const InsetBranchMailer::inset2string(Buffer const &) const
264 return params2string(inset_.params());
268 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
271 data << name_ << ' ';
277 void InsetBranchMailer::string2params(string const & in,
278 InsetBranchParams & params)
280 params = InsetBranchParams();
284 istringstream data(in);
291 return print_mailer_error("InsetBranchMailer", in, 1, name_);
293 // This is part of the inset proper that is usually swallowed
294 // by LyXText::readInset
297 if (!lex || id != "Branch")
298 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
304 void InsetBranchParams::write(ostream & os) const
306 os << "Branch " << branch << '\n';
310 void InsetBranchParams::read(LyXLex & lex)