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 "FuncStatus.h"
18 #include "FuncRequest.h"
19 #include "InsetList.h"
22 #include "OutputParams.h"
23 #include "ParIterator.h"
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
36 InsetArgument::InsetArgument(Buffer * buf, string const & name)
37 : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
38 font_(inherit_font), labelfont_(inherit_font), decoration_(string())
42 void InsetArgument::write(ostream & os) const
44 os << "Argument " << name_ << "\n";
45 InsetCollapsable::write(os);
48 void InsetArgument::read(Lexer & lex)
51 InsetCollapsable::read(lex);
54 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
56 Layout::LaTeXArgMap args;
57 bool const insetlayout = &it.inset() && it.paragraph().layout().latexargs().empty();
59 args = it.inset().getLayout().latexargs();
61 args = it.paragraph().layout().latexargs();
63 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
65 unsigned int const req = insetlayout ? it.inset().getLayout().numRequiredArgs()
66 : it.paragraph().layout().requiredArgs();
67 unsigned int const opts = insetlayout ? it.inset().getLayout().numOptArgs()
68 : it.paragraph().layout().optArgs();
70 unsigned int ours = 0;
71 InsetList::const_iterator parit = it.paragraph().insetList().begin();
72 InsetList::const_iterator parend = it.paragraph().insetList().end();
73 for (; parit != parend; ++parit) {
74 if (parit->inset->lyxCode() == ARG_CODE) {
76 if (parit->inset == this)
81 unsigned int realopts = 0;
83 // We have optional arguments
85 if (ours <= realopts) {
86 name_ = convert<string>(ours);
91 // This is a mandatory argument. We have to consider
92 // non-given optional arguments for the numbering
93 int offset = opts - realopts;
95 name_ = convert<string>(ours);
98 Layout::LaTeXArgMap::const_iterator const lait =
99 args.find(convert<unsigned int>(name_));
100 if (lait != args.end()) {
101 docstring label = translateIfPossible((*lait).second.labelstring);
102 docstring striplabel;
103 support::rsplit(label, striplabel, '|');
104 labelstring_ = striplabel.empty() ? label: striplabel;
105 tooltip_ = translateIfPossible((*lait).second.tooltip);
106 font_ = (*lait).second.font;
107 labelfont_ = (*lait).second.labelfont;
108 decoration_ = (*lait).second.decoration;
110 labelstring_ = _("Unknown Argument");
111 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
114 InsetCollapsable::updateBuffer(it, utype);
117 void InsetArgument::setButtonLabel()
119 setLabel(labelstring_);
122 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
126 return toolTipText(tooltip_ + from_ascii(":\n"));
129 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
131 switch (cmd.action()) {
133 case LFUN_INSET_MODIFY: {
134 string const first_arg = cmd.getArg(0);
135 bool const change_type = first_arg == "changetype";
138 // this will not be handled higher up
142 cur.recordUndoInset(ATOMIC_UNDO, this);
143 name_ = cmd.getArg(1);
144 cur.forceBufferUpdate();
149 InsetCollapsable::doDispatch(cur, cmd);
155 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
156 FuncStatus & flag) const
158 switch (cmd.action()) {
160 case LFUN_INSET_MODIFY: {
161 string const first_arg = cmd.getArg(0);
162 if (first_arg == "changetype") {
163 string const type = cmd.getArg(1);
164 flag.setOnOff(type == name_);
166 flag.setEnabled(true);
169 Layout::LaTeXArgMap args;
170 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
172 args = cur.inset().getLayout().latexargs();
174 args = cur.paragraph().layout().latexargs();
175 Layout::LaTeXArgMap::const_iterator const lait =
176 args.find(convert<unsigned int>(type));
177 if (lait != args.end()) {
178 flag.setEnabled(true);
179 InsetList::const_iterator it = cur.paragraph().insetList().begin();
180 InsetList::const_iterator end = cur.paragraph().insetList().end();
181 for (; it != end; ++it) {
182 if (it->inset->lyxCode() == ARG_CODE) {
183 InsetArgument const * ins =
184 static_cast<InsetArgument const *>(it->inset);
185 if (ins->name() == type) {
186 // we have this already
187 flag.setEnabled(false);
193 flag.setEnabled(false);
196 return InsetCollapsable::getStatus(cur, cmd, flag);
200 return InsetCollapsable::getStatus(cur, cmd, flag);
204 string InsetArgument::contextMenuName() const
206 return "context-argument";
209 FontInfo InsetArgument::getFont() const
211 if (font_ != inherit_font)
213 return getLayout().font();
216 FontInfo InsetArgument::getLabelfont() const
218 if (labelfont_ != inherit_font)
220 return getLayout().labelfont();
225 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
227 if (support::compare_ascii_no_case(str, "classic") == 0)
228 return InsetLayout::CLASSIC;
229 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
230 return InsetLayout::MINIMALISTIC;
231 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
232 return InsetLayout::CONGLOMERATE;
233 return InsetLayout::DEFAULT;
238 InsetLayout::InsetDecoration InsetArgument::decoration() const
240 InsetLayout::InsetDecoration dec = getLayout().decoration();
241 if (!decoration_.empty())
242 dec = translateDecoration(decoration_);
243 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
246 void InsetArgument::latexArgument(otexstream & os,
247 OutputParams const & runparams_in, docstring const & ldelim,
248 docstring const & rdelim) const
252 otexstream ots(ss, texrow);
253 OutputParams runparams = runparams_in;
254 if (getLayout().isPassThru())
255 runparams.pass_thru = true;
256 InsetText::latex(ots, runparams);
257 docstring str = ss.str();
258 if (ldelim != "{" && support::contains(str, rdelim))
259 str = '{' + str + '}';
260 os << ldelim << str << rdelim;