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 "support/convert.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
39 InsetArgument::InsetArgument(Buffer * buf, string const & name)
40 : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
41 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
42 pass_thru_(false), pass_thru_chars_(docstring())
46 void InsetArgument::write(ostream & os) const
48 os << "Argument " << name_ << "\n";
49 InsetCollapsable::write(os);
53 void InsetArgument::read(Lexer & lex)
56 InsetCollapsable::read(lex);
60 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
62 Layout::LaTeXArgMap args = it.paragraph().layout().args();
63 pass_thru_ = it.paragraph().layout().pass_thru;
64 bool const insetlayout = args.empty();
66 args = it.inset().getLayout().args();
67 pass_thru_ = it.inset().getLayout().isPassThru();
70 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
72 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
73 : it.paragraph().layout().requiredArgs();
74 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
75 : it.paragraph().layout().optArgs();
77 unsigned int ours = 0;
78 InsetList::const_iterator parit = it.paragraph().insetList().begin();
79 InsetList::const_iterator parend = it.paragraph().insetList().end();
80 for (; parit != parend; ++parit) {
81 if (parit->inset->lyxCode() == ARG_CODE) {
83 if (parit->inset == this)
88 unsigned int realopts = 0;
90 // We have optional arguments
92 if (ours <= realopts) {
93 name_ = convert<string>(ours);
98 // This is a mandatory argument. We have to consider
99 // non-given optional arguments for the numbering
100 int offset = opts - realopts;
102 name_ = convert<string>(ours);
105 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
106 if (lait != args.end()) {
107 docstring label = translateIfPossible((*lait).second.labelstring);
108 docstring striplabel;
109 support::rsplit(label, striplabel, '|');
110 labelstring_ = striplabel.empty() ? label: striplabel;
111 tooltip_ = translateIfPossible((*lait).second.tooltip);
112 font_ = (*lait).second.font;
113 labelfont_ = (*lait).second.labelfont;
114 decoration_ = (*lait).second.decoration;
115 pass_thru_chars_ = (*lait).second.pass_thru_chars;
117 labelstring_ = _("Unknown Argument");
118 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
121 InsetCollapsable::updateBuffer(it, utype);
125 void InsetArgument::setButtonLabel()
127 setLabel(labelstring_);
131 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
135 return toolTipText(tooltip_ + from_ascii(":\n"));
139 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 string const first_arg = cmd.getArg(0);
145 bool const change_type = first_arg == "changetype";
148 // this will not be handled higher up
152 cur.recordUndoInset(this);
153 name_ = cmd.getArg(1);
154 cur.forceBufferUpdate();
159 case LFUN_CLIPBOARD_PASTE:
160 case LFUN_SELECTION_PASTE:
161 case LFUN_PRIMARY_SELECTION_PASTE:
162 // Do not call InsetCollapsable::doDispatch(cur, cmd)
163 // with (inherited) pass_thru to avoid call for
164 // fixParagraphsFont(), which does not play nicely with
165 // inherited pass_thru (see #8471).
166 // FIXME: Once we have implemented genuine pass_thru
167 // option for InsetArgument (not inherited pass_thru),
168 // we should probably directly call
169 // InsetCollapsable::doDispatch(cur, cmd) for that
172 text().dispatch(cur, cmd);
174 InsetCollapsable::doDispatch(cur, cmd);
178 InsetCollapsable::doDispatch(cur, cmd);
184 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
185 FuncStatus & flag) const
187 switch (cmd.action()) {
189 case LFUN_INSET_MODIFY: {
190 string const first_arg = cmd.getArg(0);
191 if (first_arg == "changetype") {
192 string const type = cmd.getArg(1);
193 flag.setOnOff(type == name_);
195 flag.setEnabled(true);
198 Layout::LaTeXArgMap args;
199 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
201 args = cur.inset().getLayout().latexargs();
203 args = cur.paragraph().layout().latexargs();
204 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
205 if (lait != args.end()) {
206 flag.setEnabled(true);
207 InsetList::const_iterator it = cur.paragraph().insetList().begin();
208 InsetList::const_iterator end = cur.paragraph().insetList().end();
209 for (; it != end; ++it) {
210 if (it->inset->lyxCode() == ARG_CODE) {
211 InsetArgument const * ins =
212 static_cast<InsetArgument const *>(it->inset);
213 if (ins->name() == type) {
214 // we have this already
215 flag.setEnabled(false);
221 flag.setEnabled(false);
224 return InsetCollapsable::getStatus(cur, cmd, flag);
228 return InsetCollapsable::getStatus(cur, cmd, flag);
233 string InsetArgument::contextMenuName() const
235 return "context-argument";
239 FontInfo InsetArgument::getFont() const
241 if (font_ != inherit_font)
243 return getLayout().font();
247 FontInfo InsetArgument::getLabelfont() const
249 if (labelfont_ != inherit_font)
251 return getLayout().labelfont();
255 InsetLayout::InsetDecoration InsetArgument::decoration() const
257 InsetLayout::InsetDecoration dec = getLayout().decoration();
258 if (!decoration_.empty())
259 dec = translateDecoration(decoration_);
260 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
264 void InsetArgument::latexArgument(otexstream & os,
265 OutputParams const & runparams_in, docstring const & ldelim,
266 docstring const & rdelim, docstring const & presetarg) const
270 otexstream ots(ss, texrow);
271 OutputParams runparams = runparams_in;
272 if (!pass_thru_chars_.empty())
273 runparams.pass_thru_chars += pass_thru_chars_;
274 InsetText::latex(ots, runparams);
275 docstring str = ss.str();
276 docstring const sep = str.empty() ? docstring() : from_ascii(", ");
277 if (!presetarg.empty())
278 str = presetarg + sep + str;
279 if (ldelim != "{" && support::contains(str, rdelim))
280 str = '{' + str + '}';
281 os << ldelim << str << rdelim;