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"
29 #include "TocBackend.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
42 InsetArgument::InsetArgument(Buffer * buf, string const & name)
43 : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
44 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
45 pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
46 pass_thru_chars_(docstring())
50 void InsetArgument::write(ostream & os) const
52 os << "Argument " << name_ << "\n";
53 InsetCollapsable::write(os);
57 void InsetArgument::read(Lexer & lex)
60 InsetCollapsable::read(lex);
64 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
66 bool const insetlayout = !it.paragraph().layout().hasArgs();
67 Layout::LaTeXArgMap const args = insetlayout ?
68 it.inset().getLayout().args() : it.paragraph().layout().args();
69 pass_thru_context_ = insetlayout ?
70 it.inset().getLayout().isPassThru() : it.paragraph().layout().pass_thru;
72 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
74 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
75 : it.paragraph().layout().requiredArgs();
76 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
77 : it.paragraph().layout().optArgs();
79 unsigned int ours = 0;
80 InsetList::const_iterator parit = it.paragraph().insetList().begin();
81 InsetList::const_iterator parend = it.paragraph().insetList().end();
82 for (; parit != parend; ++parit) {
83 if (parit->inset->lyxCode() == ARG_CODE) {
85 if (parit->inset == this)
90 unsigned int realopts = 0;
92 // We have optional arguments
94 if (ours <= realopts) {
95 name_ = convert<string>(ours);
100 // This is a mandatory argument. We have to consider
101 // non-given optional arguments for the numbering
102 int offset = opts - realopts;
104 name_ = convert<string>(ours);
107 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
108 caption_of_toc_ = string();
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 if (lait->second.is_toc_caption)
121 // empty if AddToToc is not set
122 caption_of_toc_ = insetlayout
123 ? it.inset().getLayout().tocType()
124 : it.paragraph().layout().tocType();
126 switch ((*lait).second.passthru) {
128 pass_thru_ = pass_thru_context_;
132 pass_thru_local_ = true;
139 labelstring_ = _("Unknown Argument");
140 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
143 InsetCollapsable::updateBuffer(it, utype);
147 void InsetArgument::setButtonLabel()
149 setLabel(labelstring_);
153 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
157 return toolTipText(tooltip_ + from_ascii(":\n"));
161 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
163 switch (cmd.action()) {
165 case LFUN_INSET_MODIFY: {
166 string const first_arg = cmd.getArg(0);
167 bool const change_type = first_arg == "changetype";
170 // this will not be handled higher up
174 cur.recordUndoInset(this);
175 name_ = cmd.getArg(1);
176 cur.forceBufferUpdate();
181 case LFUN_CLIPBOARD_PASTE:
182 case LFUN_SELECTION_PASTE:
183 case LFUN_PRIMARY_SELECTION_PASTE:
184 case LFUN_SELF_INSERT:
185 // With (only) inherited pass_thru, call Text::dispatch()
186 // directly to avoid call for fixParagraphsFont() and/or
187 // forcing to latex_language in InsetText::dispatch(),
188 // since this does not play nicely with inherited pass_thru
190 if (pass_thru_ && !pass_thru_local_)
191 text().dispatch(cur, cmd);
193 InsetCollapsable::doDispatch(cur, cmd);
197 InsetCollapsable::doDispatch(cur, cmd);
203 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
204 FuncStatus & flag) const
206 switch (cmd.action()) {
208 case LFUN_INSET_MODIFY: {
209 string const first_arg = cmd.getArg(0);
210 if (first_arg == "changetype") {
211 string const type = cmd.getArg(1);
212 flag.setOnOff(type == name_);
214 flag.setEnabled(true);
217 Layout::LaTeXArgMap args;
218 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
220 args = cur.inset().getLayout().latexargs();
222 args = cur.paragraph().layout().latexargs();
223 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
224 if (lait != args.end()) {
225 flag.setEnabled(true);
226 for (auto const & table : cur.paragraph().insetList())
227 if (InsetArgument const * ins = table.inset->asInsetArgument())
228 if (ins->name() == type) {
229 // we have this already
230 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())
312 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
313 UpdateType utype, TocBackend & backend) const
315 if (!caption_of_toc_.empty()) {
317 text().forOutliner(str, TOC_ENTRY_LENGTH);
318 backend.builder(caption_of_toc_).argumentItem(str);
320 // Proceed with the rest of the inset.
321 InsetText::addToToc(dit, output_active, utype, backend);