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"
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
43 InsetArgument::InsetArgument(Buffer * buf, string const & name)
44 : InsetCollapsible(buf), name_(name), labelstring_(docstring()),
45 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
46 pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
47 free_spacing_(false), pass_thru_chars_(docstring()), is_toc_caption_(false),
48 newline_cmd_(string())
52 void InsetArgument::write(ostream & os) const
54 os << "Argument " << name_ << "\n";
55 InsetCollapsible::write(os);
59 void InsetArgument::read(Lexer & lex)
62 InsetCollapsible::read(lex);
66 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
68 bool const insetlayout = !it.paragraph().layout().hasArgs();
69 Layout::LaTeXArgMap const args = insetlayout ?
70 it.inset().getLayout().args() : it.paragraph().layout().args();
71 pass_thru_context_ = insetlayout ?
72 it.inset().getLayout().isPassThru() : it.paragraph().layout().pass_thru;
73 // Record PassThru status in order to act on changes.
74 bool const former_pass_thru = pass_thru_;
76 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
77 // "999" is the conventional name given to those by lyx2lyx
79 int const req = insetlayout ? it.inset().getLayout().requiredArgs()
80 : it.paragraph().layout().requiredArgs();
81 int const opts = insetlayout ? it.inset().getLayout().optArgs()
82 : it.paragraph().layout().optArgs();
85 InsetList::const_iterator parit = it.paragraph().insetList().begin();
86 InsetList::const_iterator parend = it.paragraph().insetList().end();
87 for (; parit != parend; ++parit) {
88 if (parit->inset->lyxCode() == ARG_CODE) {
90 if (parit->inset == this)
97 // We have optional arguments
99 if (ours <= realopts) {
100 name_ = convert<string>(ours);
105 // This is a mandatory argument. We have to consider
106 // non-given optional arguments for the numbering
107 int offset = opts - realopts;
109 name_ = convert<string>(ours);
112 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
113 caption_of_toc_ = string();
114 if (lait != args.end()) {
115 docstring label = translateIfPossible((*lait).second.labelstring);
116 docstring striplabel;
117 support::rsplit(label, striplabel, '|');
118 labelstring_ = striplabel.empty() ? label: striplabel;
119 tooltip_ = translateIfPossible((*lait).second.tooltip);
120 font_ = (*lait).second.font;
121 labelfont_ = (*lait).second.labelfont;
122 decoration_ = (*lait).second.decoration;
123 pass_thru_chars_ = (*lait).second.pass_thru_chars;
124 newline_cmd_ = (*lait).second.newlinecmd;
125 free_spacing_ = (*lait).second.free_spacing;
126 docbooktag_ = (*lait).second.docbooktag;
127 docbooktagtype_ = (*lait).second.docbooktagtype;
128 docbookattr_ = (*lait).second.docbookattr;
129 pass_thru_local_ = false;
130 if (lait->second.is_toc_caption) {
131 is_toc_caption_ = true;
132 // empty if AddToToc is not set
133 caption_of_toc_ = insetlayout
134 ? it.inset().getLayout().tocType()
135 : it.paragraph().layout().tocType();
138 switch ((*lait).second.passthru) {
140 pass_thru_ = pass_thru_context_;
144 pass_thru_local_ = true;
151 labelstring_ = _("Unknown Argument");
152 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
155 if (former_pass_thru != pass_thru_) {
156 // PassThru status changed. We might need to update
157 // the language of the contents
158 Language const * l = insetlayout
159 ? it.inset().buffer().language()
160 : it.buffer()->language();
161 fixParagraphLanguage(l);
165 InsetCollapsible::updateBuffer(it, utype, deleted);
169 void InsetArgument::setButtonLabel()
171 setLabel(labelstring_);
175 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
179 return toolTipText(tooltip_ + from_ascii(":\n"));
183 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
185 switch (cmd.action()) {
187 case LFUN_INSET_MODIFY: {
188 string const first_arg = cmd.getArg(0);
189 bool const change_type = first_arg == "changetype";
192 // this will not be handled higher up
196 cur.recordUndoInset(this);
197 name_ = cmd.getArg(1);
198 cur.forceBufferUpdate();
203 case LFUN_CLIPBOARD_PASTE:
204 case LFUN_SELECTION_PASTE:
205 case LFUN_PRIMARY_SELECTION_PASTE:
206 case LFUN_SELF_INSERT:
207 // With (only) inherited pass_thru, call Text::dispatch()
208 // directly to avoid call for fixParagraphsFont() and/or
209 // forcing to latex_language in InsetText::dispatch(),
210 // since this does not play nicely with inherited pass_thru
212 if (pass_thru_ && !pass_thru_local_) {
213 text().dispatch(cur, cmd);
214 // For the paste operations, check if we have
215 // non-latex_language, and if so, fix.
216 if (cmd.action() != LFUN_SELF_INSERT)
217 fixParagraphLanguage(buffer().params().language);
220 InsetCollapsible::doDispatch(cur, cmd);
224 InsetCollapsible::doDispatch(cur, cmd);
230 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
231 FuncStatus & flag) const
233 switch (cmd.action()) {
235 case LFUN_INSET_MODIFY: {
236 string const first_arg = cmd.getArg(0);
237 if (first_arg == "changetype") {
238 string const type = cmd.getArg(1);
239 flag.setOnOff(type == name_);
241 flag.setEnabled(true);
244 Layout::LaTeXArgMap args;
245 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
247 args = cur.inset().getLayout().latexargs();
249 args = cur.paragraph().layout().latexargs();
250 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
251 if (lait != args.end()) {
252 flag.setEnabled(true);
253 for (auto const & table : cur.paragraph().insetList())
254 if (InsetArgument const * ins = table.inset->asInsetArgument())
255 if (ins->name() == type) {
256 // we have this already
257 flag.setEnabled(false);
261 flag.setEnabled(false);
264 return InsetCollapsible::getStatus(cur, cmd, flag);
268 return InsetCollapsible::getStatus(cur, cmd, flag);
273 string InsetArgument::contextMenuName() const
275 if (decoration() == InsetLayout::CONGLOMERATE)
276 return "context-argument-conglomerate";
278 return "context-argument";
282 FontInfo InsetArgument::getFont() const
284 if (font_ != inherit_font)
286 return InsetCollapsible::getFont();
290 FontInfo InsetArgument::getLabelfont() const
292 if (labelfont_ != inherit_font)
294 return InsetCollapsible::getLabelfont();
298 ColorCode InsetArgument::labelColor() const {
299 if (labelfont_.color() != Color_inherit)
300 return labelfont_.color();
301 return InsetCollapsible::labelColor();
305 InsetLayout::InsetDecoration InsetArgument::decoration() const
307 InsetLayout::InsetDecoration dec = getLayout().decoration();
308 if (!decoration_.empty())
309 dec = translateDecoration(decoration_);
310 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
314 void InsetArgument::docbook(XMLStream & xs, OutputParams const & rp) const {
315 if (docbooktag_ != from_ascii("NONE")) {
316 // TODO: implement docbooktagtype_.
317 xs << xml::StartTag(docbooktag_, docbookattr_);
318 InsetText::docbook(xs, rp);
319 xs << xml::EndTag(docbooktag_);
325 void InsetArgument::latexArgument(otexstream & os,
326 OutputParams const & runparams_in, docstring const & ldelim,
327 docstring const & rdelim, docstring const & presetarg) const
329 otexstringstream ots;
330 OutputParams runparams = runparams_in;
331 if (!pass_thru_chars_.empty())
332 runparams.pass_thru_chars += pass_thru_chars_;
333 if (!newline_cmd_.empty())
334 runparams.newlinecmd = newline_cmd_;
335 runparams.pass_thru = isPassThru();
336 InsetText::latex(ots, runparams);
337 TexString ts = ots.release();
338 bool const add_braces = !ldelim.empty() && ldelim != "{"
339 && support::contains(ts.str, rdelim);
344 if (!presetarg.empty() && !ts.str.empty())
353 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
354 UpdateType utype, TocBackend & backend) const
356 if (!caption_of_toc_.empty()) {
358 text().forOutliner(str, TOC_ENTRY_LENGTH);
359 backend.builder(caption_of_toc_).argumentItem(str);
361 // Proceed with the rest of the inset.
362 InsetText::addToToc(dit, output_active, utype, backend);
366 void InsetArgument::fixParagraphLanguage(Language const * l)
368 Font font(inherit_font, l);
370 font.setLanguage(latex_language);
371 paragraphs().front().resetFonts(font);