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);
52 void InsetArgument::read(Lexer & lex)
55 InsetCollapsable::read(lex);
58 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
60 Layout::LaTeXArgMap args = it.paragraph().layout().args();
61 pass_thru_ = it.paragraph().layout().pass_thru;
62 bool const insetlayout = &it.inset() && args.empty();
64 args = it.inset().getLayout().args();
65 pass_thru_ = it.inset().getLayout().isPassThru();
68 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
70 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
71 : it.paragraph().layout().requiredArgs();
72 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
73 : it.paragraph().layout().optArgs();
75 unsigned int ours = 0;
76 InsetList::const_iterator parit = it.paragraph().insetList().begin();
77 InsetList::const_iterator parend = it.paragraph().insetList().end();
78 for (; parit != parend; ++parit) {
79 if (parit->inset->lyxCode() == ARG_CODE) {
81 if (parit->inset == this)
86 unsigned int realopts = 0;
88 // We have optional arguments
90 if (ours <= realopts) {
91 name_ = convert<string>(ours);
96 // This is a mandatory argument. We have to consider
97 // non-given optional arguments for the numbering
98 int offset = opts - realopts;
100 name_ = convert<string>(ours);
103 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
104 if (lait != args.end()) {
105 docstring label = translateIfPossible((*lait).second.labelstring);
106 docstring striplabel;
107 support::rsplit(label, striplabel, '|');
108 labelstring_ = striplabel.empty() ? label: striplabel;
109 tooltip_ = translateIfPossible((*lait).second.tooltip);
110 font_ = (*lait).second.font;
111 labelfont_ = (*lait).second.labelfont;
112 decoration_ = (*lait).second.decoration;
114 labelstring_ = _("Unknown Argument");
115 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
118 InsetCollapsable::updateBuffer(it, utype);
121 void InsetArgument::setButtonLabel()
123 setLabel(labelstring_);
126 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
130 return toolTipText(tooltip_ + from_ascii(":\n"));
133 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
135 switch (cmd.action()) {
137 case LFUN_INSET_MODIFY: {
138 string const first_arg = cmd.getArg(0);
139 bool const change_type = first_arg == "changetype";
142 // this will not be handled higher up
146 cur.recordUndoInset(ATOMIC_UNDO, this);
147 name_ = cmd.getArg(1);
148 cur.forceBufferUpdate();
153 case LFUN_CLIPBOARD_PASTE:
154 case LFUN_SELECTION_PASTE:
155 case LFUN_PRIMARY_SELECTION_PASTE:
156 // Do not call InsetCollapsable::doDispatch(cur, cmd)
157 // with (inherited) pass_thru to avoid call for
158 // fixParagraphsFont(), which does not play nicely with
159 // inherited pass_thru (see #8471).
160 // FIXME: Once we have implemented genuine pass_thru
161 // option for InsetArgument (not inherited pass_thru),
162 // we should probably directly call
163 // InsetCollapsable::doDispatch(cur, cmd) for that
166 text().dispatch(cur, cmd);
168 InsetCollapsable::doDispatch(cur, cmd);
172 InsetCollapsable::doDispatch(cur, cmd);
178 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & flag) const
181 switch (cmd.action()) {
183 case LFUN_INSET_MODIFY: {
184 string const first_arg = cmd.getArg(0);
185 if (first_arg == "changetype") {
186 string const type = cmd.getArg(1);
187 flag.setOnOff(type == name_);
189 flag.setEnabled(true);
192 Layout::LaTeXArgMap args;
193 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
195 args = cur.inset().getLayout().latexargs();
197 args = cur.paragraph().layout().latexargs();
198 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
199 if (lait != args.end()) {
200 flag.setEnabled(true);
201 InsetList::const_iterator it = cur.paragraph().insetList().begin();
202 InsetList::const_iterator end = cur.paragraph().insetList().end();
203 for (; it != end; ++it) {
204 if (it->inset->lyxCode() == ARG_CODE) {
205 InsetArgument const * ins =
206 static_cast<InsetArgument const *>(it->inset);
207 if (ins->name() == type) {
208 // we have this already
209 flag.setEnabled(false);
215 flag.setEnabled(false);
218 return InsetCollapsable::getStatus(cur, cmd, flag);
222 return InsetCollapsable::getStatus(cur, cmd, flag);
226 string InsetArgument::contextMenuName() const
228 return "context-argument";
231 FontInfo InsetArgument::getFont() const
233 if (font_ != inherit_font)
235 return getLayout().font();
238 FontInfo InsetArgument::getLabelfont() const
240 if (labelfont_ != inherit_font)
242 return getLayout().labelfont();
245 InsetLayout::InsetDecoration InsetArgument::decoration() const
247 InsetLayout::InsetDecoration dec = getLayout().decoration();
248 if (!decoration_.empty())
249 dec = translateDecoration(decoration_);
250 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
253 void InsetArgument::latexArgument(otexstream & os,
254 OutputParams const & runparams_in, docstring const & ldelim,
255 docstring const & rdelim, docstring const & presetarg) const
259 otexstream ots(ss, texrow);
260 OutputParams runparams = runparams_in;
261 InsetText::latex(ots, runparams);
262 docstring str = ss.str();
263 docstring const sep = str.empty() ? docstring() : from_ascii(", ");
264 if (!presetarg.empty())
265 str = presetarg + sep + str;
266 if (ldelim != "{" && support::contains(str, rdelim))
267 str = '{' + str + '}';
268 os << ldelim << str << rdelim;