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 "texstream.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
41 InsetArgument::InsetArgument(Buffer * buf, string const & name)
42 : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
43 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
44 pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
45 pass_thru_chars_(docstring())
49 void InsetArgument::write(ostream & os) const
51 os << "Argument " << name_ << "\n";
52 InsetCollapsable::write(os);
56 void InsetArgument::read(Lexer & lex)
59 InsetCollapsable::read(lex);
63 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
65 Layout::LaTeXArgMap args = it.paragraph().layout().args();
66 pass_thru_context_ = it.paragraph().layout().pass_thru;
67 bool const insetlayout = args.empty();
69 args = it.inset().getLayout().args();
70 pass_thru_context_ = it.inset().getLayout().isPassThru();
73 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
75 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
76 : it.paragraph().layout().requiredArgs();
77 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
78 : it.paragraph().layout().optArgs();
80 unsigned int ours = 0;
81 InsetList::const_iterator parit = it.paragraph().insetList().begin();
82 InsetList::const_iterator parend = it.paragraph().insetList().end();
83 for (; parit != parend; ++parit) {
84 if (parit->inset->lyxCode() == ARG_CODE) {
86 if (parit->inset == this)
91 unsigned int realopts = 0;
93 // We have optional arguments
95 if (ours <= realopts) {
96 name_ = convert<string>(ours);
101 // This is a mandatory argument. We have to consider
102 // non-given optional arguments for the numbering
103 int offset = opts - realopts;
105 name_ = convert<string>(ours);
108 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
109 if (lait != args.end()) {
110 docstring label = translateIfPossible((*lait).second.labelstring);
111 docstring striplabel;
112 support::rsplit(label, striplabel, '|');
113 labelstring_ = striplabel.empty() ? label: striplabel;
114 tooltip_ = translateIfPossible((*lait).second.tooltip);
115 font_ = (*lait).second.font;
116 labelfont_ = (*lait).second.labelfont;
117 decoration_ = (*lait).second.decoration;
118 pass_thru_chars_ = (*lait).second.pass_thru_chars;
119 pass_thru_local_ = false;
120 switch ((*lait).second.passthru) {
122 pass_thru_ = pass_thru_context_;
126 pass_thru_local_ = true;
133 labelstring_ = _("Unknown Argument");
134 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
137 InsetCollapsable::updateBuffer(it, utype);
141 void InsetArgument::setButtonLabel()
143 setLabel(labelstring_);
147 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
151 return toolTipText(tooltip_ + from_ascii(":\n"));
155 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
157 switch (cmd.action()) {
159 case LFUN_INSET_MODIFY: {
160 string const first_arg = cmd.getArg(0);
161 bool const change_type = first_arg == "changetype";
164 // this will not be handled higher up
168 cur.recordUndoInset(this);
169 name_ = cmd.getArg(1);
170 cur.forceBufferUpdate();
175 case LFUN_CLIPBOARD_PASTE:
176 case LFUN_SELECTION_PASTE:
177 case LFUN_PRIMARY_SELECTION_PASTE:
178 case LFUN_SELF_INSERT:
179 // With (only) inherited pass_thru, call Text::dispatch()
180 // directly to avoid call for fixParagraphsFont() and/or
181 // forcing to latex_language in InsetText::dispatch(),
182 // since this does not play nicely with inherited pass_thru
184 if (pass_thru_ && !pass_thru_local_)
185 text().dispatch(cur, cmd);
187 InsetCollapsable::doDispatch(cur, cmd);
191 InsetCollapsable::doDispatch(cur, cmd);
197 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
198 FuncStatus & flag) const
200 switch (cmd.action()) {
202 case LFUN_INSET_MODIFY: {
203 string const first_arg = cmd.getArg(0);
204 if (first_arg == "changetype") {
205 string const type = cmd.getArg(1);
206 flag.setOnOff(type == name_);
208 flag.setEnabled(true);
211 Layout::LaTeXArgMap args;
212 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
214 args = cur.inset().getLayout().latexargs();
216 args = cur.paragraph().layout().latexargs();
217 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
218 if (lait != args.end()) {
219 flag.setEnabled(true);
220 InsetList::const_iterator it = cur.paragraph().insetList().begin();
221 InsetList::const_iterator end = cur.paragraph().insetList().end();
222 for (; it != end; ++it) {
223 if (it->inset->lyxCode() == ARG_CODE) {
224 InsetArgument const * ins =
225 static_cast<InsetArgument const *>(it->inset);
226 if (ins->name() == type) {
227 // we have this already
228 flag.setEnabled(false);
234 flag.setEnabled(false);
237 return InsetCollapsable::getStatus(cur, cmd, flag);
241 return InsetCollapsable::getStatus(cur, cmd, flag);
246 string InsetArgument::contextMenuName() const
248 if (decoration() == InsetLayout::CONGLOMERATE)
249 return "context-argument-conglomerate";
251 return "context-argument";
255 FontInfo InsetArgument::getFont() const
257 if (font_ != inherit_font)
259 return InsetCollapsable::getFont();
263 FontInfo InsetArgument::getLabelfont() const
265 if (labelfont_ != inherit_font)
267 return InsetCollapsable::getLabelfont();
271 ColorCode InsetArgument::labelColor() const {
272 if (labelfont_.color() != Color_inherit)
273 return labelfont_.color();
274 return InsetCollapsable::labelColor();
278 InsetLayout::InsetDecoration InsetArgument::decoration() const
280 InsetLayout::InsetDecoration dec = getLayout().decoration();
281 if (!decoration_.empty())
282 dec = translateDecoration(decoration_);
283 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
287 void InsetArgument::latexArgument(otexstream & os,
288 OutputParams const & runparams_in, docstring const & ldelim,
289 docstring const & rdelim, docstring const & presetarg) const
291 otexstringstream ots;
292 OutputParams runparams = runparams_in;
293 if (!pass_thru_chars_.empty())
294 runparams.pass_thru_chars += pass_thru_chars_;
295 runparams.pass_thru = isPassThru();
296 InsetText::latex(ots, runparams);
297 TexString ts = ots.release();
298 bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
303 if (!presetarg.empty() && !ts.str.empty())