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 : InsetCollapsible(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 free_spacing_(false), pass_thru_chars_(docstring()), is_toc_caption_(false),
47 newline_cmd_(string())
51 void InsetArgument::write(ostream & os) const
53 os << "Argument " << name_ << "\n";
54 InsetCollapsible::write(os);
58 void InsetArgument::read(Lexer & lex)
61 InsetCollapsible::read(lex);
65 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
67 bool const insetlayout = !it.paragraph().layout().hasArgs();
68 Layout::LaTeXArgMap const args = insetlayout ?
69 it.inset().getLayout().args() : it.paragraph().layout().args();
70 pass_thru_context_ = insetlayout ?
71 it.inset().getLayout().isPassThru() : it.paragraph().layout().pass_thru;
72 // Record PassThru status in order to act on changes.
73 bool const former_pass_thru = pass_thru_;
75 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
76 // "999" is the conventional name given to those by lyx2lyx
78 int const req = insetlayout ? it.inset().getLayout().requiredArgs()
79 : it.paragraph().layout().requiredArgs();
80 int const opts = insetlayout ? it.inset().getLayout().optArgs()
81 : it.paragraph().layout().optArgs();
84 InsetList::const_iterator parit = it.paragraph().insetList().begin();
85 InsetList::const_iterator parend = it.paragraph().insetList().end();
86 for (; parit != parend; ++parit) {
87 if (parit->inset->lyxCode() == ARG_CODE) {
89 if (parit->inset == this)
96 // We have optional arguments
98 if (ours <= realopts) {
99 name_ = convert<string>(ours);
104 // This is a mandatory argument. We have to consider
105 // non-given optional arguments for the numbering
106 int offset = opts - realopts;
108 name_ = convert<string>(ours);
111 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
112 caption_of_toc_ = string();
113 if (lait != args.end()) {
114 docstring label = translateIfPossible((*lait).second.labelstring);
115 docstring striplabel;
116 support::rsplit(label, striplabel, '|');
117 labelstring_ = striplabel.empty() ? label: striplabel;
118 tooltip_ = translateIfPossible((*lait).second.tooltip);
119 font_ = (*lait).second.font;
120 labelfont_ = (*lait).second.labelfont;
121 decoration_ = (*lait).second.decoration;
122 pass_thru_chars_ = (*lait).second.pass_thru_chars;
123 newline_cmd_ = (*lait).second.newlinecmd;
124 free_spacing_ = (*lait).second.free_spacing;
125 pass_thru_local_ = false;
126 if (lait->second.is_toc_caption) {
127 is_toc_caption_ = true;
128 // empty if AddToToc is not set
129 caption_of_toc_ = insetlayout
130 ? it.inset().getLayout().tocType()
131 : it.paragraph().layout().tocType();
134 switch ((*lait).second.passthru) {
136 pass_thru_ = pass_thru_context_;
140 pass_thru_local_ = true;
147 labelstring_ = _("Unknown Argument");
148 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
151 if (former_pass_thru != pass_thru_) {
152 // PassThru status changed. We might need to update
153 // the language of the contents
154 Language const * l = insetlayout
155 ? it.inset().buffer().language()
156 : it.buffer()->language();
157 fixParagraphLanguage(l);
161 InsetCollapsible::updateBuffer(it, utype, deleted);
165 void InsetArgument::setButtonLabel()
167 setLabel(labelstring_);
171 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
175 return toolTipText(tooltip_ + from_ascii(":\n"));
179 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
181 switch (cmd.action()) {
183 case LFUN_INSET_MODIFY: {
184 string const first_arg = cmd.getArg(0);
185 bool const change_type = first_arg == "changetype";
188 // this will not be handled higher up
192 cur.recordUndoInset(this);
193 name_ = cmd.getArg(1);
194 cur.forceBufferUpdate();
199 case LFUN_CLIPBOARD_PASTE:
200 case LFUN_SELECTION_PASTE:
201 case LFUN_PRIMARY_SELECTION_PASTE:
202 case LFUN_SELF_INSERT:
203 // With (only) inherited pass_thru, call Text::dispatch()
204 // directly to avoid call for fixParagraphsFont() and/or
205 // forcing to latex_language in InsetText::dispatch(),
206 // since this does not play nicely with inherited pass_thru
208 if (pass_thru_ && !pass_thru_local_) {
209 text().dispatch(cur, cmd);
210 // For the paste operations, check if we have
211 // non-latex_language, and if so, fix.
212 if (cmd.action() != LFUN_SELF_INSERT)
213 fixParagraphLanguage(buffer().params().language);
216 InsetCollapsible::doDispatch(cur, cmd);
220 InsetCollapsible::doDispatch(cur, cmd);
226 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
227 FuncStatus & flag) const
229 switch (cmd.action()) {
231 case LFUN_INSET_MODIFY: {
232 string const first_arg = cmd.getArg(0);
233 if (first_arg == "changetype") {
234 string const type = cmd.getArg(1);
235 flag.setOnOff(type == name_);
237 flag.setEnabled(true);
240 Layout::LaTeXArgMap args;
241 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
243 args = cur.inset().getLayout().latexargs();
245 args = cur.paragraph().layout().latexargs();
246 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
247 if (lait != args.end()) {
248 flag.setEnabled(true);
249 for (auto const & table : cur.paragraph().insetList())
250 if (InsetArgument const * ins = table.inset->asInsetArgument())
251 if (ins->name() == type) {
252 // we have this already
253 flag.setEnabled(false);
257 flag.setEnabled(false);
260 return InsetCollapsible::getStatus(cur, cmd, flag);
264 return InsetCollapsible::getStatus(cur, cmd, flag);
269 string InsetArgument::contextMenuName() const
271 if (decoration() == InsetLayout::CONGLOMERATE)
272 return "context-argument-conglomerate";
274 return "context-argument";
278 FontInfo InsetArgument::getFont() const
280 if (font_ != inherit_font)
282 return InsetCollapsible::getFont();
286 FontInfo InsetArgument::getLabelfont() const
288 if (labelfont_ != inherit_font)
290 return InsetCollapsible::getLabelfont();
294 ColorCode InsetArgument::labelColor() const {
295 if (labelfont_.color() != Color_inherit)
296 return labelfont_.color();
297 return InsetCollapsible::labelColor();
301 InsetLayout::InsetDecoration InsetArgument::decoration() const
303 InsetLayout::InsetDecoration dec = getLayout().decoration();
304 if (!decoration_.empty())
305 dec = translateDecoration(decoration_);
306 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
310 void InsetArgument::latexArgument(otexstream & os,
311 OutputParams const & runparams_in, docstring const & ldelim,
312 docstring const & rdelim, docstring const & presetarg) const
314 otexstringstream ots;
315 OutputParams runparams = runparams_in;
316 if (!pass_thru_chars_.empty())
317 runparams.pass_thru_chars += pass_thru_chars_;
318 if (!newline_cmd_.empty())
319 runparams.newlinecmd = newline_cmd_;
320 runparams.pass_thru = isPassThru();
321 InsetText::latex(ots, runparams);
322 TexString ts = ots.release();
323 bool const add_braces = !ldelim.empty() && ldelim != "{"
324 && support::contains(ts.str, rdelim);
329 if (!presetarg.empty() && !ts.str.empty())
338 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
339 UpdateType utype, TocBackend & backend) const
341 if (!caption_of_toc_.empty()) {
343 text().forOutliner(str, TOC_ENTRY_LENGTH);
344 backend.builder(caption_of_toc_).argumentItem(str);
346 // Proceed with the rest of the inset.
347 InsetText::addToToc(dit, output_active, utype, backend);
351 void InsetArgument::fixParagraphLanguage(Language const * l)
353 Font font(inherit_font, l);
355 font.setLanguage(latex_language);
356 paragraphs().front().resetFonts(font);