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()),
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;
116 labelstring_ = _("Unknown Argument");
117 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
120 InsetCollapsable::updateBuffer(it, utype);
124 void InsetArgument::setButtonLabel()
126 setLabel(labelstring_);
130 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
134 return toolTipText(tooltip_ + from_ascii(":\n"));
138 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
140 switch (cmd.action()) {
142 case LFUN_INSET_MODIFY: {
143 string const first_arg = cmd.getArg(0);
144 bool const change_type = first_arg == "changetype";
147 // this will not be handled higher up
151 cur.recordUndoInset(ATOMIC_UNDO, this);
152 name_ = cmd.getArg(1);
153 cur.forceBufferUpdate();
158 case LFUN_CLIPBOARD_PASTE:
159 case LFUN_SELECTION_PASTE:
160 case LFUN_PRIMARY_SELECTION_PASTE:
161 // Do not call InsetCollapsable::doDispatch(cur, cmd)
162 // with (inherited) pass_thru to avoid call for
163 // fixParagraphsFont(), which does not play nicely with
164 // inherited pass_thru (see #8471).
165 // FIXME: Once we have implemented genuine pass_thru
166 // option for InsetArgument (not inherited pass_thru),
167 // we should probably directly call
168 // InsetCollapsable::doDispatch(cur, cmd) for that
171 text().dispatch(cur, cmd);
173 InsetCollapsable::doDispatch(cur, cmd);
177 InsetCollapsable::doDispatch(cur, cmd);
183 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
184 FuncStatus & flag) const
186 switch (cmd.action()) {
188 case LFUN_INSET_MODIFY: {
189 string const first_arg = cmd.getArg(0);
190 if (first_arg == "changetype") {
191 string const type = cmd.getArg(1);
192 flag.setOnOff(type == name_);
194 flag.setEnabled(true);
197 Layout::LaTeXArgMap args;
198 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
200 args = cur.inset().getLayout().latexargs();
202 args = cur.paragraph().layout().latexargs();
203 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
204 if (lait != args.end()) {
205 flag.setEnabled(true);
206 InsetList::const_iterator it = cur.paragraph().insetList().begin();
207 InsetList::const_iterator end = cur.paragraph().insetList().end();
208 for (; it != end; ++it) {
209 if (it->inset->lyxCode() == ARG_CODE) {
210 InsetArgument const * ins =
211 static_cast<InsetArgument const *>(it->inset);
212 if (ins->name() == type) {
213 // we have this already
214 flag.setEnabled(false);
220 flag.setEnabled(false);
223 return InsetCollapsable::getStatus(cur, cmd, flag);
227 return InsetCollapsable::getStatus(cur, cmd, flag);
232 string InsetArgument::contextMenuName() const
234 return "context-argument";
238 FontInfo InsetArgument::getFont() const
240 if (font_ != inherit_font)
242 return getLayout().font();
246 FontInfo InsetArgument::getLabelfont() const
248 if (labelfont_ != inherit_font)
250 return getLayout().labelfont();
254 InsetLayout::InsetDecoration InsetArgument::decoration() const
256 InsetLayout::InsetDecoration dec = getLayout().decoration();
257 if (!decoration_.empty())
258 dec = translateDecoration(decoration_);
259 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
263 void InsetArgument::latexArgument(otexstream & os,
264 OutputParams const & runparams_in, docstring const & ldelim,
265 docstring const & rdelim, docstring const & presetarg) const
269 otexstream ots(ss, texrow);
270 OutputParams runparams = runparams_in;
271 InsetText::latex(ots, runparams);
272 docstring str = ss.str();
273 docstring const sep = str.empty() ? docstring() : from_ascii(", ");
274 if (!presetarg.empty())
275 str = presetarg + sep + str;
276 if (ldelim != "{" && support::contains(str, rdelim))
277 str = '{' + str + '}';
278 os << ldelim << str << rdelim;