2 * \file InsetBranch.cpp
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 "BranchList.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "OutputParams.h"
27 #include "TextClass.h"
29 #include "support/debug.h"
30 #include "support/gettext.h"
32 #include "frontends/Application.h"
41 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
42 : InsetCollapsable(buf), params_(params)
46 InsetBranch::~InsetBranch()
48 hideDialogs("branch", this);
52 docstring InsetBranch::editMessage() const
54 return _("Opened Branch Inset");
58 void InsetBranch::write(ostream & os) const
61 InsetCollapsable::write(os);
65 void InsetBranch::read(Lexer & lex)
68 InsetCollapsable::read(lex);
72 docstring InsetBranch::toolTip(BufferView const &, int, int) const
74 return _("Branch: ") + params_.branch;
78 void InsetBranch::setButtonLabel()
80 docstring s = _("Branch: ") + params_.branch;
81 if (!params_.branch.empty()) {
83 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
84 if (c == Color_none) {
88 if (decoration() == InsetLayout::Classic)
89 setLabel(isOpen() ? s : getNewLabel(s) );
91 setLabel(params_.branch + ": " + getNewLabel(s));
95 ColorCode InsetBranch::backgroundColor() const
97 if (!params_.branch.empty()) {
99 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
100 if (c == Color_none) {
105 return Inset::backgroundColor();
109 bool InsetBranch::showInsetDialog(BufferView * bv) const
111 bv->showDialog("branch", params2string(params()),
112 const_cast<InsetBranch *>(this));
117 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
119 switch (cmd.action) {
120 case LFUN_INSET_MODIFY: {
121 InsetBranchParams params;
122 InsetBranch::string2params(to_utf8(cmd.argument()), params);
123 params_.branch = params.branch;
124 setLayout(cur.buffer().params());
128 case LFUN_MOUSE_PRESS:
129 if (cmd.button() != mouse_button::button3)
130 InsetCollapsable::doDispatch(cur, cmd);
135 case LFUN_INSET_DIALOG_UPDATE:
136 cur.bv().updateDialog("branch", params2string(params()));
139 case LFUN_INSET_TOGGLE:
140 if (cmd.argument() == "assign") {
141 // The branch inset uses "assign".
142 if (isBranchSelected()) {
143 if (status() != Open)
144 setStatus(cur, Open);
148 if (status() != Collapsed)
149 setStatus(cur, Collapsed);
155 InsetCollapsable::doDispatch(cur, cmd);
159 InsetCollapsable::doDispatch(cur, cmd);
165 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
166 FuncStatus & flag) const
168 switch (cmd.action) {
169 case LFUN_INSET_MODIFY:
170 case LFUN_INSET_DIALOG_UPDATE:
171 flag.setEnabled(true);
174 case LFUN_INSET_TOGGLE:
175 if (cmd.argument() == "open" || cmd.argument() == "close" ||
176 cmd.argument() == "toggle")
177 flag.setEnabled(true);
178 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
179 if (isBranchSelected())
180 flag.setEnabled(status() != Open);
182 flag.setEnabled(status() != Collapsed);
184 flag.setEnabled(true);
188 return InsetCollapsable::getStatus(cur, cmd, flag);
194 bool InsetBranch::isBranchSelected() const
196 Buffer const & realbuffer = *buffer().masterBuffer();
197 BranchList const & branchlist = realbuffer.params().branchlist();
198 Branch const * ourBranch = branchlist.find(params_.branch);
201 return ourBranch->getSelected();
205 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
207 return isBranchSelected() ? InsetText::latex(os, runparams) : 0;
211 int InsetBranch::plaintext(odocstream & os,
212 OutputParams const & runparams) const
214 if (!isBranchSelected())
217 os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
218 InsetText::plaintext(os, runparams);
221 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
225 int InsetBranch::docbook(odocstream & os,
226 OutputParams const & runparams) const
228 return isBranchSelected() ? InsetText::docbook(os, runparams) : 0;
232 void InsetBranch::textString(odocstream & os) const
234 if (isBranchSelected())
235 os << paragraphs().begin()->asString(AS_STR_LABEL | AS_STR_INSETS);
239 void InsetBranch::validate(LaTeXFeatures & features) const
241 InsetText::validate(features);
245 bool InsetBranch::isMacroScope() const
247 // Its own scope if not selected by buffer
248 return !isBranchSelected();
252 string InsetBranch::params2string(InsetBranchParams const & params)
255 data << "branch" << ' ';
261 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
263 params = InsetBranchParams();
267 istringstream data(in);
270 lex.setContext("InsetBranch::string2params");
271 lex >> "branch" >> "Branch";
276 void InsetBranchParams::write(ostream & os) const
278 os << "Branch " << to_utf8(branch) << '\n';
282 void InsetBranchParams::read(Lexer & lex)