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 "BufferView.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
23 #include "paragraph.h"
25 #include "support/std_sstream.h"
30 using std::istringstream;
32 using std::ostringstream;
35 void InsetBranch::init()
37 setInsetName("Branch");
42 InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
43 : InsetCollapsable(bp)
45 params_.branch = label;
46 // Hack: stash the list of all allowable branch labels from this
47 // buffer into inset's parm list as a "stowaway":
48 params_.branchlist = bp.branchlist();
53 InsetBranch::InsetBranch(InsetBranch const & in)
54 : InsetCollapsable(in), params_(in.params_)
60 InsetBranch::~InsetBranch()
62 InsetBranchMailer mailer("branch", *this);
67 auto_ptr<InsetBase> InsetBranch::clone() const
69 return auto_ptr<InsetBase>(new InsetBranch(*this));
73 string const InsetBranch::editMessage() const
75 return _("Opened Branch Inset");
79 void InsetBranch::write(Buffer const & buf, ostream & os) const
82 InsetCollapsable::write(buf, os);
86 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
90 params_.branch = lex.getString();
92 InsetCollapsable::read(buf, lex);
97 void InsetBranch::setButtonLabel()
99 LyXFont font(LyXFont::ALL_SANE);
103 setLabel("Branch: " + params_.branch);
104 font.setColor(LColor::foreground);
105 if (!params_.branch.empty())
106 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
108 setBackgroundColor(LColor::background);
113 bool InsetBranch::showInsetDialog(BufferView * bv) const
115 InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
121 InsetBranch::priv_dispatch(FuncRequest const & cmd,
122 idx_type & idx, pos_type & pos)
124 BufferView * bv = cmd.view();
125 switch (cmd.action) {
126 case LFUN_INSET_MODIFY: {
127 InsetBranchParams params;
128 InsetBranchMailer::string2params(cmd.argument, params);
129 params_.branch = params.branch;
131 return DispatchResult(true, true);
134 case LFUN_MOUSE_PRESS:
135 if (cmd.button() != mouse_button::button3)
136 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
137 return DispatchResult(false);
139 case LFUN_INSET_DIALOG_UPDATE:
140 InsetBranchMailer("branch", *this).updateDialog(bv);
141 return DispatchResult(true);
143 case LFUN_MOUSE_RELEASE:
144 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
145 InsetBranchMailer("branch", *this).showDialog(bv);
146 return DispatchResult(true);
148 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
151 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
156 int InsetBranch::latex(Buffer const & buf, ostream & os,
157 OutputParams const & runparams) const
159 string const branch_sel = buf.params().branchlist().allSelected();
160 if (branch_sel.find(params_.branch, 0) != string::npos)
161 return inset.latex(buf, os, runparams);
166 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
167 OutputParams const & runparams) const
169 string const branch_sel = buf.params().branchlist().allSelected();
170 if (branch_sel.find(params_.branch, 0) != string::npos)
171 return inset.linuxdoc(buf, os, runparams);
176 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
177 OutputParams const & runparams) const
179 string const branch_sel = buf.params().branchlist().allSelected();
180 if (branch_sel.find(params_.branch, 0) != string::npos)
181 return inset.docbook(buf, os, runparams);
186 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
187 OutputParams const & runparams) const
189 string const branch_sel = buf.params().branchlist().allSelected();
190 if (branch_sel.find(params_.branch, 0) != string::npos) {
191 return inset.plaintext(buf, os, runparams);
197 void InsetBranch::validate(LaTeXFeatures & features) const
199 inset.validate(features);
204 InsetBranchMailer::InsetBranchMailer(string const & name,
206 : name_(name), inset_(inset)
211 string const InsetBranchMailer::inset2string(Buffer const & buf) const
213 InsetBranchParams params = inset_.params();
214 params.branchlist = buf.params().branchlist();
215 inset_.setParams(params);
216 return params2string(name_, params);
220 string const InsetBranchMailer::params2string(string const & name,
221 InsetBranchParams const & params)
226 // Add all_branches parameter to data:
227 data << params.branchlist.allBranches() << "\n";
232 void InsetBranchMailer::string2params(string const & in,
233 InsetBranchParams & params)
235 params = InsetBranchParams();
240 istringstream data(in);
244 // Process all_branches here:
247 params.branchlist.add(lex.getString());
252 void InsetBranchParams::write(ostream & os) const
254 os << "Branch" << " " << branch << "\n";
258 void InsetBranchParams::read(LyXLex & lex)
262 string token = lex.getString();
266 string token = lex.getString();
270 branch = lex.getString();