2 * \file InsetArgument.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
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetArgument.h"
17 #include "BufferParams.h"
19 #include "FuncStatus.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
25 #include "OutputParams.h"
26 #include "ParIterator.h"
28 #include "texstream.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
41 InsetArgument::InsetArgument(Buffer * buf, string const & name)
42 : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
43 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
44 pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
45 pass_thru_chars_(docstring())
49 void InsetArgument::write(ostream & os) const
51 os << "Argument " << name_ << "\n";
52 InsetCollapsable::write(os);
56 void InsetArgument::read(Lexer & lex)
59 InsetCollapsable::read(lex);
63 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
65 Layout::LaTeXArgMap args = it.paragraph().layout().args();
66 pass_thru_context_ = it.paragraph().layout().pass_thru;
67 bool const insetlayout = args.empty();
69 args = it.inset().getLayout().args();
70 pass_thru_context_ = it.inset().getLayout().isPassThru();
73 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
75 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
76 : it.paragraph().layout().requiredArgs();
77 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
78 : it.paragraph().layout().optArgs();
80 unsigned int ours = 0;
81 InsetList::const_iterator parit = it.paragraph().insetList().begin();
82 InsetList::const_iterator parend = it.paragraph().insetList().end();
83 for (; parit != parend; ++parit) {
84 if (parit->inset->lyxCode() == ARG_CODE) {
86 if (parit->inset == this)
91 unsigned int realopts = 0;
93 // We have optional arguments
95 if (ours <= realopts) {
96 name_ = convert<string>(ours);
101 // This is a mandatory argument. We have to consider
102 // non-given optional arguments for the numbering
103 int offset = opts - realopts;
105 name_ = convert<string>(ours);
108 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
109 if (lait != args.end()) {
110 docstring label = translateIfPossible((*lait).second.labelstring);
111 docstring striplabel;
112 support::rsplit(label, striplabel, '|');
113 labelstring_ = striplabel.empty() ? label: striplabel;
114 tooltip_ = translateIfPossible((*lait).second.tooltip);
115 font_ = (*lait).second.font;
116 labelfont_ = (*lait).second.labelfont;
117 decoration_ = (*lait).second.decoration;
118 pass_thru_chars_ = (*lait).second.pass_thru_chars;
119 pass_thru_local_ = false;
120 switch ((*lait).second.passthru) {
122 pass_thru_ = pass_thru_context_;
126 pass_thru_local_ = true;
133 labelstring_ = _("Unknown Argument");
134 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
137 InsetCollapsable::updateBuffer(it, utype);
141 void InsetArgument::setButtonLabel()
143 setLabel(labelstring_);
147 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
151 return toolTipText(tooltip_ + from_ascii(":\n"));
155 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
157 switch (cmd.action()) {
159 case LFUN_INSET_MODIFY: {
160 string const first_arg = cmd.getArg(0);
161 bool const change_type = first_arg == "changetype";
164 // this will not be handled higher up
168 cur.recordUndoInset(this);
169 name_ = cmd.getArg(1);
170 cur.forceBufferUpdate();
175 case LFUN_CLIPBOARD_PASTE:
176 case LFUN_SELECTION_PASTE:
177 case LFUN_PRIMARY_SELECTION_PASTE:
178 // Do not call InsetCollapsable::doDispatch(cur, cmd)
179 // with (inherited) pass_thru to avoid call for
180 // fixParagraphsFont(), which does not play nicely with
181 // inherited pass_thru (see #8471).
182 if (pass_thru_ && !pass_thru_local_)
183 text().dispatch(cur, cmd);
185 InsetCollapsable::doDispatch(cur, cmd);
189 InsetCollapsable::doDispatch(cur, cmd);
195 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
196 FuncStatus & flag) const
198 switch (cmd.action()) {
200 case LFUN_INSET_MODIFY: {
201 string const first_arg = cmd.getArg(0);
202 if (first_arg == "changetype") {
203 string const type = cmd.getArg(1);
204 flag.setOnOff(type == name_);
206 flag.setEnabled(true);
209 Layout::LaTeXArgMap args;
210 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
212 args = cur.inset().getLayout().latexargs();
214 args = cur.paragraph().layout().latexargs();
215 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
216 if (lait != args.end()) {
217 flag.setEnabled(true);
218 InsetList::const_iterator it = cur.paragraph().insetList().begin();
219 InsetList::const_iterator end = cur.paragraph().insetList().end();
220 for (; it != end; ++it) {
221 if (it->inset->lyxCode() == ARG_CODE) {
222 InsetArgument const * ins =
223 static_cast<InsetArgument const *>(it->inset);
224 if (ins->name() == type) {
225 // we have this already
226 flag.setEnabled(false);
232 flag.setEnabled(false);
235 return InsetCollapsable::getStatus(cur, cmd, flag);
239 return InsetCollapsable::getStatus(cur, cmd, flag);
244 string InsetArgument::contextMenuName() const
246 if (decoration() == InsetLayout::CONGLOMERATE)
247 return "context-argument-conglomerate";
249 return "context-argument";
253 FontInfo InsetArgument::getFont() const
255 if (font_ != inherit_font)
257 return InsetCollapsable::getFont();
261 FontInfo InsetArgument::getLabelfont() const
263 if (labelfont_ != inherit_font)
265 return InsetCollapsable::getLabelfont();
269 ColorCode InsetArgument::labelColor() const {
270 if (labelfont_.color() != Color_inherit)
271 return labelfont_.color();
272 return InsetCollapsable::labelColor();
276 InsetLayout::InsetDecoration InsetArgument::decoration() const
278 InsetLayout::InsetDecoration dec = getLayout().decoration();
279 if (!decoration_.empty())
280 dec = translateDecoration(decoration_);
281 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
285 void InsetArgument::latexArgument(otexstream & os,
286 OutputParams const & runparams_in, docstring const & ldelim,
287 docstring const & rdelim, docstring const & presetarg) const
289 otexstringstream ots;
290 OutputParams runparams = runparams_in;
291 if (!pass_thru_chars_.empty())
292 runparams.pass_thru_chars += pass_thru_chars_;
293 runparams.pass_thru = isPassThru();
294 InsetText::latex(ots, runparams);
295 TexString ts = ots.release();
296 bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
301 if (!presetarg.empty() && !ts.str.empty())