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()
62 && it.paragraph().layout().itemargs().empty();
64 args = it.inset().getLayout().latexargs();
65 pass_thru_ = it.inset().getLayout().isPassThru();
67 args = it.paragraph().layout().latexargs();
68 Layout::LaTeXArgMap itemargs = it.paragraph().layout().itemargs();
69 if (!itemargs.empty())
70 args.insert(itemargs.begin(), itemargs.end());
71 pass_thru_ = it.paragraph().layout().pass_thru;
74 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
76 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
77 : it.paragraph().layout().requiredArgs();
78 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
79 : it.paragraph().layout().optArgs();
81 unsigned int ours = 0;
82 InsetList::const_iterator parit = it.paragraph().insetList().begin();
83 InsetList::const_iterator parend = it.paragraph().insetList().end();
84 for (; parit != parend; ++parit) {
85 if (parit->inset->lyxCode() == ARG_CODE) {
87 if (parit->inset == this)
92 unsigned int realopts = 0;
94 // We have optional arguments
96 if (ours <= realopts) {
97 name_ = convert<string>(ours);
102 // This is a mandatory argument. We have to consider
103 // non-given optional arguments for the numbering
104 int offset = opts - realopts;
106 name_ = convert<string>(ours);
109 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
110 if (lait != args.end()) {
111 docstring label = translateIfPossible((*lait).second.labelstring);
112 docstring striplabel;
113 support::rsplit(label, striplabel, '|');
114 labelstring_ = striplabel.empty() ? label: striplabel;
115 tooltip_ = translateIfPossible((*lait).second.tooltip);
116 font_ = (*lait).second.font;
117 labelfont_ = (*lait).second.labelfont;
118 decoration_ = (*lait).second.decoration;
120 labelstring_ = _("Unknown Argument");
121 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
124 InsetCollapsable::updateBuffer(it, utype);
127 void InsetArgument::setButtonLabel()
129 setLabel(labelstring_);
132 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
136 return toolTipText(tooltip_ + from_ascii(":\n"));
139 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 string const first_arg = cmd.getArg(0);
145 bool const change_type = first_arg == "changetype";
148 // this will not be handled higher up
152 cur.recordUndoInset(ATOMIC_UNDO, this);
153 name_ = cmd.getArg(1);
154 cur.forceBufferUpdate();
159 InsetCollapsable::doDispatch(cur, cmd);
165 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
166 FuncStatus & flag) const
168 switch (cmd.action()) {
170 case LFUN_INSET_MODIFY: {
171 string const first_arg = cmd.getArg(0);
172 if (first_arg == "changetype") {
173 string const type = cmd.getArg(1);
174 flag.setOnOff(type == name_);
176 flag.setEnabled(true);
179 Layout::LaTeXArgMap args;
180 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
182 args = cur.inset().getLayout().latexargs();
184 args = cur.paragraph().layout().latexargs();
185 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
186 if (lait != args.end()) {
187 flag.setEnabled(true);
188 InsetList::const_iterator it = cur.paragraph().insetList().begin();
189 InsetList::const_iterator end = cur.paragraph().insetList().end();
190 for (; it != end; ++it) {
191 if (it->inset->lyxCode() == ARG_CODE) {
192 InsetArgument const * ins =
193 static_cast<InsetArgument const *>(it->inset);
194 if (ins->name() == type) {
195 // we have this already
196 flag.setEnabled(false);
202 flag.setEnabled(false);
205 return InsetCollapsable::getStatus(cur, cmd, flag);
209 return InsetCollapsable::getStatus(cur, cmd, flag);
213 string InsetArgument::contextMenuName() const
215 return "context-argument";
218 FontInfo InsetArgument::getFont() const
220 if (font_ != inherit_font)
222 return getLayout().font();
225 FontInfo InsetArgument::getLabelfont() const
227 if (labelfont_ != inherit_font)
229 return getLayout().labelfont();
232 InsetLayout::InsetDecoration InsetArgument::decoration() const
234 InsetLayout::InsetDecoration dec = getLayout().decoration();
235 if (!decoration_.empty())
236 dec = translateDecoration(decoration_);
237 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
240 void InsetArgument::latexArgument(otexstream & os,
241 OutputParams const & runparams_in, docstring const & ldelim,
242 docstring const & rdelim) const
246 otexstream ots(ss, texrow);
247 OutputParams runparams = runparams_in;
248 InsetText::latex(ots, runparams);
249 docstring str = ss.str();
250 if (ldelim != "{" && support::contains(str, rdelim))
251 str = '{' + str + '}';
252 os << ldelim << str << rdelim;