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;
61 bool const insetlayout = &it.inset() && it.paragraph().layout().latexargs().empty();
63 args = it.inset().getLayout().latexargs();
64 pass_thru_ = it.inset().getLayout().isPassThru();
66 args = it.paragraph().layout().latexargs();
67 pass_thru_ = it.paragraph().layout().pass_thru;
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 =
106 args.find(convert<unsigned int>(name_));
107 if (lait != args.end()) {
108 docstring label = translateIfPossible((*lait).second.labelstring);
109 docstring striplabel;
110 support::rsplit(label, striplabel, '|');
111 labelstring_ = striplabel.empty() ? label: striplabel;
112 tooltip_ = translateIfPossible((*lait).second.tooltip);
113 font_ = (*lait).second.font;
114 labelfont_ = (*lait).second.labelfont;
115 decoration_ = (*lait).second.decoration;
117 labelstring_ = _("Unknown Argument");
118 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
121 InsetCollapsable::updateBuffer(it, utype);
124 void InsetArgument::setButtonLabel()
126 setLabel(labelstring_);
129 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
133 return toolTipText(tooltip_ + from_ascii(":\n"));
136 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
138 switch (cmd.action()) {
140 case LFUN_INSET_MODIFY: {
141 string const first_arg = cmd.getArg(0);
142 bool const change_type = first_arg == "changetype";
145 // this will not be handled higher up
149 cur.recordUndoInset(ATOMIC_UNDO, this);
150 name_ = cmd.getArg(1);
151 cur.forceBufferUpdate();
156 InsetCollapsable::doDispatch(cur, cmd);
162 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
163 FuncStatus & flag) const
165 switch (cmd.action()) {
167 case LFUN_INSET_MODIFY: {
168 string const first_arg = cmd.getArg(0);
169 if (first_arg == "changetype") {
170 string const type = cmd.getArg(1);
171 flag.setOnOff(type == name_);
173 flag.setEnabled(true);
176 Layout::LaTeXArgMap args;
177 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
179 args = cur.inset().getLayout().latexargs();
181 args = cur.paragraph().layout().latexargs();
182 Layout::LaTeXArgMap::const_iterator const lait =
183 args.find(convert<unsigned int>(type));
184 if (lait != args.end()) {
185 flag.setEnabled(true);
186 InsetList::const_iterator it = cur.paragraph().insetList().begin();
187 InsetList::const_iterator end = cur.paragraph().insetList().end();
188 for (; it != end; ++it) {
189 if (it->inset->lyxCode() == ARG_CODE) {
190 InsetArgument const * ins =
191 static_cast<InsetArgument const *>(it->inset);
192 if (ins->name() == type) {
193 // we have this already
194 flag.setEnabled(false);
200 flag.setEnabled(false);
203 return InsetCollapsable::getStatus(cur, cmd, flag);
207 return InsetCollapsable::getStatus(cur, cmd, flag);
211 string InsetArgument::contextMenuName() const
213 return "context-argument";
216 FontInfo InsetArgument::getFont() const
218 if (font_ != inherit_font)
220 return getLayout().font();
223 FontInfo InsetArgument::getLabelfont() const
225 if (labelfont_ != inherit_font)
227 return getLayout().labelfont();
232 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
234 if (support::compare_ascii_no_case(str, "classic") == 0)
235 return InsetLayout::CLASSIC;
236 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
237 return InsetLayout::MINIMALISTIC;
238 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
239 return InsetLayout::CONGLOMERATE;
240 return InsetLayout::DEFAULT;
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) const
259 otexstream ots(ss, texrow);
260 OutputParams runparams = runparams_in;
261 InsetText::latex(ots, runparams);
262 docstring str = ss.str();
263 if (ldelim != "{" && support::contains(str, rdelim))
264 str = '{' + str + '}';
265 os << ldelim << str << rdelim;