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.
15 #include "insetbranch.h"
20 #include "BufferView.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
25 #include "insets/insettext.h"
26 #include "support/LOstream.h"
27 #include "support/lstrings.h"
33 void InsetBranch::init()
35 setInsetName("Branch");
40 InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
41 : InsetCollapsable(bp)
43 params_.branch = label;
44 // Hack: stash the list of all allowable branch labels from this
45 // buffer into inset's parm list as a "stowaway":
46 params_.branchlist = bp.branchlist;
51 InsetBranch::InsetBranch(InsetBranch const & in)
52 : InsetCollapsable(in), params_(in.params_)
58 InsetBranch::~InsetBranch()
60 InsetBranchMailer mailer("branch", *this);
65 auto_ptr<InsetBase> InsetBranch::clone() const
67 return auto_ptr<InsetBase>(new InsetBranch(*this));
71 string const InsetBranch::editMessage() const
73 return _("Opened Branch Inset");
77 void InsetBranch::write(Buffer const * buf, ostream & os) const
80 InsetCollapsable::write(buf, os);
84 void InsetBranch::read(Buffer const * buf, LyXLex & lex)
88 params_.branch = lex.getString();
90 InsetCollapsable::read(buf, lex);
95 void InsetBranch::setButtonLabel()
97 LyXFont font(LyXFont::ALL_SANE);
101 setLabel(params_.branch);
102 font.setColor(LColor::foreground);
103 string const color = params_.branchlist.getColor(params_.branch);
104 if (!color.empty()) {
105 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
107 setBackgroundColor(LColor::background);
112 void InsetBranch::metrics(MetricsInfo & mi, Dimension & dim) const
114 InsetCollapsable::metrics(mi, dim);
119 void InsetBranch::draw(PainterInfo & pi, int x, int y) const
121 InsetCollapsable::draw(pi, x, y);
125 bool InsetBranch::showInsetDialog(BufferView * bv) const
127 InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
132 dispatch_result InsetBranch::localDispatch(FuncRequest const & cmd)
134 BufferView * bv = cmd.view();
135 switch (cmd.action) {
136 case LFUN_INSET_MODIFY:
138 InsetBranchParams params;
139 InsetBranchMailer::string2params(cmd.argument, params);
140 params_.branch = params.branch;
145 case LFUN_INSET_EDIT:
146 if (cmd.button() != mouse_button::button3)
147 return InsetCollapsable::localDispatch(cmd);
149 case LFUN_INSET_DIALOG_UPDATE:
150 InsetBranchMailer("branch", *this).updateDialog(bv);
152 case LFUN_MOUSE_RELEASE:
153 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
154 InsetBranchMailer("branch", *this).showDialog(bv);
159 return InsetCollapsable::localDispatch(cmd);
165 int InsetBranch::latex(Buffer const * buf, ostream & os,
166 LatexRunParams const & runparams) const
168 string const branch_sel = buf->params.branchlist.allSelected();
169 if (branch_sel.find(params_.branch, 0) != string::npos)
170 return inset.latex(buf, os, runparams);
175 int InsetBranch::linuxdoc(Buffer const *, std::ostream &) const
181 int InsetBranch::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
184 string const branch_sel = buf->params.branchlist.allSelected();
185 if (branch_sel.find(params_.branch, 0) != string::npos)
186 return inset.docbook(buf, os, mixcont);
191 int InsetBranch::ascii(Buffer const * buf, std::ostream & os, int ll) const
193 string const branch_sel = buf->params.branchlist.allSelected();
194 if (branch_sel.find(params_.branch, 0) != string::npos) {
195 return inset.ascii(buf, os, ll);
201 void InsetBranch::validate(LaTeXFeatures & features) const
203 inset.validate(features);
208 InsetBranchMailer::InsetBranchMailer(string const & name,
210 : name_(name), inset_(inset)
215 string const InsetBranchMailer::inset2string(Buffer const & buf) const
217 InsetBranchParams params = inset_.params();
218 params.branchlist = buf.params.branchlist;
219 inset_.setParams(params);
220 return params2string(name_, params);
224 string const InsetBranchMailer::params2string(string const & name,
225 InsetBranchParams const & params)
230 // Add all_branches parameter to data:
231 data << params.branchlist.allBranches() << "\n";
232 return STRCONV(data.str());
236 void InsetBranchMailer::string2params(string const & in,
237 InsetBranchParams & params)
239 params = InsetBranchParams();
244 istringstream data(STRCONV(in));
248 // Process all_branches here:
251 params.branchlist.add(lex.getString());
256 void InsetBranchParams::write(ostream & os) const
258 os << "Branch" << " " << branch << "\n";
262 void InsetBranchParams::read(LyXLex & lex)
266 string token = lex.getString();
270 string token = lex.getString();
274 branch = lex.getString();