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 InsetCollapsable::doDispatch(cur, cmd);
159 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
160 FuncStatus & flag) const
162 switch (cmd.action()) {
164 case LFUN_INSET_MODIFY: {
165 string const first_arg = cmd.getArg(0);
166 if (first_arg == "changetype") {
167 string const type = cmd.getArg(1);
168 flag.setOnOff(type == name_);
170 flag.setEnabled(true);
173 Layout::LaTeXArgMap args;
174 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
176 args = cur.inset().getLayout().latexargs();
178 args = cur.paragraph().layout().latexargs();
179 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
180 if (lait != args.end()) {
181 flag.setEnabled(true);
182 InsetList::const_iterator it = cur.paragraph().insetList().begin();
183 InsetList::const_iterator end = cur.paragraph().insetList().end();
184 for (; it != end; ++it) {
185 if (it->inset->lyxCode() == ARG_CODE) {
186 InsetArgument const * ins =
187 static_cast<InsetArgument const *>(it->inset);
188 if (ins->name() == type) {
189 // we have this already
190 flag.setEnabled(false);
196 flag.setEnabled(false);
199 return InsetCollapsable::getStatus(cur, cmd, flag);
203 return InsetCollapsable::getStatus(cur, cmd, flag);
207 string InsetArgument::contextMenuName() const
209 return "context-argument";
212 FontInfo InsetArgument::getFont() const
214 if (font_ != inherit_font)
216 return getLayout().font();
219 FontInfo InsetArgument::getLabelfont() const
221 if (labelfont_ != inherit_font)
223 return getLayout().labelfont();
226 InsetLayout::InsetDecoration InsetArgument::decoration() const
228 InsetLayout::InsetDecoration dec = getLayout().decoration();
229 if (!decoration_.empty())
230 dec = translateDecoration(decoration_);
231 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
234 void InsetArgument::latexArgument(otexstream & os,
235 OutputParams const & runparams_in, docstring const & ldelim,
236 docstring const & rdelim, docstring const & presetarg) const
240 otexstream ots(ss, texrow);
241 OutputParams runparams = runparams_in;
242 InsetText::latex(ots, runparams);
243 docstring str = ss.str();
244 docstring const sep = str.empty() ? docstring() : from_ascii(", ");
245 if (!presetarg.empty())
246 str = presetarg + sep + str;
247 if (ldelim != "{" && support::contains(str, rdelim))
248 str = '{' + str + '}';
249 os << ldelim << str << rdelim;